Skip to main content

call/ret

The call instructions push the program counter value of the next instruction onto the stack and set the program counter to the given address. The return instruction pops the program counter value and sets the program counter to it.

Prefixing these instructions with prefix_a16 results in undefined behavior. (The program counter value would be written to or read from IO devices.)

InstructionEncodingSemanticsCycles
call imm1600 001 001 imm16see below7
call pi00 010 001see below5
ret00 011 001see below4

Semantics of call imm16

Let PC\mathit{PC} denote the address of the first byte after this instruction and pc\mathit{pc} denote the 16 bit program counter register. sp\mathit{sp} is the 16 bit stack pointer register.

spsp1;mem[sp]PC[8:15];spsp1;mem[sp]PC[0:7];pcimm16\begin{aligned} \mathit{sp} &\gets \mathit{sp} - 1; \\ \mathit{mem}[\mathit{sp}] &\gets \mathit{PC}[8:15]; \\ \mathit{sp} &\gets \mathit{sp} - 1; \\ \mathit{mem}[\mathit{sp}] &\gets \mathit{PC}[0:7]; \\ \mathit{pc} &\gets \mathit{imm16} \end{aligned}

The microcode of this instruction uses the 16 bit shadow register uv\mathit{uv} internally.

Semantics of call pi

Let PC\mathit{PC} denote the address of the first byte after this instruction and pc\mathit{pc} denote the 16 bit program counter register. sp\mathit{sp} is the 16 bit stack pointer register and pi\mathit{pi} is the 16 bit page-index register.

spsp1;mem[sp]PC[8:15];spsp1;mem[sp]PC[0:7];pcpi\begin{aligned} \mathit{sp} &\gets \mathit{sp} - 1; \\ \mathit{mem}[\mathit{sp}] &\gets \mathit{PC}[8:15]; \\ \mathit{sp} &\gets \mathit{sp} - 1; \\ \mathit{mem}[\mathit{sp}] &\gets \mathit{PC}[0:7]; \\ \mathit{pc} &\gets \mathit{pi} \end{aligned}

Semantics of ret

pc\mathit{pc} denotes the 16 bit program counter register and sp\mathit{sp} the 16 bit stack pointer register.

pc[0:7]mem[sp];spsp+1;pc[8:15]mem[sp];spsp+1\begin{aligned} \mathit{pc}[0:7] &\gets \mathit{mem}[\mathit{sp}]; \\ \mathit{sp} &\gets \mathit{sp} + 1; \\ \mathit{pc}[8:15] &\gets \mathit{mem}[\mathit{sp}]; \\ \mathit{sp} &\gets \mathit{sp} + 1 \end{aligned}