📓
Documents
  • Directory
  • Vancouver Cafe Database
  • Latex Resources
  • Ada Links
  • On Interviewing
  • Electrical
    • WIP - Mapping the Territory
    • Gain and Phase Margin
    • Piezoelectrics
    • Common ICs
    • WIP - PCB Design
    • WIP - High frequency circuits
      • Transmission Line Theory
      • Propogation
  • Computer Science
    • Statics, Volatiles, and Externs
    • Linked Lists
    • Dynamic Memory Allocation
    • The Stack
    • WIP - Investigations into Embedded
  • Mathematics
    • Markov Chains
      • Properties of Markov Chains
      • Cayley-Hamilton and Matrix Similarity
  • Ongoing Projects
    • Master List
Powered by GitBook
On this page

Was this helpful?

  1. Computer Science

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.

Currently I am learning about RTOS which constantly multi-tasks in order to achieve time sensitive system objectives. Such systems use multiple stacks for saving system contexts for different "tasks" or "threads".

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.

Note that the Program Counter is first and last out, this is to avoid accidentally incrementing and loading program instructions before the original context is fully restored.

PreviousDynamic Memory AllocationNextWIP - Investigations into Embedded

Last updated 5 years ago

Was this helpful?