Docsity

Lecture Slides on Assembly Language Implementation | CS 403, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2003 CS 403 Class Notes Page 1
Lecture 7- Thursday, 1/30/03
• Today
– Finish storage model implementations
• Reading Assignments For Next Class
– Chapter 6
• Assignments
– Assignment #2 due one week from today
Spring 2003 CS 403 Class Notes Page 2
Assembly Language Implementation
• Compiler:
– Input: Source program (C++ or something)
– Output: Object program (machine language)
• Machine language version:
– Operates in terms of registers, main memory locations
– Must implement an appropriate memory model:
• Static
•Stack
• Heap
– Frequently optimized beyond what we are going to discuss
here
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Slides on Assembly Language Implementation | CS 403 and more Study notes Programming Languages in PDF only on Docsity!

Spring 2003 CS 403 Class Notes (^) Page 1

Lecture 7- Thursday, 1/30/

• Today

– Finish storage model implementations

• Reading Assignments For Next Class

– Chapter 6

• Assignments

– Assignment #2 due one week from today

Assembly Language Implementation

• Compiler:

  • Input: Source program (C++ or something)
  • Output: Object program (machine language)

• Machine language version:

  • Operates in terms of registers, main memory locations
  • Must implement an appropriate memory model:
    • Static
    • Stack
    • Heap
  • Frequently optimized beyond what we are going to discuss

here

Spring 2003 CS 403 Class Notes (^) Page 3

Static Memory Model

code

IP

IP contains an integer which gives location of current instruction

data for foo

main

foo

Return address

params

Local vars

data for main

Return address

params

Local vars

Data Segment Structure

• Address 0: Return address (in code)

• Address 1: Function return value

• Addresses 2 to k: Parameter values

• Addresses k+1 to n: Local variables

Spring 2003 CS 403 Class Notes (^) Page 7

All-Static Memory Model – After Load

High-level language code:

void foo(int x, int y){ int a; y++; a = y; }

void main(void){ int x = 3; int y = 2; foo(x, y); x++; }

Assembly code:

Code for foo: INC 5003 SV 5004, 5003 J *

Code for main: SC 3002, 3 SC 3003, 2 SV 5002, 3002 SV 5003, 3003 SA 5000, 1009 J 1000 INC 3002

BOM = 3000 BOF = 5000 BOC = 1000

Stack Memory

• Allows support for recursion

• Activation record structure:

– Address 0: Data return address

– Address 1: Code return address

– Address 2: Function return value

– Addresses 3 to k: Parameter values

– Addresses k+1 to n: Local variables

Spring 2003 CS 403 Class Notes (^) Page 9

Stack Memory: Special Registers

  • Special registers: FP and SP
  • FP (Frame Pointer): Points to the current activation

record.

  • SP (Stack Pointer): Points to the next free space

available for a new activation record.

  • These locations are provided initial values by the

loader:

  • FP: Beginning of the run-time stack
  • SP: End of the “main” activation record

Extension to Assembly Language

SC Addr1, Constant (Addr1 <- Constant)

SV Addr1, Addr2 (Addr1 <- *Addr2)

SA Addr1, Addr2 (Addr1 <- Addr2)

SR Register, Addr (Register <- Addr)

J Addr (Jump to Addr)

INC Addr (Increment value at Addr)

*Addr (Dereference Addr)

*Using a register as an operand where an address is

expected means that the register is resolved to the

address before the operation is applied.

Spring 2003 CS 403 Class Notes (^) Page 13

Exercise

Write the assembly language version of the following code.

Do it two different ways: once assuming static memory, and

once assuming stack memory.

void bar(int y){ y++; }

void foo(int x){ bar(x); x++; }

void main(void){ int x = 2; foo(x); x++; }

Reference Parameters – Post Compile

High-level language code:

void foo(int x, int & y){ int a; y++; a = y; }

void main(void){ int x = 3; int y = 2; foo(x, y); x++; }

Assembly code:

Code for foo: INC *( FP+4 ) SV FP+5, FP+ J *(FP+1)

