Relative Jump Instructions
The Z80 instruction set includes two types of relative Jump instructions: unconditional and conditional. The new address to which the program sequence is redirected is specified by an 8-bit offset (displacement) value relative to the Jump instruction. The displacement can be positive (forward jump), specified by the seven bits D6-D0 (the MSB D7 = 0), or negative (backward jump) specified in 2’s complement. The total offset values range from -126 to + 129 bytes. (explained in Example). The list of relative Jump instructions is (d = displacement).
Mnemonics |
Bytes |
Description |
JR d |
2 |
;Jump relative unconditionally |
JR Z, d |
2 |
;Jump relative if Z = 1 |
JR NZ, d |
2 |
;Jump relative if Z =0 |
JR C, d |
2 |
;Jump relative if CY =1 |
JR NC, d |
2 |
;Jump relative if CY =0 |
Note: There are no relative Jump instructions based on Sign and Parity flags. |
General Characteristics
1. These are 2-byte instructions; therefore, they are more efficient than 3-byte absolute jump instructions in terms of memory space and, in some situations, execution time.
2. Relative jumps are limited to 256 memory locations.
3. No flags are affected by these instructions.
Example
The unconditional relative Jump instruction is stored in memory locations 2100 and 2101H, as shown below. Find the memory address of the forward jump location if the displacement byte is 7FH. and find the memory address for the backward jump if the displacement byte is 9CH.
Solution
1. When the jump instruction is executed, the program counter (PC) contains the .address 2102(PC always points to the next machine code to be fetched). By adding the displacement byte to the program counter, the address of the jump location becomes 2181H (2102H + 7FH).
For an 8-bit displacement byte, 7FH is the largest offset value for a forward jump. Therefore, relative to the memory location of the first code of the Jump instruction, the maximum displacement is 7FH plus two memory locations of the instruction. The decimal equivalent of 81H (7F + 2) is 129; thus, the positive range extends to 129 memory locations.
2. If the displacement byte is 9CH, it is in 2’s complement because D7 = l. The memory address for the backward jump location from 2l02H (contents of the program counter) can be calculated two ways: (a) by adding the negative displacement 9CH or (b) by subtracting the positive displacement 64H, which is the 2’s c9mplement of 9CH. ·
(a) |
(b) |
||||
Program Counter: |
21 02 |
2 l |
02 |
||
Displacement Byte: |
+ 9C |
– |
6 4 2’s Complement of 9CH |
||
In 2’s Complement: |
9E |
20 9E |
|||
Complement CY and: |
1 |
9E |
|||
Subtract from 21": |
20 9E
The memory address of the Jump location is 209EH. In the first calculation, after adding a negative number, the sum is 9EH without a carry. Therefore, in 2’s complement addition, the ninth bit must be set to l, indicating a borrow that is subtracted from 21H. If you are unfamiliar with 2’s complement arithmetic, review section B.2 in Appendix B. In the second calculation, 64H is a positive number obtained by taking 2’s complement of the negative number, and it is subtracted from 2102H.