Skip to main content

POP

Pop the top value from the SP stack and assign it to the specified register.

OpcodeBytesCyclesFormExample
$3023pop b:_pop b:1
$3123pop p:_pop p:2
$3223pop i:_pop i:4
$1a12pop fppop fp
$1b12pop gppop gp
$1c12pop ippop ip
$1d12pop flagspop 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 sp register), whereas the GP stack grows backwards. (The "top" of the GP stack is actually the lowest memory address.)

  • pop undoes the action of push.

  • For example, the effect of pop p:6 is to subtract 2 from the sp register, because p:6 is a pair register. Two bytes are read from the memory address indicated by the new sp and stored in p:6.

  • pop ip undoes the action of push return ip. This pair of instructions is used to call subroutines, for example Hybrix function calls.

  • pop flags pops 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 flags form, the pop instruction 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.