In relation to computers, a stack register is a memory location — usually on the central processing unit (CPU) or related processing hardware — that holds the current address of the top of a region of separate computer memory known as the stack. The stack register is important because, without it, a computer would need to implement a slower, more error-prone method of tracing the flow of execution of a program. In most system architectures, the stack register is a dedicated register so it is not accidentally accessed when working with other memory registers. More rarely, a stack register can be a general register that usually is accessible by a program but that intentionally is not used because its use is defined by the manufacturer. When a computer system contains two or more stack registers, meaning there is potentially more than one stack, the architecture is known as a stack machine.
At the lowest level of computer programming, a stack is an area of memory — usually in random access memory (RAM) — that has a well-defined type of behavior. The stack can have information added to it in a process called pushing, or it can have information retrieved from it, which is called popping. The model for a stack is first-in, last-out, meaning that if several pieces of information are pushed into the stack, then the first element pushed in will be the last one to be popped out, while the last element pushed in will be the first one to be retrieved with a pop command. A stack register keeps track of the top of the stack, which is always the last item pushed into it.
When a computer program is executing, each instruction that is being executed has a specific memory address where it is temporarily stored for the duration of the program. If a program calls a subroutine — or a procedure, function or method, depending on the programming language — then the program must jump to the memory address of the subroutine code to execute it. The address where the program control flow breaks to branch to the subroutine is pushed onto the stack so it is remembered. When the subroutine has completed executing, the program knows to where it should return in the main code by popping the code address from the top of the stack, where the stack register is pointing.
Although there are other methods that can be used to achieve the same results, using a stack and stack register allows for an important programming concept known as recursion. A recursive function is a function that, within its own code, calls itself. This process commonly is used in sorting algorithms and for certain mathematical functions. The stack register is keeping track of all the last addresses where the execution is branching, so a function can safely implement recursion with the knowledge that, eventually, control will return to the point of origin. One complication occurs if the entire stack becomes full and no room in memory remains, in which case a stack overflow occurs, ceasing execution of the program.