Control Flow
JMP, JSR & RET
Unconditional jumps and subroutine calls.
JMP — Unconditional Jump
JMP BaseR — Sets PC to the value in BaseR. Unlike BR which is limited to ±256 addresses, JMP can go anywhere since it uses a full 16-bit register value.
JSR/JSRR — Jump to Subroutine
JSR LABEL — Saves the return address in R7, then jumps to LABEL.
JSRR BaseR — Saves the return address in R7, then jumps to the address in BaseR.
The key: R7 = PC (the address of the instruction after JSR) before jumping. This is how the subroutine knows where to return.
RET — Return from Subroutine
RET is actually just JMP R7 — it returns to the address saved in R7..ORIG x3000
; Main program
LEA R0, MSG1
PUTS ; Print "Before"
JSR MY_SUB ; Call subroutine (R7 = return addr)
LEA R0, MSG3
PUTS ; Print "After"
HALT
; Subroutine
MY_SUB
LEA R0, MSG2
PUTS ; Print "Inside"
RET ; Return to caller (JMP R7)
MSG1 .STRINGZ "Before "
MSG2 .STRINGZ "Inside "
MSG3 .STRINGZ "After"
.ENDR7 is the link register. JSR overwrites R7 with the return address. If your subroutine calls another subroutine (nested calls), you must save R7 first or the original return address is lost!
Exercise
Write a program with a subroutine called DOUBLE that doubles the value in R0. Main should set R0 = 3, call DOUBLE, then print the result (6) as an ASCII character.
1
R0
x00000
R1
x00000
R2
x00000
R3
x00000
R4
x00000
R5
x00000
R6
x00000
R7
x00000
PCx3000
CC
NZP