Code for main: SC FP+3, 3 -- main[3] = 3 SC FP+4, 2 -- main[4] = 2 SA SP, FP -- foo[0] = &main[0] SR FP, SP -- fp = sp SR SP, SP+6 -- sp = sp + 6 SV FP+3, *FP+3 -- foo[3] = main[3] SA FP+4, *FP+4 -- foo[4] = main[4] SA FP+1, BOC+12 -- foo[1] = code[12] J BOC -- jump to code[0] SR SP, FP -- sp = fp SR FP, *FP -- fp = &main[0] INC FP+3 -- main[3]++

Spring 2003 CS 403 Class Notes (^) Page 15

Reference Parameters – Post Load

High-level language code:

void foo(int x, int & y){ int a; y++; a = y; }

void main(void){ int x = 3; int y = 2; foo(x, y); x++; }

Assembly code:

Code for foo: BOC = 2000

INC *( FP+4 )

SV FP+5, FP+

J *(FP+1)

Code for main: SC FP+3, 3 -- main[3] = 3 SC FP+4, 2 -- main[4] = 2 SA SP, FP -- foo[0] = &main[0] SR FP, SP -- fp = sp SR SP, SP+6 -- sp = sp + 6 SV FP+3, *FP+3 -- foo[3] = main[3] SA FP+4, *FP+4 -- foo[4] = main[4] SA FP+1, 2012 -- foo[1] = code[12] J 2000 -- jump to code[0] SR SP, FP -- sp = fp SR FP, *FP -- fp = &main[0] INC FP+3 -- main[3]++

Hybrid Static-Stack Model

• Run-time stack and static data segments exist

• RT stack activation records only contain space

for non-static variables

• Static data segments only contain space for

static variables:

– Return addresses, etc. are stored in the stack

– This makes the static data segment simpler – only

has to contain static local variables

Spring 2003 CS 403 Class Notes (^) Page 19

Hybrid Static-Stack Model

main

foo

x, y

x, y

foo static data segment

return addresses

return addresses

a

code

segment

Function Return Values – Post Compile

High-level language code:

int foo(int x, int y){ int a; y++; a = y; return a; }

void main(void){ int x = 3; int y = 2; int b = foo(x, y); x++; }

Assembly code: Code for foo: INC FP+ SV FP+5, FP+ SV FP+2, FP+ J *(FP+1)

Code for main: SC FP+3, 3 -- main[3] = 3 SC FP+4, 2 -- main[4] = 2 SA SP, FP -- foo[0] = &main[0] SR FP, SP -- fp = sp SR SP, SP+6 -- sp = sp + 6 SV FP+3, *FP+3 -- foo[3] = main[3] SV FP+4, *FP+4 -- foo[4] = main[4] SA FP+1, BOC+12 -- foo[1] = code[12] J BOC -- jump to code[0] SR SP, FP -- sp = fp SR FP, *FP -- fp = &main[0] SV FP+5, SP+2 -- main[5] = foo[2] INC FP+3 -- main[3]++

Spring 2003 CS 403 Class Notes (^) Page 21

Function Return Values – Post Load

High-level language code:

int foo(int x, int y){ int a; y++; a = y; return a; }

void main(void){ int x = 3; int y = 2; int b = foo(x, y); x++; }

Assembly code: Code for foo: INC FP+4 BOC = 2000 SV FP+5, FP+ SV FP+2, FP+ J *(FP+1)

Code for main: SC FP+3, 3 -- main[3] = 3 SC FP+4, 2 -- main[4] = 2 SA SP, FP -- foo[0] = &main[0] SR FP, SP -- fp = sp SR SP, SP+6 -- sp = sp + 6 SV FP+3, *FP+3 -- foo[3] = main[3] SV FP+4, *FP+4 -- foo[4] = main[4] SA FP+1, 2012 -- foo[1] = code[12] J 2000 -- jump to code[0] SR SP, FP -- sp = fp SR FP, *FP -- fp = &main[0] SV FP+5, SP+2 -- main[5] = foo[2] INC FP+3 -- main[3]++

Summary

• Expense vs. flexibility:

– Static model:

  • Addresses fully resolvable by load time
  • Run-time variable accesses relatively cheap

– Stack model:

  • Manipulation of SP and FP are at a cost
  • Run-time address resolution is at a cost
  • Some of this cost can be optimized out in some cases
    • EX. Main is always on the stack in the lowest position so its

addresses can be fully resolved by the compiler/loader

  • Additional cost results in the flexibility to support

recursion