Erdefi Rakun
Stack
Stack Pointer
Stack operations in AVR
Subroutine
2
Stack is mainly used for storing:
Temporary data
Local variables
Return Addresses after interrupts and subroutine calls
Stack Pointer Register (SPH and SPL) is an
I/O Register that points to the top of stack
(TOS)
Stack space in SRAM must be defined by
program before subroutine calls are
executed or interrupts are anable
3
Last In First Out (LIFO).
Stack is implemented as growing from
higher memory location to lower memory
locations
4
AVR Stack Pointer is implemented as two
8-bit registers in the I/O space
5
Initial value = 0
Inisialisasi nilai SPL dan SPH
Points to the last byte
in SRAM
6
7
PUSH [Register] instruction is used to push data
onto the stack.
Exp: PUSH R1
will push the content of R1 onto the stack and then
decrement SP by 1 ( SP ← SP – 1)
POP [Register] instruction is used to pop data
from the stack.
Exp: POP R1
Stack Pointer is increment by 1 ( SP ← SP + 1), read
the content of the stack, then store it in R1.
8
SRAM
LDI R16, 04 FF
FF
0x0000
MOV R1,R16 FF
....
....
PUSH R1 FF
FF
.... FF
FF
Stack FF
FF 0x025F
Pointer
9
SRAM
LDI R16, 04 FF
FF
0x0000
MOV R1,R16 FF
....
....
PUSH R1 FF
FF
.... FF
FF
FF
04 0x025F
10
SRAM
.... FF
FF
0x0000
POP R17 FF
....
....
.... FF
FF
FF
FF
FF
04 0x025F
11
SRAM
.... FF
FF
0x0000
POP R17 FF
....
....
.... FF
FF
FF
FF
FF
R17 ← 04 04 0x025F
12
SRAM
LDI R16, 07 FF
FF
0x0000
PUSH R16 FF
....
....
.... FF
FF
FF
FF
FF
04 0x025F
What will happen?
13
Similar to the subroutine call in MIPS
Use RCALL (Relative Call) instruction
RCALL [Label]
Label is the subroutine address
Return address will be stored in the Stack.
14
SRAM
FF
FF
FF
....
....
FF
FF
FF
FF
00
16
0x0016
15
SRAM
FF
FF
FF
....
....
FF
FF
FF
FF
00
16
PC ← 0x0016
16