Download ppt - Memory and Stack

Transcript
Page 1: Memory and Stack

Natawut Nupairoj Assembly Language 1

Memory and Stack

Page 2: Memory and Stack

Natawut Nupairoj Assembly Language 2

Memory

• In SPARC, there are only 32 registers.• Not enough to hold all data for computation.• We use memory to store variables.• Variables in memory can be:

C SPARC bitschar byte 8short halfword 16int, long word 32

Page 3: Memory and Stack

Natawut Nupairoj Assembly Language 3

Memory

• All variables in memory must be “aligned”.– A “short” variable is 16-bit long or halfword (2 bytes)– It must be stored in the addressed that are divisible by

“two”, or “even address” (aligned to two-byte boundary)

– For example, 0, 2, 4, …, 1024, 1026, etc.

– An “int” variable is 32-bit long or one word (4 bytes)– It must be stored in the addressed that are divisible by

“four”. (aligned to four-byte boundary).– For example, 0, 4, 8, …, 1024, 1028, etc.

• This is for efficiency in hardware.

Page 4: Memory and Stack

Natawut Nupairoj Assembly Language 4

Memory

• The SPARC architecture is big endian.• Store LSB (the smallest-numbered byte) at the

first address.• For example: to store a short variable containing

0x0932 at address 1026 (must be aligned!)• 0x32 (LSB) is stored at address 1026.• 0x09 (MSB) is stored at address 1027.

• Note: an instruction must be word-aligned. Why ?

Page 5: Memory and Stack

Natawut Nupairoj Assembly Language 5

The Stack

• We store automatic or “local” variables in the memory called “Stack”.

• An automatic variable is a variable that is accessible only inside a function.

int g;

int main() {

int i, j;

...

}

Global var

Local vars

Page 6: Memory and Stack

Natawut Nupairoj Assembly Language 6

The Stack Pointer

• The stack is last-in-first-out (LIFO).• Each program has its own private stack.• %o6 aka. %sp is the stack pointer.• Stack is located near the top of memory

(biggest addressed).• When the stack grows, the %sp decreases.• When the stack shrinks, the %sp increases.• Thus to get more spaces in the stack, we

subtract the number of bytes from the stack pointer.

Page 7: Memory and Stack

Natawut Nupairoj Assembly Language 7

The Stack Pointer

• To get 64 bytes more:– Sub %sp, 64, %sp

G rows downward

The S tack

% sp Top o f the stack

0x00000000

Program

0xf8000000

The S tack

current% sp

new% sp

Page 8: Memory and Stack

Natawut Nupairoj Assembly Language 8

The Stack Pointer

• The stack must be doubleword (8-byte) aligned.

• Thus, the address must be divisible by eight.• If we want 94 bytes, we must ask for 96 bytes

to keep the stack aligned.• Thus:

sub %sp, 96, %sp

• Or we can:add %sp, -94 & -8, %sp

• Why -94&-8 ? Check out two’s complement.

Done by assembler*

Page 9: Memory and Stack

Natawut Nupairoj Assembly Language 9

The Frame Pointer

• The stack pointer is always changed as more variables are needed.

• How can we refer to a variable ?• Use the frame pointer, %fp or %i6.• The frame pointer remains fixed for each

subroutine.• At the beginning of the program, we execute a

“save” instruction to allocate space in the stack.

Page 10: Memory and Stack

Natawut Nupairoj Assembly Language 10

Frame and Stack Pointers

• Subroutine A calls B:

int A() {

...

B();

...

}

int B() {

...

}

S tack area fo r A

S tack area fo r A

S tack area fo r B

% fp

% sp

(% fp)

(% sp) % fp

% sp

BeforeCall

AfterCall

Page 11: Memory and Stack

Natawut Nupairoj Assembly Language 11

Save Instruction

• The save instruction must allocate space for both local variables and registers.

• Must allocate 64 bytes + spaces for variables.save %sp, -64-bytes_for_vars, %sp

• Suppose we want to store five “int” (4-byte) variables (var0 - var4):

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

• This is actually:save %sp, -88, %sp

Page 12: Memory and Stack

Natawut Nupairoj Assembly Language 12

Save Instruction

var4: %fp - 20

var3: %fp - 16

var2: %fp - 12

var1: %fp - 8

var0: %fp - 4

64 bytes o f sto rage tosave registers

% sp

% fp

Page 13: Memory and Stack

Natawut Nupairoj Assembly Language 13

Addressing Stack Variables

• As SPARC is the load-store architecture, we cannot compute variables data from the stack directly.

• We must load them to registers, compute, and then store back to the stack.

• Remember all variables must be aligned based on its size.

• SPARC has different load/store instructions for each type.

Page 14: Memory and Stack

Natawut Nupairoj Assembly Language 14

Load Instructions

• ldsb - load signed byte, propagate sign.• ldub - load unsigned byte, clear high 24 bits of

register.• ldsh - load signed halfword, propagate sign.• lduh - load unsigned halfword, clear high 16

bits of register.• ld - load word• ldd - load double, register number even, first

four bytes into register n, next four into register n+1.

Page 15: Memory and Stack

Natawut Nupairoj Assembly Language 15

Load Instructions

ld [%fp - 4], %l1 ! Load var0 into %l1

ld [%fp - 8], %o2 ! Load var1 into %o2

mov -16, %l4

ld [%fp + %l4], %l3 ! Load var3 into %l3

ldd [%fp - 16], %g2 ! Load var3 into %g2

! and var2 into %g3

