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-only. ...
whose purpose is to keep track of a
call stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mach ...
. On an
accumulator-based architecture
In a computer's central processing unit (CPU), the accumulator is a register in which intermediate arithmetic logic unit results are stored.
Without a register like an accumulator, it would be necessary to write the result of each calculation ( ...
machine, this may be a dedicated register. On a machine with mulitple
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 that was announced by IBM on April 7, 1964, and delivered between 1965 and 1978. It was the first family of computers designed to cover both commercial and scientific applic ...
through
z/Architecture architecture and
RISC
In computer engineering, a reduced instruction set computer (RISC) is a computer designed to simplify the individual instructions given to the computer to accomplish tasks. Compared to the instructions given to a complex instruction set compu ...
architectures, or it may be a register that procedure call and return instructions are hardwired to use, such as on the
PDP-11
The PDP-11 is a series of 16-bit minicomputers sold by Digital Equipment Corporation (DEC) from 1970 into the 1990s, one of a set of products in the Programmed Data Processor (PDP) series. In total, around 600,000 PDP-11s of all models were so ...
,
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
The PDP-8 is a 12-bit minicomputer that was produced by Digital Equipment Corporation (DEC). It was the first commercially successful minicomputer, with over 50,000 units being sold over the model's lifetime. Its basic design follows the pioneeri ...
and
HP 2100—did not have compilers which supported
recursion
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematic ...
. 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 has 2 or more stack registers — one of them keeps track of a
call stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mach ...
, the other(s) keep track of other
stack
Stack may refer to:
Places
* Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group
* Blue Stack Mountains, in Co. Donegal, Ireland
People
* Stack (surname) (including a list of people ...
(s).
Stack registers in x86
In
8086
The 8086 (also called iAPX 86) is a 16-bit microprocessor chip designed by Intel between early 1976 and June 8, 1978, when it was released. The Intel 8088, released July 1, 1979, is a slightly modified chip with an external 8-bit data bus (allo ...
, the main stack register is called stack pointer - SP. 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 data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mach ...
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 push a value to the stack, the
PUSH
instruction is used. To pop 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
In digital electronics, especially computing, hardware registers are circuits typically composed of flip flops, often with many characteristics similar to memory, such as:
* The ability to read or write multiple bits at a time, and
* Using an a ...
and use the
arithmetic logic unit
In computing, an arithmetic logic unit (ALU) is a 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 floating point numb ...
(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
The Pentium M is a family of mobile 32-bit single-core x86 microprocessors (with the modified Intel P6 (microarchitecture), P6 microarchitecture) introduced in March 2003 and forming a part of the Intel Centrino#Carmel platform (2003), Carmel no ...
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, motorized machine equipped with a metal blade to the front for pushing material: soil, sand, snow, rubble, or rock during construction work. It travels most commonly on continuous trac ...
, 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