Explaining the code: next

Now let’s look at this piece of code:

LD A, *(IP)
INC IP
JMP *(A)

Before we start this sequence, the situation looks like this:

    PC ---> 0x1D: 0x2043    LD A, *(IP)
            0x1E: 0x7003    INC IP
            0x1F: 0x2004    JMP *(A)
             …       …
            0x27: 0x0028    
            0x28: 0xB461   
             …       …
            0x2E: 0x001A    
 IP, A ---> 0x2F: 0x0027
            0x30: 0x8614
             …       …

Now, LD A, *(IP) looks up the value at the memory location register IP points to. IP points to 0x2F, this contains 0x0027, which is loaded into register A, effectively makeing the word starting at address 0x0027 the active word.

            0x1D: 0x2043    LD A, *(IP)
    PC ---> 0x1E: 0x7003    INC IP
            0x1F: 0x2004    JMP *(A)
             …       …
     A ---> 0x27: 0x0028    
            0x28: 0xB461   
             …       …
            0x2E: 0x001A    
    IP ---> 0x2F: 0x0027
            0x30: 0x8614
             …       …

Then INC IP moves the instruction pointer to the next instruction to be executed once we are finished with the active word.

            0x1D: 0x2043    LD A, *(IP)
            0x1E: 0x7003    INC IP
    PC ---> 0x1F: 0x2004    JMP *(A)
             …       …
     A ---> 0x27: 0x0028    
            0x28: 0xB461   
             …       …
            0x2E: 0x001A    
            0x2F: 0x0027
    IP ---> 0x30: 0x8614
             …       …

Finally, JMP *(A) jumps the program counter to the address contained in the memory cell register A points to, which is 0x0028 in this case. (We could have written the instruction as LD PC, *(A) as well.)

The whole sequence is called NEXT in a Forth system.