POP
Pop the top value from the SP stack and assign it to the specified register.
| Opcode | Bytes | Cycles | Form | Example |
|---|---|---|---|---|
| $30 | 2 | 3 | pop b:_ | pop b:1 |
| $31 | 2 | 3 | pop p:_ | pop p:2 |
| $32 | 2 | 3 | pop i:_ | pop i:4 |
| $1a | 1 | 2 | pop fp | pop fp |
| $1b | 1 | 2 | pop gp | pop gp |
| $1c | 1 | 2 | pop ip | pop ip |
| $1d | 1 | 2 | pop flags | pop flags |
Notes
-
The SP stack is commonly used to store local variables and return addresses for function calls.
-
The SP stack grows forwards (pushing increases the
spregister), whereas the GP stack grows backwards. (The "top" of the GP stack is actually the lowest memory address.) -
popundoes the action ofpush. -
For example, the effect of
pop p:6is to subtract 2 from thespregister, becausep:6is apairregister. Two bytes are read from the memory address indicated by the newspand stored inp:6. -
pop ipundoes the action ofpush return ip. This pair of instructions is used to call subroutines, for example Hybrix function calls. -
pop flagspops the CPU flags as a single byte, ignoring SF and WF (to protect against unintended effects on the next instruction) as well as any unimplemented bits. -
Aside from the
pop flagsform, thepopinstruction does not affect the CPU condition flags.
With context
If pop ip is preceded by with context, then WF=1 causes the flags and stack pointer to be restored as well:
- IP is read from SP-4 as usual
- The CPU flags register is read from SP-5
- The new SP is read from SP-9
Unlike pop flags, all flags are restored including SF and WF. Although SP is changed, no io::stack_guard check occurs. Besides the 1 cycle for the with context instruction, the timing is the same as regular pop ip.
This feature enables the full CPU state to be restored. For example, suppose we want to reset all registers to zero and jump to $c0_0000, simulating a hard reset:
push int 0 # prepare sp
push byte 0 # prepare flags
push int $c0_0000 # prepare ip
push int 0
pop gp # restore gp
move fp, $00_0000 # restore fp
with context
pop ip # pop ip, flags, sp (then jump)
WF=1 does not alter the behavior of any POP forms besides pop ip.