ldd [%fp - 16], %l5 ! Illegal, why ?

Page 16: Memory and Stack

Natawut Nupairoj Assembly Language 16

Store Instructions

• 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, register number even, first

four bytes from register n, next four from register n+1.

Page 17: Memory and Stack

Natawut Nupairoj Assembly Language 17

Store Instructions

st %l1, [%fp - 4] ! Store %l1 into var0

st %o2, [%fp - 8] ! Store %o2 into var1

sth %l4, [%fp - 6] ! Store halfword of %l4

sth %l4, [%fp - 9] ! Illegal, why ?

st %o2, [%fp - 4 + %l2] ! Illegal, why ?

st %o2, [%fp - 5120] ! Illegal, why ?

Page 18: Memory and Stack

Natawut Nupairoj Assembly Language 18

Variable Offsets in Stack

• We use the frame pointer as the base reference to variables in the stack.

• All variables must be properly aligned.• Example:

int a, b; // 4 bytes each

char ch; // 1 byte

short c, d; // 2 bytes each

unsigned e; // 4 bytes

Page 19: Memory and Stack

Natawut Nupairoj Assembly Language 19

Variable Offsets in Stack

a: %fp - 4 b: %fp - 8ch: %fp - 9 c: %fp - 12 d: %fp - 14 e: %fp - 20

a

e

b

ch

c

%fp - 4

%fp - 8

%fp - 9

%fp - 10

%fp - 12

%fp - 14

%fp - 20

d

%fp - 16

Page 20: Memory and Stack

Natawut Nupairoj Assembly Language 20

Actual Addresses

a

e

b

ch

c

%fp - 4

%fp - 8

%fp - 9

%fp - 10

%fp - 12

%fp - 14

%fp - 20

d

%fp - 16

fp-20 fp-19 fp-18 fp-17

fp-4 fp-3 fp-2 fp-1

Page 21: Memory and Stack

Natawut Nupairoj Assembly Language 21

Offsets and Stack Allocation

• Use macro to arrange the offsetsdefine(a_s, -4)

define(b_s, -8)

define(ch_s, -9)

define(c_s, -12)

define(d_s, -14)

define(e_s, -20)

• Allocate spaces on stack:save %sp, ((-64 - 20) & -8), %sp

==> save %sp, -84 & -8, %sp

==> save %sp, -88, %sp

Page 22: Memory and Stack

Natawut Nupairoj Assembly Language 22

Manipulate Variables in Stack

• To load and storeld [%fp + a_s], %l0

ldub [%fp + ch_s], %l1 ! char type is unsigned.

ldsh [%fp + d_s], %l2 ! short type is signed.

ld [%fp + e_s], %l3

• To compute:

b = a + c;ld [%fp+a_s], %l0ldsh [%fp+c_s], %l1add %l0, %l1, %l2st %l2, [%fp+b_s]

Page 23: Memory and Stack

Natawut Nupairoj Assembly Language 23

Variables in Registers

• Some variables are used very often.– Loop counters

• We can use registers to hold their values instead of using stack.

• In C, we use a keyword “register”.register int i; // i is in a register.

• When referred to these variables, we use values from registers directly.

Page 24: Memory and Stack

Natawut Nupairoj Assembly Language 24

Variables in Registers

int a, b;

register int j, k;

int x, y;

• Only a, b, x, and y are in the stack.• j and k are in registers.

define(a_s, -4)

define(b_s, -8)

define(x_s, -12)

define(y_s, -16)

define(j_r, l0)

define(k_r, l1)

Page 25: Memory and Stack

Natawut Nupairoj Assembly Language 25

Variables in Registers

• To compute:

j = 19;

a = 8;

y = j - 3 + a;

• Note: we use %l2 and %l3 as temporary registers.

mov 19, %j_rmov 9, %l2st %l2, [%fp+a_s]sub %j_r, 3, %l2ld %l3, [%fp+a_s]add %l2, %l3, %l2st %l2, [%fp+y_s]

Page 26: Memory and Stack

Natawut Nupairoj Assembly Language 26

Our Fourth Program

main()

{

int a, b, c;

register int i;

i = 0;

a = 100;

b = 15;

c = 0;

while(i < 20) {

c += a - b;

a--;

i = i + 2;

}

}

Page 27: Memory and Stack

Natawut Nupairoj Assembly Language 27

Our Fourth Program

define(a_s, -4)

define(b_s, -8)

define(c_s, -12)

define(i_r, l0)

.global main

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

clr %i_r ! i = 0;

mov 100, %l1

st %l1, [%fp + a_s] ! a = 100;

mov 15, %l1

st %l1, [%fp + b_s] ! b = 15;

Page 28: Memory and Stack

Natawut Nupairoj Assembly Language 28

Our Fourth Program

clr %l1

st %l1, [%fp + c_s] ! c = 0;

loop: cmp %i_r, 20

bge done

nop

ld [%fp + a_s], %l1

ld [%fp + b_s], %l2

sub %l1, %l2, %l3 ! a - b

ld [%fp + c_s], %l1

add %l1, %l3, %l1 ! c + a - b

st %l1, [%fp + c_s] ! c += a - b;

Page 29: Memory and Stack

Natawut Nupairoj Assembly Language 29

Our Fourth Program

ld [%fp + a_s], %l1

sub %l1, 1, %l1

st %l1, [%fp + a_s] ! a--;

add %i_r, 2, %i_r ! i = i + 2

ba loop

nop

done: mov 1, %g1

ta 0


Recommended