stacks and frames

59
Stacks and Frames Memory Stacks Frames Automatic Variables

Upload: collin

Post on 12-Jan-2016

78 views

Category:

Documents


0 download

DESCRIPTION

Stacks and Frames. Memory Stacks Frames Automatic Variables. Memory. SPARC architecture has a 32 bit address register Provides 0x100,000,000 byte-addressable units of memory or 4,294,967,296 (2 32 ) bytes of memory (4 Gigabytes) Data may be stored in memory using Byte.byte1 byte - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Stacks and Frames

Stacks and Frames

MemoryStacksFrames

Automatic Variables

Page 2: Stacks and Frames

Memory

• SPARC architecture has a 32 bit address register• Provides 0x100,000,000 byte-addressable units of

memory or 4,294,967,296 (232) bytes of memory (4 Gigabytes)

• Data may be stored in memory using– Byte .byte 1 byte– Half-word .half 2 bytes– Word .word 4 bytes– Double .double 8 bytes

Page 3: Stacks and Frames

Memory alignment

• All memory references must be aligned• A byte can have any address• A half word must have an even address• A full word must have an address divisible by

4• A double word must have an address

divisible by 8

Page 4: Stacks and Frames

Program Address Space

Code SectionCode

Static variablesOS information

Heap SectionDynamic variables

Stack SectionAutomatic variables

High memoryHigh memory

Low memoryLow memory

Page 5: Stacks and Frames

Stack section

• Contains automatic variables of the functions• Contains frame information for each call of a

function• Stack grows from high memory to low

memory• Really a stack of Frames

Page 6: Stacks and Frames

Stack View

Stack Top %sp ->Stack Top %sp ->

High memoryHigh memory

Low memoryLow memory

Main FrameMain Frame

Function Frame 1Function Frame 1

Function Frame 2Function Frame 2

Function Frame 3Function Frame 3

Page 7: Stacks and Frames

Stack Addresses

• The stack is a stack of double words• Therefore, the stack pointer, register

%o6 or %sp, must always be divisible by 8

• This is accomplished by the save instruction

• save %sp, value & -8, %sp

Page 8: Stacks and Frames

The Frame (1)

Stack Top %sp ->Stack Top %sp ->

Frame Ptr %fp ->Frame Ptr %fp ->High memoryHigh memory

Low memoryLow memory

Page 9: Stacks and Frames

The Frame (2)

<- 64 bytes for <- 64 bytes for register window setregister window set

4 bytes for return 4 bytes for return structure address ->structure address ->

<- 24 bytes for first six <- 24 bytes for first six function parametersfunction parameters

Storage for Local Storage for Local variables ->variables ->

<- 16 bytes for <- 16 bytes for Compiler stuffCompiler stuff

Stack Top %sp ->Stack Top %sp ->

Frame Ptr %fp ->Frame Ptr %fp ->

Page 10: Stacks and Frames

The Frame Size

• Therefore the frame size is always(-108 - local_storage) & -8

• The minus sign is used because the stack grows from high memory to low memory

save %sp, (-108 - local_storage) & -8, %spnew sp = old_sp + (– 108 – local_storage) & -8new_fp = old_sp

Page 11: Stacks and Frames

Addressing Frame Components

• You never need to address the data in the register window set. The OS uses this

• The addresses of the return structure and parameters are referenced using the stack pointer, %sp

Those references are positive from the %sp

• The addresses of automatic variables, i.e. local variables use the frame pointer, %fp

Those references are negative from the %fp

Page 12: Stacks and Frames

Example

• Consider the C declarations:int x, y;char ch;double f;

• x and y take 4 bytes each• ch takes one byte• f takes 8 bytes• Storage starts at %fp -20

Page 13: Stacks and Frames

Example Frame

%fp-36%fp-36 chch

%fp-32%fp-32 ff

%fp-28%fp-28

%fp-24%fp-24 yy

%fp-20%fp-20 xx

%fp-16%fp-16

%fp-12%fp-12

%fp -8%fp -8

%fp-4%fp-4

%fp %fp

Page 14: Stacks and Frames

Addressing Local Variables(1)

• All automatic (local) variables are referenced using the frame pointer and their offsets (negative) from the frame pointer

• We define constants that represent their offsets• define ( x, -20)• define (y, -24)• define (ch, -36) // could use 35, 34, 33• define (f, -32)

Page 15: Stacks and Frames

Loading Local Variables(1)

• To load the variable x into %o0, we use the following instruction

ld [%fp + x], %o0

• To load the variable ch into %l0, we use the following instruction

ldub [%fp + ch], %l0

Page 16: Stacks and Frames

Loading Local Variables(2)

• To load the variable f into %o0 and %o1, we use the following instruction

ldd [%fp + f], %o0

Note: This instruction loads register %o0 with contents of f and register %o1 with contents f+4

Page 17: Stacks and Frames

Storing Local Variables(1)

• To store the variable x from %o0, we use the following instructionst %o0, [%fp + x]

• To store the variable ch from %l0, we use the following instructionstb %l0, [%fp + ch]

Page 18: Stacks and Frames

Storing Local Variables(2)

• To store the variable f from %o0 and %o1, we use the following instructionstd %o0, [%fp + f]

Note: This instruction stores register %o0 into f and register %o1 into f+4

Page 19: Stacks and Frames

Sample Program addxy.m

/*******************************************************

* File: addxy.m

* Dir: cis235/suns/ch05

* Date: February, 1999

* Modified: March 2001

* Author: HGG

* Computer: KUNET suns

* Assembler: sa addxy

* Compile: gcc

* Execute: a.out

* Purpose: To demonstrate local variables. Read

* two integers x and y, add them,

* put the result in sum and print

* everthing out.

*******************************************************/

Page 20: Stacks and Frames

addxy.m (1)

.data

.align 8prompt: .asciz "Enter two integers (^D to quit): "

. align 8formati:asciz "%d %d"

. align 8formato:.asciz "x = %d, y = %d, sum = %d\n"

.align 4 ! automatic data for a program

define(x, -20)define(y, -24)define(sum, -28)

Page 21: Stacks and Frames

addxy.m (2)

.text

.align 4

.global main

main: save %sp, (-108 - 12) & -8, %sp

 

loop: !while (printf(prompt),

sethi %hi(prompt), %o1

call printf,0

or %o1, %lo(prompt), %o0

Page 22: Stacks and Frames

addxy.m (3)

 ! scanf(formati, &x, &y) == 2)set formati, %o0 ! formati address in o0add %fp, x, %o1 ! address of x into o1call scanf,0add %fp, y, %o2 ! address of y into o2

cmp %o0, 2 ! 2 integers read? bne donenop

Page 23: Stacks and Frames

addxy.m (4)

NOTE how x and y are loadedfrom memory! sum = x + y;  ! o1 = x o2 = yld [%fp + x], %o1ld [%fp + y], %o2 add %o1, %o2, %o3 st %o3, [%fp + sum]! leave in o's for printf

Page 24: Stacks and Frames

addxy.m (4)

! printf(formato, x, y, sum)sethi %hi(formato),%l1call printf,0or %l1,%lo(formato),%o0b loopnop

 done: ! exit

callexit, 0mov 0, %o0

Page 25: Stacks and Frames

Exercise

• Modify your field extraction program so that the variables of integer, lsb, nob are local variables.

• Due date: Monday April 2

Page 26: Stacks and Frames

Array Storage

• Consider an array A in C defined as follows:

some_type A[array_size]

The compiler needs to set aside enough storage for the entire array. The following formula is used by the compiler:bytes_needed = array_size * size of some_type

Page 27: Stacks and Frames

Examples

Assume automatic storage is desired• int a[10] requires 40 bytes• double f[25] requires 200 bytes• char ch[25] usually requires 28 bytes (why?)• customertype customers[100] requires 8000

bytes assuming a customertype needs 80 bytes.

Page 28: Stacks and Frames

Array Addressing

• Once stored, how is the address of a component computed.

• Depends on how the elements are stored: big-endian little-endian

