Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

13-Oct-23

 The stack is a section of RAM used by the CPU to store


information temporarily.
 This information could be data or an address.
 The CPU needs this storage area because there are only a limited
number of registers.
How stacks are accessed in the AVR
 The register used to access the stack is called the SP (stack
pointer) register.
 In the AVRs with more than 256 bytes of memory the SP is made
of two 8-bit registers (SPL and SPH), while in the AVRs with less
than 256 bytes the SP is made of only SPL
 SPL (the low byte of the SP) and SPH (the high byte of the SP)
reside in I/O memory space.
 The storing of CPU information on the stack is called a PUSH, and
the loading of stack contents back into a CPU register is called a
POP.
 In other words, a register is pushed onto the stack to save it and
popped off the stack to retrieve it.

15

Pushing onto the stack


 The stack pointer (SP) points to the top of the stack
(TOS).
 As we push data onto the stack, the data are saved
where the SP points to, and the SP is decremented by
one.
Syntax:
PUSH Rr ;Rr can be any of the general purpose registers (R0-R31)
For example, to store the value of R10 we can write the
following instruction:
PUSH Rl0 ;store R10 onto the stack, and decrement SP

16

1
13-Oct-23

Popping from the stack


 When the POP instruction is executed, the SP is
incremented and the top location of the stack is copied
back to the register.
 That means the stack is LIFO (Last-In-First-Out)
memory.
Syntax:
POP Rr ;Rr can be any of the general purpose registers {R0-R31)
For example, the following instruction pops from the top
of stack and copies to R10:
POP R16 ;increment SP, and then load the top of stack to Rl0

17

 Example 3-8

18

You might also like