Lecture Overview • Stack and stack operations • Functions and function calls
Microprocessors & Interfacing
– Calling conventions
AVR Programming (III)
Lecturer : Dr. Annie Guo
S2, 2008
COMP9032 Week4
1
S2, 2008
Stack – A data structure in which a data item that is Last In is First Out (LIFO)
• In AVR, a stack is implemented as a block of consecutive bytes in the SRAM memory • A stack has at least two parameters: Bottom-n
• The stack usually grows from higher addresses to lower addresses • The stack bottom is the location with the highest address in the stack • In AVR, 0x0060 is the lowest address for stack 0x0060 • i.e. in AVR, stack bottom >=0x0060
SP
SP
RAMEND
Bottom S2, 2008
2
Stack Bottom
• What is stack?
– Bottom – Stack pointer
COMP9032 Week4
COMP9032 Week4
3
S2, 2008
COMP9032 Week4
4
Stack Pointer
Stack Operations
• In AVR, the stack pointer, SP, is an I/O register pair, SPH:SPL, they are defined in the device definition file
• There are two stack operations: – push – pop
– m64def.inc
• Default value of the stack pointer is 0x0000. Therefore programmers have to initialize a stack before use it. • The stack pointer always points to the top of the stack
pop
push
SP
– Definition of the top of the stack varies:
SP SP
• the location of Last-In element;
Bottom
Bottom
Bottom
– E.g, in 68K
• the location available for the next element to be stored – E.g. in AVR S2, 2008
COMP9032 Week4
5
S2, 2008
PUSH Instruction • • • • • •
Syntax: Operands: Operation: Words: Cycles:
S2, 2008
6
POP Instruction • Syntax: • Operands: • Operation:
push Rr Rr∈{r0, r1, …, r31} (SP) ← Rr SP ← SP –1 1 2
COMP9032 Week4
COMP9032 Week4
• Words: • Cycles:
7
S2, 2008
pop Rd Rd∈{r0, r1, …, r31} SP ← SP +1 Rd ← (SP) 1 2
COMP9032 Week4
8
Functions
C Code Example
• Functions are used
int pow(unsigned int b, unsigned int e) {
– In top-down design
unsigned int i, p; p = 1; for (i=0; i