Pop
What happens when we pop a value from the stack?
Starting out with a stack that contains 3 and 5, we first pop the value on the top (5) and then the next value (3) and then we are done.
┌───┐
│ 5 │ POP POP
├───┤ ┌───┐
│ 3 │ │ 3 │
─┴───┴───────┴───┴ 5 ─────── 3 ──
In our fantasy machine, it starts out like this:
BP: 0x0039
IX: 0x0002
A: 0x0005
0x38: ?
0x39: 0x0003
0x3A: 0x0005
BP[IX] ---> 0x3B: ?
Now, we execute the command POP A, BP[IX]
. The value on the top of the stack (5) – which is in the memory cell before where BP[IX] points to – is copied into register A
. The index register IX
is decremented and we get to the following situation:
BP: 0x0039
IX: 0x0001
A: 0x0005
0x38: ?
0x39: 0x0003
BP[IX] ---> 0x3A: 0x0005
0x3B: ?
Doing a POP A, BP[IX]
again gets us here:
BP: 0x0039
IX: 0x0000
A: 0x0003
0x38: ?
BP[IX] ---> 0x39: 0x0003
0x3A: 0x0005
0x3B: ?
What happens if we do another POP, now that IX
is 0? That depends on the actual implementation of the machine, best is to make sure in your program that this can’t happen.