This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

Program Flow

Jump, Call and Return

Table of Contents

1 - Jump absolute

76543210
 
\(PC \longleftarrow nn\)
JP nn
11000011C3
7nn0
158
 
\(\begin{rcases} PC \longleftarrow nn \end{rcases} \text {if } ccc = true\)
11ccc010
7nn0
158
 
\(PC \longleftarrow HL\)
JP (HL)
11101001E9
 
\(PC \longleftarrow IX\)
JP (IX)
11011101DD
11101001E9
 
\(PC \longleftarrow IY\)
JP (IY)
11111101FD
11101001E9
Conditions
cccAbbrev Condition Flag
000NZ Non Zero Z
001Z Zero
010NC No Carry C
011C Carry
100 PO Parity Odd P/V
101 PE Parity Even
110 P Sign Positive S
111 M Sign Negative

Jumps to 16bit registers

Although the instruction JP (HL) looks like it's using indirect addressing, it doesn't. It takes the address in HL as the new PC, so it should be read as if it's JP HL.

The same applies for JP (IX) and JP (IY) - the actual register is used not the value at that address.

Flags Affected
None.
Opcode Matrix
UncondCNCZNZPEPONP
JP nn
JP nn
C3nnnn310
JP C,nn
DAnnnn310
JP NC,nn
D2nnnn310
JP Z,nn
CAnnnn310
JP NZ,nn
C2nnnn310
JP PE,nn
EAnnnn310
JP PO,nn
E2nnnn310
JP N,nn
FAnnnn310
JP P,nn
F2nnnn310
JP (HL)
JP (HL)
E914








JP (IX)
JP (IX)
DDE928








JP (IY)
JP (IY)
FDE928








Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Flow

2 - Jump Relative

76543210
 
\(PC \longleftarrow PC + e\)
JR e
0001100018
e-2
 
\(\begin{rcases} PC \longleftarrow (PC) + e \end{rcases} \text {if } cc = true\)
JR cc, e
001cc000
e-2
 
\(B \longleftarrow B - 1\\ \begin{rcases} PC \longleftarrow PC + e \end{rcases} \text{ if } B \not = 0\)
DJNZ e
0001000010
e-2
Conditions
ccAbbrev Condition Flag
00NZ Non Zero Z
01Z Zero
10NC No Carry C
11C Carry

Relative Jumps

For relative instructions the offset is taken from the address of the op code so is in the range -126 to 129. Assemblers usually account for the difference where the value in memory is e-2.

Timing

For JR then when a jump takes place then it takes 12(4,3,5) T-States whilst no jump 7(4,3) T-States.

For DJNZ if the jump takes place then it takes 13 (5,3,5) T-States. If no jump then 8 (5,3) T-States.

Flags Affected
None.
Opcode Matrix
UncondCNCZNZB!=0
JR e
JR e
18nn212
JR C,e
38nn212
JR NC,e
30nn212
JR Z,e
28nn212
JR NZ,e
20nn212

DJNZ e





DJNZ e
10nn213
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Flow

3 - Call subroutine

76543210
 
\((SP-1) \longleftarrow PC_h\\ (SP-2) \longleftarrow PC_l\\ SP \longleftarrow SP-2\\ PC \longleftarrow nn\)
CALL nn
11001101CD
7nn0
158
 
\(\begin{rcases} (SP-1) \longleftarrow PC_h\\ (SP-2) \longleftarrow PC_l\\ SP \longleftarrow SP-2\\ PC \longleftarrow nn \end{rcases} \text{ if } ccc = true\)
CALL ccc, nn
11ccc100CD
7nn0
158
Conditions
cccAbbrev Condition Flag
000NZ Non Zero Z
001Z Zero
010NC No Carry C
011C Carry
100 PO Parity Odd P/V
101 PE Parity Even
110 P Sign Positive S
111 M Sign Negative

Timing

