Push

What happens when we push a value to the stack?

What we want to do is something like in the following example. First we push the number 3, and then the number 5 onto the stack:

                         ┌───┐
  PUSH 3         PUSH 5  │ 5 │    
          ┌───┐          ├───┤    
          │ 3 │          │ 3 │    
──────────┴───┴──────────┴───┴──

In our fantasy machine, we start with the situation like this:

            BP: 0x0039
            IX: 0x0000
            A:  0x0003

            0x38: ?
BP[IX] ---> 0x39: ?
            0x3A: ?
            0x3B: ?

Now, we execute the command PUSH BP[IX], A. The value in register A (3) is taken and put into memory cell BP (0x39) + IX (0x00). Then the machine automatically increases the value in register IX by 1. So we end up like this:

            BP: 0x0039
            IX: 0x0001
            A:  0x0003

            0x38: ?
            0x39: 0x0003
BP[IX] ---> 0x3A: ?
            0x3B: ?

Now, let’s load a new value into register A with LDI A, 5, so we have:

            BP: 0x0039
            IX: 0x0001
            A:  0x0005

            0x38: ?
            0x39: 0x0003
BP[IX] ---> 0x3A: ?
            0x3B: ?

Again, we execute the command PUSH BP[IX], A. The value in register A (now 5) is taken and put into memory cell BP (0x39) + IX (0x01). Then the machine automatically increases the value in register IX by 1. So we end up like this:

            BP: 0x0039
            IX: 0x0002
            A:  0x0005

            0x38: ?
            0x39: 0x0003
            0x3A: 0x0005
BP[IX] ---> 0x3B: ?

Note, BP[IX] always points to the next position something is written into. What happens if there is already something written in this memory cell? It will be overwritten. Our machine itself does not have any guards that prevent this.