MC Programming #9: Jumping Around

Author(s): Jonathan Bobst
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n10 (Volume 3, Issue 10)
Date: October 1982
Pages: 19

Most of the machine code subroutines held in REM will be be short around 100 instructions and data. Consequently, you won’t need the “JP nn” instruction.

The Jump Relatives you use instead are harder to use correctly because of calculation-error for the offset (“e” as in JR e). But using JR makes the code relocatable in memory. JP NN specifies an absolute address, which changes if you store the program in a new zero place. JR e works anywhere because the location of the instruction remains unchanged when you relocate the program. JP nn is simple; control transfers to the address “nn” (n1+(n2*256)). Conditional JPs are available and are 3 bytes long.

JR e and conditionals are 2 bytes long, but the 2nd byte is added to the Program Counter +2 to transfer control to an unspecified relative address. Pointing “e” value to the right address is hard.

JR FOWARD
ADDRESS DECIMAL=MNEMONIC COMMENT
x02      40      JR Z    ;x45-x02
                         ;=43
x03      41      e       ;43-2=41=e

x04                      ;To test
                         ;"e",
x45                      ;x02+43=x45

  JR BACKWARD
x059                     :x126-x059=
                         ;67
                         ;255-67=188
                         ;=e
x124                     ;To test
                         ;"e",
x125     56      JR C    ;255-188=67

x126     188     e       ;x126-67=
                         ;x059

JRs are limited to 129 foward addresses and 126 backward addresses, enough leeway for most MC subroutines held in REM lines.