Princeton University
COS 217:  Introduction to Programming System

Precept 15:  Using the Stack in SPARC Assembly Language

Purpose

Help you learn how to use the runtime stack in SPARC assembly language programs

Reading

Paul, Chapter 5

Approach

Study many small C programs and corresponding hand-written assembly language programs

After studying each program, refer to summary sheets to reinforce and generalize the new material that the program illustrates

The SPARC Stack

Describe stack structure using SPARC Architecture Stack Structure diagram

Stack consists of stack frames

Each stack frame holds the local data for one function

%sp (alias %o6) contains address of top of stack frame

%fp (alias %i6) contains address of bottom of stack frame

Stack grows toward low memory

Register window

(See lecture notes -- described briefly later)

Assembly language programmer must allocate space for it  (16 words = 64 bytes)

Structure pointer

(Described briefly later)

Assembly language programmer must allocate space for it (1 word = 4 bytes)

Actual parameters 1 - 6

(Described briefly later)

Assembly language programmer must allocate space for it (6 words = 24 bytes)

Actual parameters 7, ... and saved floating-point registers

(Described briefly later)

Assembly language programmer must allocate space for them

Local variables

Assembly language programmer must allocate space for them

Assembly language programmer must determine where each local variable resides in the stack

Locations are expressed as offsets from %fp

To determine size of stack frame:

Must be at least 64 + 4 + 24 = 92 bytes

Must be a multiple of 8 => must be at least 96 bytes

Must contain extra space (beyond 92 bytes) for actual parameters and local variables

Example:  sumstack

Illustrates using the stack

See sumstack.c, sumstack.S

What it does

Same as previous "sum..." programs

How it works

Uses stack to store local variables

Unnecessary (could use registers instead), but illustrative

The code:  sumstack.c

Defines local variables within main function

Those variables will be stored in stack

The code:  sumstack.S

(Not optimized -- for clarity and to illustrate stack manipulation)

#define FIRST_OFFSET 4
#define SECOND_OFFSET 8
#define SUM_OFFSET 12
#define I_OFFSET 16

Give symbolic names to stack offsets

#define MAIN_LOCAL_SIZE 16

Specifies number of bytes required to store local variables

save %sp, (-92 - MAIN_LOCAL_SIZE) & -8, %sp
(-92 - MAIN_LOCAL_SIZE) & -8
(-92 - 16) & -8
-108 & -8
-112

When x is a negative number, x & -8 yields the largest multiple of 8 that is less than x

Trick for computing stack frame size

Takes current value of %sp, adds -112

Sets %fp to old value of %sp

Stores computed value (old value of %sp - 112) in %sp

Thus pushes a new stack frame onto the stack

[%fp - FIRST_OFFSET]
[%fp - SECOND_OFFSET]
[%fp - I_OFFSET]
[%fp - SUM_OFFSET]

Expressions in address specifications in ld and st instructions to access local variables on stack

restore

Synthetic instruction for restore %g0, %g0, %g0

Restores %sp and %fp to their values prior to the save

Thus pops a stack frame from the stack

Copyright © 2002 by Robert M. Dondero, Jr.