Site Logo

Stack Instructions

Website

Home | Previous | Next

Stack Instructions - Flags are NOT set.

After pushing items onto the stack, always pop them off in reverse order. This is because the stack works by the Last In First Out (LIFO) rule. The stack is an area of RAM used in this particular way. Any part of RAM could be used. In the simulator, the stack is located just below the Video RAM at address [BF]. The stack grows towards zero. It is easily possible to implement a stack that grows the other way.

Stack Examples

Assembler Machine Code Explanation
PUSH BL E0 01 Push BL onto the stack and subtract one from the stack pointer.

E0 is the machine instruction for PUSH.
01 refers to the BL register.
POP BL E1 01 Add one to the stack pointer and pop BL from the stack.

E1 is the machine instruction for POP.
01 refers to the BL register.
PUSHF EA Save the CPU status register (SR) onto the stack. This saves the CPU flags.
POPF EB Restore the CPU status register (SR) from the stack. This restores the CPU flags.

 

The stack is used to ...

Stack Pointer

A CPU register (SP) that keeps track of (is a pointer to) the data on the stack. It is colour coded with a blue highlight in the simulator RAM display.

Push and Pop

Push - Add data to the stack at the stack pointer position and subtract one from the stack pointer.
Pop - Add one to the stack pointer and remove data from the stack at the stack pointer position.

LIFO

Last in First out. The stack operates strictly to this rule. When data is pushed onto the stack, it must later be popped in reverse order.

Stack Overflow

The stack is repeatedly pushed until it is full. The simulator does not detect this condition and the stack can overwite program code or data. Real life programs can fail in the same way.

Stack Underflow

The stack is repeatedly popped until it is empty. The next pop causes an underflow.

Home | Previous | Next

© C Neil Bauers 2003