The Stack Chapter 5

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

description

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 byteHalfword = 2 bytes - PowerPoint PPT Presentation

Transcript of The Stack Chapter 5

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

3

Data types

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

• 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

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

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

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)

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

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:

92extrabytes

save %sp, (-92 –(5*4))) & -8, %sp

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

Load Instructions

Mnemonic Operation

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

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

Store Instructions

st %l1, [%fp – 4] !%l1 into a0

st register, memory

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

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

An Example Using Macrosdefine(local_var, ‘define(last_sym, 0)’)define(var, ‘define(‘last_sym’, eval(last_sym-$2))$1 = last_sym’)

local_varvar(a0_s, 4) !a0_s = -4var(a1_s, 4) !a1_s = -8

.global main

main:save %sp, (-92 + last_sym) & -8, %spld [%fp + a0_s], %l1ld [%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, %spld [%fp + a0_offset], %l1ld [%fp + a1_offset], %l2

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

…But we still have problemslocal_varvar(a_s, 4) !a_s = -4var(b_s, 4) !b_s = -8var(ch_s, 1) !ch_s = -9var(c_s, 2) !c_s = -11var(d_s, 4) !d_s = -15

ldsh [%fp + c_s], %o0 ! -11ld [%fp + d_s], %o1 ! -15

Without macro

a_offset = -4 !a_offset = -4b_offset = a_offset – 4 !b_offset = -8ch_offset = b_offset -1 !ch_offset = -9c_offset = ch_offset -2 !c_offset = -11d_offset = c_offset - 4 !d_offset = -15

ldsh [%fp + c_offset], %o0 ! -11ld [%fp + d_offset], %o1 ! -15

19

Aligning Variables

define(‘var’, ‘define(‘last_sym’,eval((last_sym-$2) & -$2)) $1 = last_sym’)

a_s = -4b_s = -8ch_s = -9c_s = -12d_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 = -4b_offset = (aoffset – 4)&-4 !b_offset = -8ch_offset = (b_offset -1)&-1 !ch_offset = -9c_offset = (ch_offset -2)&-2 !c_offset = -12d_offset = (c_offset - 4)&-4 !d_offset = -16

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

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

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

24

Declaring Arrays

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

define(‘var’, ‘define(‘last_sym’,

eval((last_sym ifelse($3,,$2,$3)) & -$2)) $1 = last_sym’)

25

Using macrolocal_varvar(a_s, 4)var(c1_s, 1)var(arr_s, 4, 4 * 5)var(c2_s, 1)var(d_s, 4)

a_s = -4c1_s = -5arr_s = -28c2_s = -29d_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 = -4c1_offset = (a_offset – 1)& -1 !c1_offset = -5arr_offset = (c1_offset – 4*5) & -4 !arr_offset = -28c2_offset = (arr_offset – 1) & -1 !c2_offset = -29d_offset = (c2_offset – 4) & - 4 !d_offset = -36

C variables

int a;char c1;int arr[5];char c2;int d;