The Stack Chapter 5 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

The Stack Chapter 5

Description:

The Stack. Chapter 5. Lecture notes for SPARC Architecture, ... by Anu G. Bourgeois. 2. Memory. Addresses are 32 bits wide. Therefore, 232 bytes in memory ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 24
Provided by: csc82
Learn more at: http://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: The Stack Chapter 5


1
The StackChapter 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
  • 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
Top of stack
sp -gt
the stack
0xf8000000
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
  • Lets see how

7
Example for aligning the stack
  • Want to add 94 bytes to the stack
  • 94 is not divisible by 8
  • -9410 01000102 (size to increase)
  • 0100010
  • and 1111000 (aligning size) 0100000
  • 2s Comp 1100000 -96 (aligned)

8
The Frame Pointer
  • The value for sp is not constant
  • So it is difficult to reference a variables
    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

save sp, (-92 (54))) -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
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
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 -gt 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 ary5
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
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
d
fp 36
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
c2
fp 29
ary0
fp 28
ary1
fp 28 4
ary2
fp -28 8
ary3
fp -28 12
ary4
fp -28 16
fp -5
c1
a
fp 4
fp
Write a Comment
User Comments (0)
About PowerShow.com