Docsity

Assembly Language Implementation and Memory Models in CS 403 Class, 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:
– Operat es in terms of registers, main memory loc ations
– Must imple ment 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
Spring 2003 CS 403 Class Notes Page 4
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 5
A Simple Assembly Language
SC Addr1, Constant (Addr1 <- Constant)
SV Addr1, Addr2 (Addr1 <- *Addr2)
SA Addr1, Addr2 (Addr1 <- Addr2)
J Addr (Jump to Addr)
INC Addr (Increment value at Addr)
*Addr (Dereference Addr)
Spring 2003 CS 403 Class Notes Page 6
All-Static Memory Model – After 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 BOF+3
SV BOF+4, BOF+3
J *BOF //Why saved instead of
hard-coded?
Code for main:
SC BOM+2, 3
SC BOM+3, 2
SV BOF+2, BOM+2
SV BOF+3, BOM+3
SA BOF, BOC+9
JBOC
INC BOM+2
BOF = Initial address in foo’s data segment
BOM = Initial address in main’s data segment
BOC = Beginning of code segment
pf3
pf4

Partial preview of the text

Download Assembly Language Implementation and Memory Models in CS 403 Class 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

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

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

Spring 2003 CS 403 Class Notes Page 4

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 5

A Simple Assembly Language

SC Addr1, Constant (Addr1 <- Constant)

SV Addr1, Addr2 (Addr1 <- *Addr2)

SA Addr1, Addr2 (Addr1 <- Addr2)

J Addr (Jump to Addr)

INC Addr (Increment value at Addr)

*Addr (Dereference Addr)

All-Static Memory Model – After 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 BOF+ SV BOF+4, BOF+ J *BOF //Why saved instead of hard-coded? Code for main: SC BOM+2, 3 SC BOM+3, 2 SV BOF+2, BOM+ SV BOF+3, BOM+ SA BOF, BOC+ J BOC INC BOM+

BOF = Initial address in foo’s data segment BOM = Initial address in main’s data segment BOC = Beginning of code segment

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

Spring 2003 CS 403 Class Notes (^) Page 8

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

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

Spring 2003 CS 403 Class Notes Page 10

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 11

Addressing Issues – After 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+ 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] 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] INC FP+3 -- main[3]++

Addressing Issues – 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: [BOC = 2000] Code for foo: INC FP+ 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] 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] INC FP+3 -- main[3]++

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

Spring 2003 CS 403 Class Notes (^) Page 20

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]++

Spring 2003 CS 403 Class Notes Page 22

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