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, Abinashi Dhungel.

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, Abinashi Dhungel."— Presentation transcript:

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

2 2 Memory Addresses are 32 bits wide Can specify up to 2 32 bytes in memory Memory data types –Byte = 1 byteHalfword = 2 bytes –Word = 4 bytesDoubleword = 8 bytes

3 3 Data types C TypeSPARCBitsUnsignedSigned charbyte80, 255-128, 127 shorthalf160, 65,535-32,768, 32,767 int, longword320, 4.294x10 9  2.147 x 10 9 All memory references must be aligned x byte quantities must begin in an address divisible by x

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

5 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 the stack Top of stack %sp → 0x00000000 0xf800000

6 “Chopping”

7 7 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, -92 & 0xfffffff8, %sp add%sp, -92 & -8, %sp Results in 96 being subtracted from %sp Let’s see how…

8 8 Example for aligning the stack Want to add 92 bytes to the stack -92 is not divisible by 8 -92 10 = 10100100 2 (size to increase) 10100100 and11111000(aligning size) 10100000 10100000 = -96(aligned)

9 9 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

10 10 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

11 11 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)

12 12 Load Instructions MnemonicOperation ldsb 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

13 13 Load Instruction Format ldmemory, 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

14 14 Store Instructions st%l1, [%fp – 4]!%l1 into a0 stregister, memory MnemonicOperation 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

15 15 Problems with Stack Variable Offsets Define the constants symbolically: define(a0_s, -4) define(a1_s, -8) define(a2_s, -12) ld [%fp + a0_s], %l1 ld [%fp + a1_s], %l2 ld [%fp + a2_s], %l3 …but still have to compute the offsets Stack variable offsets without using macro a0_offset = -4 a1_offset = -8 a2_offset = -12 ld [%fp + a0_offset], %l1 ld [%fp + a1_offset], %l2 ld [%fp + a2_offset], %l3

16 16 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 The example without using macro a0_offset = -4 a1_offset = a0_offset - 4.global main main: save %sp, (-92 + a1_offset) & -8, %sp ld[%fp + a0_offset], %l1 ld[%fp + a1_offset], %l2

17 17 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’)

18 18 …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 Without macro a_offset = -4!a_offset = -4 b_offset = a_offset – 4!b_offset = -8 ch_offset = b_offset -1!ch_offset = -9 c_offset = ch_offset -2!c_offset = -11 d_offset = c_offset - 4!d_offset = -15 ldsh[%fp + c_offset], %o0! -11 ld[%fp + d_offset], %o1! -15

19 19 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 %fp %fp - 4 %fp - 8 %fp -9 %fp -12 %fp -16 d c ch b a Aligning without macro a_offset = -4!a_offset = -4 b_offset = (aoffset – 4)&-4!b_offset = -8 ch_offset = (b_offset -1)&-1!ch_offset = -9 c_offset = (ch_offset -2)&-2!c_offset = -12 d_offset = (c_offset - 4)&-4!d_offset = -16

20 Example -calculate the offsets relative to the frame pointer where you would store the variables in the memory -draw a picture showing the locations of all variable in the memory shading unused memory location. int a, b; char d; short x, y; int u, v; char e;

21 21 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 i th element can be accessed at: address_of_first_element + i * byte_size_of_array_element

22 22 Integer Array in C int arr[5] arr[0] arr[1] arr[2] arr[3] arr[4] arr: arr + 4: arr + 8: arr + 12: arr + 16:

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

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

25 25 Using macro local_var var(a_s, 4) var(c1_s, 1) var(arr_s, 4, 4 * 5) var(c2_s, 1) var(d_s, 4) a_s = -4 c1_s = -5 arr_s = -28 c2_s = -29 d_s = -36 arr[0] arr[1] arr[2] arr[3] arr[4] %fp –28: %fp –28 + 4: %fp -28 + 8: %fp -28 + 12: %fp -28 + 16: %fp -8: c2 c1 d a %fp –36: %fp –32: %fp –4: %fp: Without macro a_offset = -4 ! a_offset = -4 c1_offset = (a_offset – 1)& -1 !c1_offset = -5 arr_offset = (c1_offset – 4*5) & -4 !arr_offset = -28 c2_offset = (arr_offset – 1) & -1 !c2_offset = -29 d_offset = (c2_offset – 4) & - 4 !d_offset = -36 C variables int a; char c1; int arr[5]; char c2; int d;


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

Similar presentations


Ads by Google