A stack register is a computer central
processor register
A processor register is a quickly accessible location available to a computer's processor. Registers usually consist of a small amount of fast storage, although some registers have specific hardware functions, and may be read-only or write-onl ...
whose purpose is to keep track of a
call stack
In computer science, a call stack is a Stack (abstract data type), stack data structure that stores information about the active subroutines and block (programming), inline blocks of a computer program. This type of stack is also known as an exe ...
. On an
accumulator-based architecture machine, this may be a dedicated register. On a machine with multiple
general-purpose registers, it may be a register that is reserved by convention, such as on the
IBM System/360
The IBM System/360 (S/360) is a family of mainframe computer systems announced by IBM on April 7, 1964, and delivered between 1965 and 1978. System/360 was the first family of computers designed to cover both commercial and scientific applicati ...
through
z/Architecture architecture and
RISC
In electronics and computer science, a reduced instruction set computer (RISC) is a computer architecture designed to simplify the individual instructions given to the computer to accomplish tasks. Compared to the instructions given to a comp ...
architectures, or it may be a register that procedure call and return instructions are hardwired to use, such as on the
PDP-11,
VAX, and
Intel x86 architectures. Some designs such as the
Data General Eclipse had no dedicated register, but used a reserved hardware memory address for this function.
Machines before the late 1960s—such as the
PDP-8 and
HP 2100
The HP 2100 is a series of 16-bit minicomputers that were produced by Hewlett-Packard (HP) from the mid-1960s to early 1990s. Tens of thousands of machines in the series were sold over its 25-year lifetime, making HP the fourth-largest minicomp ...
—did not have compilers which supported
recursion
Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
. Their subroutine instructions typically would save the current location in the jump address, and then set the program counter to the ''next'' address.
While this is simpler than maintaining a stack, since there is only one return location per subroutine code section, there cannot be recursion without considerable effort on the part of the programmer.
A
stack machine
In computer science, computer engineering and programming language implementations, a stack machine is a computer processor or a Virtual machine#Process virtual machines, process virtual machine in which the primary interaction is moving short- ...
may have 2 or more stack registers — one of them keeps track of a
call stack
In computer science, a call stack is a Stack (abstract data type), stack data structure that stores information about the active subroutines and block (programming), inline blocks of a computer program. This type of stack is also known as an exe ...
, the other(s) keep track of other
stack(s).
Stack registers in x86
In the 16-bit version of the
x86
x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel, based on the 8086 microprocessor and its 8-bit-external-bus variant, the 8088. Th ...
architecture, the main stack register is the 16-bit Stack Pointer (SP), with the 32-bit
IA-32
IA-32 (short for "Intel Architecture, 32-bit", commonly called ''i386'') is the 32-bit version of the x86 instruction set architecture, designed by Intel and first implemented in the i386, 80386 microprocessor in 1985. IA-32 is the first incarn ...
version extending SP to the 32-bit Extended Stack Pointer (ESP), and the 64-bit
x86-64
x86-64 (also known as x64, x86_64, AMD64, and Intel 64) is a 64-bit extension of the x86 instruction set architecture, instruction set. It was announced in 1999 and first available in the AMD Opteron family in 2003. It introduces two new ope ...
version extending ESP to the 64-bit Register Stack Pointer (RSP). The stack segment register (SS) is usually used to store information about the
memory segment that stores the
call stack
In computer science, a call stack is a Stack (abstract data type), stack data structure that stores information about the active subroutines and block (programming), inline blocks of a computer program. This type of stack is also known as an exe ...
of currently executed program. SP points to current stack top. By default, the stack grows downward in memory, so newer values are placed at lower memory addresses. To save a value to the stack, the
PUSH
instruction is used. To retrieve a value from the stack, the
POP
instruction is used.
Example: Assuming that SS = 1000h and SP = 0xF820. This means that current stack top is the physical address 0x1F820 (this is due to
memory segmentation in 8086). The next two machine instructions of the program are:
PUSH AX
PUSH BX
* These first instruction shall push the value stored in AX (16-bit register) to the stack. This is done by subtracting a value of 2 (2 bytes) from SP.
* The new value of SP becomes 0xF81E. The CPU then copies the value of AX to the memory word whose physical address is 0x1F81E.
* When "PUSH BX" is executed, SP is set to 0xF81C and BX is copied to 0x1F81C.
This illustrates how PUSH works. Usually, the running program pushes registers to the stack to make use of the registers for other purposes, like to call a routine that may change the current values of registers. To restore the values stored at the stack, the program shall contain machine instructions like this:
POP BX
POP AX
*
POP BX
copies the word at 0x1F81C (which is the old value of BX) to BX, then increases SP by 2. SP now is 0xF81E.
*
POP AX
copies the word at 0x1F81E to AX, then sets SP to 0xF820.
Stack engine
Simpler processors store the stack pointer in a regular
hardware register and use the
arithmetic logic unit
In computing, an arithmetic logic unit (ALU) is a Combinational logic, combinational digital circuit that performs arithmetic and bitwise operations on integer binary numbers. This is in contrast to a floating-point unit (FPU), which operates on ...
(ALU) to manipulate its value. Typically push and pop are translated into multiple
micro-ops, to separately add/subtract the stack pointer, and perform the load/store in memory.
Newer processors contain a dedicated stack engine to optimize stack operations.
Pentium M was the first x86 processor to introduce a stack engine. In its implementation, the stack pointer is split among two registers: ESP
O, which is a 32-bit register, and ESP
d, an 8-bit delta value that is updated directly by stack operations. PUSH, POP, CALL and RET opcodes operate directly with the ESP
d register. If ESP
d is near overflow or the ESP register is referenced from other instructions (when ESP
d ≠ 0), a synchronisation micro-op is inserted that updates the ESP
O using the ALU and resets ESP
d to 0. This design has remained largely unmodified in later Intel processors, although ESP
O has been expanded to 64 bits.
A stack engine similar to Intel's was also adopted in the
AMD K8 microarchitecture. In
Bulldozer
A bulldozer or dozer (also called a crawler) is a large tractor equipped with a metal #Blade, blade at the front for pushing material (soil, sand, snow, rubble, or rock) during construction work. It travels most commonly on continuous tracks, ...
, the need for synchronization micro-ops was removed, but the internal design of the stack engine is not known.
Notes
References
{{CPU technologies
Control flow
Central processing unit
Digital registers