DYSTOPIANPIXEL

CALL and RET

Trace a call, its saved return address and the return to Odin.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 2 / PART 11

CALL and RET as a pair

CALL routine tells the CPU to continue at another address and remembers where it should return. RET retrieves the saved return address. Together they let us write a routine once and use it from many places.

Here is our first complete subroutine experiment:

start:
    ld a,$05
    call add_three
    ld hl,$6000
    ret

add_three:
    add a,$03
    ret

Follow the sequence rather than reading the source only from top to bottom. Odin starts the routine at start. LD A,$05 makes A five. CALL add_three stores the address of the next main-programme instruction, then begins add_three. The subroutine adds three, giving A = $08. Its RET returns to the LD HL,$6000 instruction in the main programme. The second RET, at the end of the main programme, returns to Odin.

Expected: A = $08, HL = $6000. The stack should be back to the condition expected by Odin once the routine has returned, because the internal CALL and RET match.

See the return point in machine code

With a $6000 starting address, these particular Z80 instructions have the following layout:

$6000ld a,$053E 05Two-byte instruction
$6002call add_threeCD 09 60The subroutine is at $6009
$6005ld hl,$600021 00 60This is where the subroutine must return
$6008retC9Return from the entry routine to Odin
$6009add a,$03C6 03Subroutine body
$600BretC9Return to $6005

The machine-code bytes for CALL contain the destination address $6009, little-endian as 09 60. The return address $6005 is not another operand inside CALL; the CPU calculates and pushes that address onto the stack when executing the call. That distinction is one of the important ideas in this chapter.

Try this on paper: change the initial A value to $10. Without touching the subroutine, what should A contain after the call? Answer: $13.

Walk through PC and SP, one instruction at a time

We can now see exactly how the simple add_three example reaches its subroutine and returns. Assume the programme begins at $6000. We call the Stack Pointer value S because Odin supplies the real value; it must not be replaced with an invented SP address.

1At $6000: LD A,$05SA becomes $05.
2At $6002: CALL $6009S → S−2Return address $6005 is pushed onto the stack.
3At $6009: ADD A,$03S−2A becomes $08; flags updated.
4At $600B: RETS−2 → SPC is restored to $6005.
5At $6005: LD HL,$6000SHL becomes $6000.
6At $6008: RETS → S+2Odin’s saved return address is consumed.

The fifth step is not an extra CALL. When the subroutine ends, its RET returns to the instruction after CALL, not to the top of start. The final RET is different: it returns out of our teaching programme to Odin’s own calling context.

StageStack and programme counter
Before CALLSP = S; Odin’s return address is already stacked.
After CALLSP = S − 2; [SP] = $05, [SP+1] = $60; PC = $6009.
After helper RETPC = $6005; SP = S; Odin’s original return address remains intact.

The two bytes $05 and $60 are the little-endian representation of the 16-bit return address $6005. They are not stored in the assembler’s CALL instruction: the CPU writes them onto the live stack when CALL executes. The diagram deliberately avoids choosing a real RAM location for the stack.