• Big-endian stores the low index in lower memory and the higher indices in high memory

• Little-endian stores the low index in high memory and the high indices in low memory

• Comes from the way numbers are stored

Page 29: Stacks and Frames

Big-endian vs Little-endianStoring 0x12345678

1212

3434

5656

7878

7878

5656

3434

1212

High memoryHigh memory

Low memoryLow memory

Big-EndianBig-Endian Little-EndianLittle-Endian

Page 30: Stacks and Frames

Big-endian vs Little-endian

A[0]A[0]

A[1]A[1]

A[size-1]A[size-1]

A[size-1]A[size-1]

A[1]A[1]

A[0]A[0] High memoryHigh memory

Low memoryLow memory

Big-EndianBig-Endian Little-EndianLittle-Endian

Page 31: Stacks and Frames

Big Endian Array Storage

• Most compiler store arrays in big endian order

• Address of a component A[I] is then computed using the following formula:address(A[i]) = address(A) +

i*(size of component)

Page 32: Stacks and Frames

Computing Address of A[i]

A[0]A[0]

A[1]A[1]

A[I]A[I]

A[size-1]A[size-1]

Address of AAddress of A

Address of A[I]Address of A[I]

i componentsi components

Size of 1 componentSize of 1 component

Page 33: Stacks and Frames

Example

• Suppose we have the following definitions in C:int sum;int A[10];int i;

• In assembler we could have the offsets:define(sum, -20)define(A, -60)define(i, -64)

Page 34: Stacks and Frames

Example (con’t)

The address of A[i] is given byaddress(A[i]) = address(A) + i * 4

Assembler code for the address:add %fp, A, %l0 ! Address of Ald [%fp + I], %l1 ! Value of isll %l1, 2, %l2 ! Multiply by 4add %l1, %l2, %l3 ! Address of A[i]

Page 35: Stacks and Frames

Looping Thru an Array

Consider the codesum = 0;

for(i = 0; i < 10; i++) sum = sum + A[i];

We use the computation of the address of A[i] to write the assembler code for the loop

Page 36: Stacks and Frames

Assembler Code for Loopclr %l0 ! sumclr %l1 ! i

loop: cmp %l1, 10bge donenopadd %fp, A, %o0 ! %o0 = addr(A)sll %l1, 2, %l2ld [%o0+ %l2], %o1 !%o1 = A[i]add %l0, %o1, %l0 !sum += A[i];inc %l1 ! i++b loopnop

done: st %l0, [%fp + sum]st %l1, [%fp + i]

Page 37: Stacks and Frames

Example

• Write an assembler program to read in an array of 10 integers and write them out.

Page 38: Stacks and Frames

arr.m (1)

.data .align 8prompt: .asciz "enter ten numbers: " .align 8frmti: .asciz "%d" .align 8frmto: .asciz "The value is %d\n"

Page 39: Stacks and Frames

arr.m (2)

define(i, -20) ! i - loop control variable define(A, -60) ! the array A .align 4 .text .global main

main: save %sp, (-108 - 44) & -8, %sp

Page 40: Stacks and Frames

arr.m (3)

! printf(prompt); sethi %hi(prompt), %o0 call printf, 0 or %o0, %lo(prompt), %o0

!***************************** ! for(i = 0;1 < 4; i++) ! scanf(frmti, &A[i]); mov 0, %l0 ! i = 0; st %l0, [%fp + i]

Page 41: Stacks and Frames

arr.m (4)

loop1: ! scanf(frmti, &A[i]) set frmti, %o0

add %fp, A, %o2 ! the address of A ld [%fp+i], %o1 sll %o1, 2, %o1 ! 4 * i add %o2, %o1, %o1 ! the addr( A[i] ) call scanf, 0 nop

Page 42: Stacks and Frames

arr.m (5)

! i++ ; i < 10? ld [%fp+i], %l0 inc %l0 st %l0, [%fp+i] cmp %l0, 10 bl loop1 nop

Page 43: Stacks and Frames

arr.m (6)