All call operation's take 17 (4,3,4,3,3) T-States, except for the conditional ones when the condition has not been met. In those instances it takes 10(4,3,3) T-States.

Flags Affected
None.
Opcode Matrix
UncondCNCZNZPEPONP
CALL nn
CALL nn
CDnnnn317
CALL C,nn
DCnnnn317
CALL NC,nn
D4nnnn317
CALL Z,nn
CCnnnn317
CALL NZ,nn
C4nnnn317
CALL PE,nn
ECnnnn317
CALL PO,nn
E4nnnn317
CALL N,nn
FCnnnn317
CALL P,nn
F4nnnn317
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Flow

4 - Return from Subroutine

76543210
 
\(PC_l \longleftarrow (SP) \\ PC_h \longleftarrow (SP+1) \\ SP \longleftarrow SP+2\)
RET
11001001
 
\(\begin{rcases} PC_l \longleftarrow (SP) \\ PC_h \longleftarrow (SP+1) \\ SP \longleftarrow SP+2 \end{rcases} \text{ if } ccc = true\)
RET ccc
11ccc000
Conditions
cccAbbrev Condition Flag
000NZ Non Zero Z
001Z Zero
010NC No Carry C
011C Carry
100 PO Parity Odd P/V
101 PE Parity Even
110 P Sign Positive S
111 M Sign Negative

Timing

The unconditional RET takes 10 (4,3,3) T-States. The conditional RET takes 17(5,3,3) T-States if the condition is true and 5 T-States if false and no return was performed.

Flags Affected
None.
Opcode Matrix
UncondCNCZNZPEPONP
RET
RET
C9110
RET C
D8111
RET NC
D0111
RET Z
C8111
RET NZ
C0111
RET PE
E8111
RET PO
E0111
RET N
F8111
RET P
F0111
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Flow

5 - RST

Invoke a Reset

RST performs a reset. Specifically it calls a routine at one of 8 addresses at the base of memory. It is the equivalent of performing a CALL to that address except the RST instruction is just 1 byte compared to 3 for CALL and is slightly faster.

\((SP-1) \longleftarrow PC_h \\(SP-2) \longleftarrow PC_l \\SP \longleftarrow SP-2 \\PC_h \longleftarrow 0\\PC_l \longleftarrow b*8\)

76543210
11b111
Bits
Valueb
0000
1001
2010
3011
4100
5101
6110
7111

Issues with RST instructions

Assemblers use different conventions for the RST instruction. Some use numbers 0…7 whilst others use the address of the code invoked. They are all equivalent, as there are just 8 possible instruction codes.

Address OP Code RST Instruction Action
0000 C7 RST 0 Reset machine
0008 CF RST 1 RST 8 Operating System Specific
0010 D7 RST 2 RST $10 RST 16
0018 DF RST 3 RST $18 RST 24
0020 E7 RST 4 RST $20 RST 32
0028 EF RST 5 RST $28 RST 40
0030 F7 RST 6 RST $30 RST 48
0038 FF RST 7 RST $38 RST 56 Interrupt Handler in Mode 1
Flags Affected
None.
Opcode Matrix
Reset routine
01234567
RST
RST 0
C7111
RST 1
CF111
RST 2
D7111
RST 3
DF111
RST 4
E7111
RST 5
EF111
RST 6
F7111
RST 7
FF111
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Special

6 - Return from Interrupt

76543210
 
\(PC_l \longleftarrow (SP) \\ PC_h \longleftarrow (SP+1) \\ SP \longleftarrow SP+2\)
RETI
11101101ED
010011014D
 
\(PC_l \longleftarrow (SP) \\ PC_h \longleftarrow (SP+1) \\ SP \longleftarrow SP+2 \\ IFF_1 \longleftarrow IFF_2\)
RETN
11101101ED
0100010145
Flags Affected
None.
Opcode Matrix
RETIRETN
Op
RETI
ED4D214
RETN
ED45214
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Interrupt