The Stack
The stack exists in RAM and is used as a level of memory one step larger (and therefore one step slower) than CPU registers. It's used for storing data when handling multi-tasking behaviour. It is therefore the natural domain of the OS software kernel which handles multi-tasking behaviourin modern computing devices.
Importantly, the stack must exhibit stack-like behaviour. In other words it must follow the principle of Last In First Out (LIFO). As an MtG player you know all about this.
Thread Switching
Suppose we are writing code for a task in an embedded system that uses the following frame layout:
Program Counter
Keeps track of position in program memory
Link Register
Stores the return vector after the current function completes
R12
Register (usually some special CPU function)
R3
Register
R2
Register
R1
Register
R0
Register
If an interrupt arrives and we need to switch to a different context we first need to save the current context by pushing the given values in the given order. Then when the interrupt service routine (ISR) completes we return to the original context by popping these saved values back to the CPU in reverse order.
Last updated
Was this helpful?