Stack Pointer
The Stack is mainly used for storing temporary data, for storing local variables and for
storing return addresses after interrupts and subroutine calls. The Stack Pointer Regis-
ter always points to the top of the Stack. Note that the Stack is implemented as growing
from higher memory locations to lower memory locations. This implies that a Stack
PUSH command decreases the Stack Pointer.
The Stack Pointer points to the data SRAM stack area where the Subroutine and Inter-
rupt Stacks are located. This Stack space in the data SRAM must be defined by the
program before any subroutine calls are executed or interrupts are enabled. The Stack
Pointer must be set to point above $60. The Stack Pointer is decremented by one when
data is pushed onto the Stack with the PUSH instruction, and it is decremented by two
when the return address is pushed onto the Stack with subroutine call or interrupt. The
Stack Pointer is incremented by one when data is popped from the Stack with the POP
instruction, and it is incremented by two when data is popped from the Stack with return
from subroutine RET or return from interrupt RETI.
The AVR Stack Pointer is implemented as two 8-bit registers in the I/O space. The num-
ber of bits actually used is implementation dependent. Note that the data space in some
implementations of the AVR architecture is so small that only SPL is needed. In this
case, the SPH Register will not be present.
Bit
Read/Write
Initial Value
15
14
13
12
11
10
9
SP15
SP14
SP13
SP12
SP11
SP10
SP9
SP7
SP6
SP5
SP4
SP3
SP2
SP1
7
6
5
4
3
2
1
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
0
0
0
0
0
0
0
0
0
0
0
0
0
0
8
SP8
SP0
0
R/W
R/W
0
0
SPH
SPL
RAM Page Z Select Register –
RAMPZ
Bit
7
6
5
4
3
2
1
0
–
––
–
–
–
–
–
RAMPZ0 RAMPZ
Read/Write
R
R
R
R
R
R
R
R/W
Initial Value
0
0
0
0
0
0
0
0
• Bits 7..1 – Res: Reserved Bits
These are reserved bits and will always read as zero. When writing to this address loca-
tion, write these bits to zero for compatibility with future devices.
• Bit 0 – RAMPZ0: Extended RAM Page Z-pointer
The RAMPZ Register is normally used to select which 64K RAM Page is accessed by
the Z-pointer. As the ATmega128 does not support more than 64K of SRAM memory,
this register is used only to select which page in the program memory is accessed when
the ELPM/SPM instruction is used. The different settings of the RAMPZ0 bit have the
following effects:
RAMPZ0 = 0:
RAMPZ0 = 1:
Program memory address $0000 - $7FFF (lower 64K bytes) is
accessed by ELPM/SPM
Program memory address $8000 - $FFFF (higher 64K bytes) is
accessed by ELPM/SPM
Note that LPM is not affected by the RAMPZ setting.
14 ATmega128
2467O–AVR–10/06