1 / 25

The Stack Chapter 5

The Stack Chapter 5. Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois, Abinashi Dhungel. Memory. Addresses are 32 bits wide Can specify up to 2 32 bytes in memory Memory data types Byte = 1 byte Halfword = 2 bytes

Download Presentation

The Stack Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Memory • Addresses are 32 bits wide • Can specify up to 232 bytes in memory • Memory data types • Byte = 1 byte Halfword = 2 bytes • Word = 4 bytes Doubleword = 8 bytes 2

  3. Data types • All memory references must be aligned • x byte quantities must begin in an address divisible by x 3

  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 4

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

  6. “Chopping”

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

  8. Example for aligning the stack • Want to add 92 bytes to the stack • -92 is not divisible by 8 -9210 = 101001002 (size to increase) 10100100 and 11111000 (aligning size) 10100000 10100000 = -96 (aligned) 8

  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 9

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

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

  12. Load Instructions 12

  13. 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

  14. Store Instructions st register, memory st %l1, [%fp – 4] !%l1 into a0 14

  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 15

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

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

  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 18

  19. d %fp -16 %fp -12 c %fp -9 ch %fp - 8 b a %fp - 4 %fp 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 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 19

  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. 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 21

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

  23. 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 23

  24. Declaring Arrays 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(arr_s, 4, 4 * 5) 24

  25. C variables int a; char c1; int arr[5];char c2;int d; 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 %fp –36: d %fp –32: c2 %fp –28: arr[0] %fp –28 + 4: arr[1] %fp -28 + 8: arr[2] %fp -28 + 12: arr[3] 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 arr[4] %fp -28 + 16: %fp -8: c1 a %fp –4: %fp: 25

More Related