Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.

Similar presentations


Presentation on theme: "The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois."— Presentation transcript:

1 The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois

2 Memory Addresses are 32 bits wide Therefore, 232 bytes in memory
Each location is numbered consecutively Memory data types Byte = 1 byte Halfword = 2 bytes Word = 4 bytes Doubleword = 8 bytes

3 Data types All memory references must be aligned
C Type SPARC Bits Unsigned Signed char byte 8 0, 255 -128, 127 short half 16 0, 65,535 -32,768, 32,767 int, long word 32 0, 4.294x109 2.147 x 109 All memory references must be aligned x byte quantities must begin in an address divisible by x

4 Memory Allocation and the Stack
Program loaded into low memory usually starts around 0x2000 for SPARC Automatic variables near top of memory = the Stack Stack has LIFO property Register %o6 holds the address of the last element placed on the stack = the stack pointer %sp

5 More about the stack… The stack grows downward – to increase stack space, subtract from stack pointer sub %sp, 64, %sp The stack must be double word aligned 0x0000 %sp -> Top of stack the stack 0xf

6 Aligning the Stack The address in %sp must be divisible by 8
Clear lowest three bits by using the and operation Add a “chopped” negative # to increase size add %sp, -94 & 0xfffffff8, %sp add %sp, -94 & -8, %sp Results in 96 being subtracted from %sp Let’s see how…

7 Example for aligning the stack
Want to add 94 bytes to the stack 94 is not divisible by 8 -9410 = (size to increase) and (aligning size) 2’s Comp: = -96 (aligned)

8 The Frame Pointer The value for %sp is not constant
So it is difficult to reference a variable’s location by using %sp The frame pointer, %fp = %i6, stores a copy of %sp before it is changed The save instruction performs addition and updates %fp

9 Example using %fp Want to add 92 extra bytes and store five 4-byte variables a0: %fp-4: %sp: a4: %fp-20: a2: %fp-12: a1: %fp-8: a3: %fp-16: 92 extra bytes save %sp, (-92 –(5*4))) & -8, %sp

10 Addressing Stack Variables
Load and Store operations are the only instructions that reference memory Both instructions take two operands One memory, and one register Can access memory using different data types (byte, half word, word, double word)

11 Load Instructions Mnemonic Operation ldsb ldub ldsh lduh ld ldd
Load signed byte, propagate sign left in register ldub Load unsigned byte, clear high 24 bits of register ldsh Load signed halfword, propogate sign left in register lduh Load unsigned halfword, clear high 16 bits of register ld Load word ldd Load double, reg. # even, first 4 bytes into reg. n, next 4 into reg. n + 1

12 Load Instruction Format
ld memory, register ld [%fp – 4], %l1 !a0 into %l1 ld [%fp – 8], %l2 !a1 into %l2 ld [%fp – 16], %l4 !a3 into %l4 Note: Memory argument can be a register, register + immediate, or register + register

13 Store Instructions st register, memory st %l1, [%fp – 4] !%l1 into a0
Mnemonic Operation stb Store low byte of register, bits 0-7, into memory sth Store low two bytes of register, bits 0-15 into memory st Store register std Store double, reg. # even, first 4 bytes from reg. n, next 4 from reg. n + 1 st register, memory st %l1, [%fp – 4] !%l1 into a0

14 Problems with Stack Variable Offsets
Define the constants symbolically: define(a0_s, -4) ld [%fp + a0_s], %l1 define(a0_s, -8) ld [%fp + a1_s], %l2 define(a0_s, -12) ld [%fp + a2_s], %l3 …but still have to compute the offsets

15 An Example Using Macros
define(local_var, ‘define(last_sym, 0)’) define(var, ‘define(‘last_sym’, eval(last_sym-$2))$1 = last_sym’) local_var var(a0_s, 4) !a0_s = -4 var(a1_s, 4) !a1_s = -8 .global main main: save %sp, (-92 + last_sym) & -8, %sp ld [%fp + a0_s], %l1 ld [%fp + a1_s], %l2

16 Defining Stack Variable Offsets
Define macros to compute the offsets and make the definitions… define(local_var, ‘define(last_sym, 0)’) define(var, ‘define(‘last_sym’, eval(last_sym - $2))$1 = last_sym’)

17 …But we still have problems
local_var var(a_s, 4) !a_s = -4 var(b_s, 4) !b_s = -8 var(ch_s, 1) !ch_s = -9 var(c_s, 2) !c_s = -11 var(d_s, 4) !d_s = -15 ldsh [%fp + c_s], %o0 ! -11 ld [%fp + d_s], %o1 ! -15

18 Aligning Variables define(‘var’, ‘define(‘last_sym’,
eval((last_sym-$2) & -$2)) $1 = last_sym’) a_s = -4 b_s = -8 ch_s = -9 c_s = -12 d_s = -16 %sp -> %fp -16 d %fp -12 c %fp -9 ch %fp - 8 b a %fp - 4 %fp

19 One-Dimensional Arrays
A one-dimensional array (vector) is a block of memory into which a # of variables, all of the same type, may be stored Array address = address of first element = pointer to the array The ith element can be accessed at: address_of_first_element + i * byte_size_of_array_element

20 Integer Array in C int ary[5] ary[0] ary[1] ary[2] ary[3] ary[4] ary:

21 If-Else Macro Takes 4 arguments If 1st string argument = 2nd
Then value is 3rd argument Else value is 4th argument Example: ifelse(a,b,c,d) = d since a  b

22 Declaring Arrays This checks to see if a 3rd argument is present
define(‘var’, ‘define(‘last_sym’, eval((last_sym ifelse($3,,$2,$3)) & -$2)) $1 = last_sym’) This checks to see if a 3rd argument is present If not, then # subtracted from last_sym is 2nd If so, then 3rd argument subtracted var(a_s, 4) vs. var(ary_s, 4, 4 * 5)

23 %fp –36: d local_var var(a_s, 4) var(c1_s, 1) var(ary_s, 4, 4 * 5) var(c2_s, 1) var(d_s, 4) a_s = -4 c1_s = -5 ary_s = -28 c2_s = -29 d_s = -36 %fp –29: c2 %fp –28: ary[0] %fp –28 + 4: ary[1] %fp : ary[2] %fp : ary[3] %fp : ary[4] %fp -5: c1 %fp –4: a %fp:


Download ppt "The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois."

Similar presentations


Ads by Google