In the last lesson, you learned how to jump to an absolute address using JP. This time, you’ll explore a different instruction: Jump Relative (JR).
JR is especially useful when writing loops and makes your programs far easier to read when combined with labels
JR tells the CPU to jump a short distance forward or backward in memory relative to where it is now.
Key points:
If you’re repeating something small, JR is usually your best choice.
ORG 256
LD A, 83 ; Start with ASCII 'S'
loop:
ADD A, 1 ; Keep increasing A
JR loop ; Jump back to the 'loop' label
This results in an infinite loop. You’ll need to reset the emulator to stop it
Previously, you might have written:
JP 262
That works, but if your program changes, the addresses change too, and you must recalculate them manually.
Using labels like this:
loop:
JR loop
means the assembler automatically calculates the jump distance. This makes your code more flexible, readable, and error-proof. Thank goodness!
Write a program that: