Title: Stack
1Stack
- Special data structure
- Last-In, First-Out (LIFO) list
- Pointer to next (empty) place in list top of
stack - Base of stack
- Push operation add item to list at top and
update top - Pop/Pull operation remove item from top of list
and update top return value of top item
2Stack
- Special block of memory
- Base - Usually initialized to a large address
- Stack usually grows toward lower addresses
- Push operation decrement top of stack
- Pop operation increment top of stack
- Top stored in a CPU register called the Stack
Pointer (SP) - GPR R1 in CodeWarrior
- SP is like a pointer variable in C it is the
address of the top of the stack
3Stack
- Use of a stack
- Temporary storage during program runtime
- Not fixed in size grows and shrinks using push
and pop operations - Not typed elements of list not defined as one
type - Subroutines (functions, procedures)
- Linkage saving a link back to the caller
(address of calling routine) - Data return value, parameters, local variables
- Other saved information
- Managed at the assembly language level
- Rules for compiler, AL programmer
4Stack
SP 0x7FFF SP points to top - next empty
location to push data to
5Stack
Push 15 Takes up 1 byte on the stack SP is now
pointing to 0x7FFE (next location to push data to)
6Stack
Push -3 Takes up 1 byte on the stack SP is now
pointing to 0x7FFD (next location to push data to)
7Stack
Push 0x1808 0x1808 is a 16-bit value Takes up
two bytes on stack Pushed LSB, then
MSB Why? Little Endian LSB is at higher
address Stack grows toward lower addresses SP
is now 0x7FFB
8Stack
Pop a byte value Update SP first by
incrementing, then read value Value read
0x18 SP is now 0x7FFB
9Stack
Pop a half-word value Update SP, read MSB, read
LSB Value MSB, LSB 0x08, 3 0x08,
0xFD 0x08FD SP is now 0x7FFE
10Stack
- Pushing and Popping
- Should be symmetric what goes on, must come off
- When you pop an item from the stack, the value is
still there in memory (i.e., it is not cleaned
out) - Declare local variables in C - initial values are
whatever was left on the stack - Stack does not know that a byte is part of a
32-bit, 16-bit, or 8-bit value - For PPC, stack grows minimum /- 4 bytes at a
time - Word alignment is enforced, where word boundaries
have addresses with least-significant nibbles of
0, 4, 8, C - SP assigned word-aligned addresses only,
incremented and decremented by a minimum of 4
bytes
11Stack
- Notes - section 4.6
- Assembly language view of the stack
- Revisit again later
- Selected topics
- Subroutine linkage using Link Register
- Nested subroutines
- Local variables
12Stack
- Subroutine call and return
- LR Link Register
- Call Do_Tasks
- Return address in main
- RA saved in LR
- Inside Do_Tasks
- Call Do_Calc
- Return address in Do_Tasks
- Saved?
- Need to use stack
main () int j Do_Tasks () Do_Tasks2
() void Do_Tasks () ... fResult
Do_Calc(15) ... float Do_Calc (float
fNum)
13Stack
14Stack
- Local variables
- long lVal
- char byVal
- char szName10
- Total Space 4 1 10 15 bytes
- Decrease SP by 16 to make space
- Why 16 bytes?
- PowerPC is word-aligned
- Need to keep stack pointer at an even multiple of
4 bytes
15Stack
16Rules
- EABI Embedded Application Binary Interface
- Specifies the interface between compiled
application programs and the processor - API how software is used in applications
- ABI how hardware (or low-level software) is used
in applications - Reduce memory usage and optimize execution speed
- Describes conventions for register usage,
parameter passing, stack organization, small data
areas, object file, and executable file formats
17Rules
- Data types and alignment
- All data types are aligned in memory on addresses
that are a multiple of their size - Word-aligned Since size is 4 bytes, a word is
aligned on an address evenly divisible by 4
18Rules
- Register usage conventions
- Will learn more later about processor registers
in assembly language programming
Recall GPR Register View in CodeWarrior R
General PurposeF FloatsCR Conditions
19Rules
- Stack frame
- Organizes or delineates a functions stack space
- Supports parameter passing, register
preservation, local variables, and code debugging - Place various data into a stack frame in a
consistent manner - Important for functions that call other functions
(calling chain) - SP always points to the lowest addressed word of
the currently executing functions stack frame.
20Rules
- Stack frame
- New frame is created adjacent to the most
recently allocated frame lower in memory
(pushed). - Creation decrement the SP just once, in the
function prologue, by the total amount of space
required by that function - store-with-update (stwu) instruction insure the
SP update is an atomic operation that cannot be
interrupted - Removed in the function's epilogue by adding the
current stack frame size to the SP before the
function returns to the calling function
21Rules
22Rules
LR Link Register, stores return address of caller
23Rules
- Parameter passing
- For PowerPC processors, it is more efficient to
pass arguments (parameters) in registers than
using memory. - Up to eight scalar values are passed by using R3
through R10 - Return values are passed back in R3 and R4