!**************************** ! for(i = 0;1 < 10; i++) ! printf(frmto, A[i]);

mov 0, %l0 ! i = 0; st %l0, [%fp + i]

Page 44: Stacks and Frames

arr.m (7)

loop2: ! printf(frmto, A[i]) != 1 set frmto, %o0 add %fp, A, %o2 ! the address of A ld [%fp+i], %o1 ! %o1 = i sll %o1, 2, %o1 ! %o1 = 4 * i add %o2, %o1, %o1 ! address of A[i] ld [%o1], %o1 ! contents of A[i] call printf, 0 nop

Page 45: Stacks and Frames

arr.m (8)

! i++ ; i < 10? ld [%fp+i], %l0 inc %l0 st %l0, [%fp+i] cmp %l0, 10 bl loop2 nop

Page 46: Stacks and Frames

arr.m (9)

call exit,0 mov 0, %o0

Page 47: Stacks and Frames

Structures

A structure is similar to a class, where all data members are public. In C structures do not have member functions.

The data members of a structure are stored as a composite unit. Each member has a address (offset) measured from the beginning of the structure

Page 48: Stacks and Frames

Example

Consider the customer structure defined below:

typedef struct { char name[24];

int age; int salary; } customer_type;

customer_type customer;

Page 49: Stacks and Frames

Addressing Structure Components

To address a component of a structure, we use the formula:addr(component) = addr(structure)

+ offset of component from beginning of

structureIt is customary to use Big Endian storage for structures, so the offset is positive.

Page 50: Stacks and Frames

customer

namename

ageage

salarysalary

CustomerCustomer

Offset of name: 0Offset of name: 0

Offset of age: 24Offset of age: 24

Offset of salary: 28Offset of salary: 28 High memoryHigh memory

Low memoryLow memory

Page 51: Stacks and Frames

customer.m(1)

/********************************************** File: customer.m* Dir: cis235/ch05* Date: January 2000* Author: HGG* Computer: KUNET suns* Assembler: sa compiles and executes* Purpose: To demonstrate the use of * structures in Assembly language**********************************************/

Page 52: Stacks and Frames

customer.m(2)

.dataprompt: .asciz "Enter the customer:\n"prompt_name: .asciz "Enter name: "prompt_age: .asciz "Enter age: "prompt_salary: .asciz "Enter salary: "fmt_name: .asciz "%s"fmt_age: .asciz "%d"fmt_salary: .asciz "%d"fmt_cust: .asciz "Customer: %s, %d, %d\n"

Page 53: Stacks and Frames

customer.m(3)

define(customer, 48) !frame address of customer define(name, 0) !offset of name within customer define(age, 24) !offset of age within customer define(salary, 28) !offset of salary within customer

Page 54: Stacks and Frames

customer.m(4)

.text .global mainmain: save %sp, (-108 - 32) & -8, %sp

! printf(prompt) set prompt, %o0 call printf nop

Page 55: Stacks and Frames

customer.m(5)

! printf(prompt_name); ! scanf(fmt_name, &customer.name); set prompt_name, %o0 call printf nop set fmt_name, %o0 add %fp, customer+name, %o1 call scanf nop

Page 56: Stacks and Frames

customer.m(6)

! printf(prompt_age); ! scanf(fmt_name, &customer.age); set prompt_age, %o0 call printf nop set fmt_age, %o0 add %fp, customer+age, %o1 call scanf nop

Page 57: Stacks and Frames

customer.m(7)

! printf(prompt_salary); ! scanf(fmt_salary, &customer.salary); set prompt_salary, %o0 call printf nop set fmt_salary, %o0 add %fp, customer+salary, %o1 call scanf nop

Page 58: Stacks and Frames

customer.m(8)

!printf(fmt_cust, customer.name, !customer.age. customer.salary);

set fmt_cust, %o0 add %fp, customer+name, %o1 ld [%fp + customer+age], %o2 ld [%fp + customer+salary], %o3 call printf nop done: call exit mov 0, %o0

Page 59: Stacks and Frames

Exercise

Write a program which will read in an array of five customers and print them out.