Call And Return Instructions - Nintendo GAME BOY Programming Manual

Hide thumbs Also See for GAME BOY:
Table of Contents

Advertisement

Game Boy Programming Manual

2.8 Call and Return Instructions

CALL
In memory, pushes the PC value corresponding to the instruction at the address following that of the
CALL instruction to the 2 bytes following the byte specified by the current SP. Operand nn is then loaded
in the PC.
The subroutine is placed after the location specified by the new PC value.
When the subroutine finishes, control is returned to the source program using a return instruction and by
popping the starting address of next instruction, which was just pushed, and moving it to the PC.
With the push, the current value of the SP is decremented by 1, and the higher-order byte of the PC is
loaded in the memory address specified by the new SP value. The value of the SP is then again
decremented by 1, and the lower-order byte of the PC is loaded in the memory address specified by that
value of the SP.
The lower-order byte of the address is placed in byte 2 of the object code, and the higher-order byte is
placed in byte 3.
Examples: When PC = 8000h and SP = FFFEh,
Address
8000h
CALL 1234H
8003h
CALL
If condition cc matches the flag, the PC value corresponding to the instruction following the CALL
instruction in memory is pushed to the 2 bytes following the memory byte specified by the SP. Operand
nn is then loaded in the PC.
Examples: When Z = 1,
Address
7FFCh
CALL NZ, 1234h
8000h
CALL
Z, 1234h
8003h
(SP - 1) ← PC
nn
(SP - 2) ← PC
PC ← nn
SP ← SP-2
;
Jumps to address 1234h, and
(FFFDH) ← 80H
(FFFCH) ← 03H
SP ← FFFCH
cc,
nn
If cc true,
(SP - 1) ← PC
(SP - 2) ← PC
PC ← nn
SP ← SP – 2
;
Moves to next instruction after 3 cycles.
;
Pushes 8003h to the stack,
and jumps to 1234h.
CY
H
--
--
H
L
CY
H
--
--
H
L
118
N
Z
CYCL
7 6
--
--
6
11
L - ADRS
H - ADRS
N
Z
CYCL
7 6
--
--
6/3
11
L-ADRS
H-ADRS
5 4 3
2 1 0
001
101
n
n
5 4 3
2 1 0
Occ
100
n
n

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents