Skip to content

Control flow instructions

Guillaume DERAMCHI edited this page Feb 22, 2024 · 8 revisions

Control flow instructions in the virtual processor's assembly language guide the program's execution flow.

Instruction Syntax Binary Code Usage Error Check
goto goto label1 0000 1000 0101 0110
(Op code: 0 0001)
Unconditionally jumps to the specified label. Confirm label existence.
call call label1 0001 0000 0101 0110
(Op code: 00010)
Calls a subroutine at label1. Verify label existence.
ret ret 1011 0000 0000 0000
(Op code: 1 0110)
Returns from a subroutine. Use only after a call instruction.

Examples

  1. Using goto:
    ; Assume 'mainLoop' is a label defined elsewhere in the code

mainLoop: ; Some code here ; ...

; Jumping to 'mainLoop' goto mainLoop ; Control of the program jumps to the 'mainLoop' label


2. **Calling a subroutine**:
   ```assembly
; Defining a subroutine
computeValue:
    ; Code for the subroutine
    ret  ; Returns to the calling location

; In the main program
call computeValue  ; Calls the subroutine 'computeValue'
; Execution continues here after the subroutine finishes
  1. Returning from a subroutine:
    ; Subroutine definition

computeValue: ; Code for the subroutine ret ; Returns control to the instruction after the 'call computeValue'

; In the main program call computeValue ; Calls 'computeValue' ; Execution resumes here after 'computeValue' completes


Clone this wiki locally