Z80 Assembly - Instructions: Branching

Jump instructions

These are the instructions that change the place of execution without saving their own address for a possible return.

JP and JR

Syntax: jp/jr label or (hl)/(ix)/(iy) (unconditional) or jp/jr condition,label (conditional)

When arriving at any of these intructions, execution is immediately continued from the location denoted by the label given (you can use numbers instead of labels of course). If the operand is a register reference (e. g. jp (hl)), it means that the value of the register will be loaded into PC directly; these jumps can be unconditional only. Otherwise, if the condition is not fulfilled, execution continues as if there wasn’t any jump. The flags are preserved in all the cases. The conditions can be the following:

conditionmeaning
ccarry (C) is set
nccarry is not set
zzero (Z) is set
nzzero is not set
msign (S) is set
psign is not set
peparity/overflow (P/V) is set
poparity/overflow is not set

The most important difference between the two jumps is that jr calculates the jump address with respect to itself, and the destination must be in its 128-byte vicinity. Moreover, jr can only take the first four conditions of those listed above. Besides all these differences, jr is slower but also smaller than the corresponding jp, therefore you have to decide whether speed or size is more important in the piece of code you place the jump into.

DJNZ

Syntax: djnz label

B is decreased, and a jr label happens if the result was zero. The flags are preserved. Since this is a relative jump, it can only point to its 128-byte vicinity.

Subroutine instructions

The two instructions of this section are also jumps, but by taking advantage of the stack, they offer the possibility to create subroutines.

CALL

Syntax: call label (unconditional) or call condition,label (conditional)

The address of the instruction immediately following the call (i. e. PC+3) is saved to the stack, and execution is continued from the address given by the label. The conditions are the same as those of the absolute jump, i. e. all the eight conditions can be used. The flags are preserved.

RET

Syntax: ret (unconditional) or ret condition (conditional)

The word on the top of the stack is retrieved and it is used as the address of the next instruction from which the execution is to be continued. This is basically a pop pc. The conditions work the same way as above, all of them can be used. The flags are preserved.

Back to the index