Lesson 6
 TI-82 ASM Corner :: Programming Guide :: Lesson 6 Back | Home | Search
Lesson 6 : Jumps and calls

This lesson will teach you how you can realize absolute and relative jumps and calls.

Jumps | Calls

Jumps

As you have seen in previous lessons, it is possible to give a name to a certain line of your code. This is called a label. An example:

 ``` LD (\$800C),A MyLabel: INC A DEC A ```

In this example, the label mylabel points to the instruction immediately behind it, and this is the INC A instruction. Even more, it actually points to the first byte of the instruction that comes immediately behind it. But since the INC A instruction takes only 1 byte, we don't have to worry about that. And even if the instuction immediately behind the label took more than 1 byte, we would normally not have to worry about that.

As you probably already figured out, we don't place the labels there 'just for fun'. We will do something with them. So what can we do with them?

Well, we can jump to that line using the jp and jr instructions. The Z-80 processor will change the PC (program counter - the register that holds the absolute address of the next instruction that needs to be executed) so it points to the address provided as an operand of the JP or JR instruction. More details on the difference between JR and JP will follow later in this lesson. Please note that it's not only possible to jump forward, you can also jump backwards!
An example:

 ``` LD A,\$05 ; 1 LD A,\$10 ; 2 JP This_Is_A_Label ; 3 LD A,\$20 ; 4 LD B,\$03 ; 5 This_Is_A_Label: ; 6 LD B,\$1A ; 7 ```

Try to figure out what the value of the A and B registers will be at the end of this snipplet of code. A will be \$10 and B will be \$1A. When this code is executed, the jump on line 3 will make the program counter point to line 7, so the instructions at lines 4 and 5 will never be executed.

Now what's the difference between JP and JR ?

Well JP is an absolute jump and JR is a relative jump. The difference between both is that a relative jump can only jump 128 bytes backwards and 127 forward while the absolute jump (JP) can jump to whatever memory location you desire. On the TI-82 this can be from \$0000 to \$FFFF.

A question you could ask yourself is: why would you need JR when you can jump anywhere with JP ?

The answer is simple: the JR instruction takes only 2 bytes, where a JP instruction takes 3 (that's in normal circumstances). There is also a difference in execution speed between these two.

To be complete: there are also conditional jumps possible with JP and JR (this means that the jump is only executed if a certain condition has been met). More about these conditional jumps in the lesson 10

Calls

When you want to make subroutines that you can call from inside your program, you'll need the CALL and RET instructions. What a call does is the following: a jump is made to the address that was given as operand. So far, this looks like a normal jump. But this time, the address of the instruction that comes immediately after the CALL instruction is stored somewhere in the memory (on the stack - see advanced details further in this lesson). Now, when you are in your subroutine and use RET (return), the processor gets the address back from the memory and jumps to it.

Like with the jumps, you can also use conditional calls and conditional returns. More about that in the lesson 10 and the lesson 11 (handling of keypresses)

Example:

 ``` ... ; main program ... call subroutine ... ret ; end of program subroutine: ... ; code ... ret ```

Below is some more advanced data on how a call is done, for the people who are interested. You might want to take a look at lesson 12 (the part about the stack) first.

 ``` ld hl,\$8D14 call Routine pop hl ret Routine: push hl ret ```