-
Notifications
You must be signed in to change notification settings - Fork 50
Subroutines
A subroutine is a sequence of instructions that is modular and can be executed multiple times. If you are familiar with any programming languages or dealt with functions in mathematics, you may see that subroutines are similar to functions.
MARIE provides a way to call these subroutines by using the JnS instruction, and normal program execution can be resumed once the subroutine exits by using the JumpI instruction.
Here is an example that prints the variable X, then halts the program:
/ Enter subroutine PrintVariableX
JnS PrintVariableX
Halt
PrintVariableX, HEX 000 / Used for storing return address
Load X
Output
/ Exit subroutine PrintVariableX
JumpI PrintVariableX
X, DEC 42
The JnS instruction stores the address of the next instruction after it was called. In this case, the memory address points to the Halt instruction. This is the return address which will be later used to restore program execution. Once it has done that, it then jumps to the instruction immediately below the label PrintVariableX, by using the memory address of PrintVariableX and incrementing that value once.
Once the subroutine performs its intended task, and runs the JumpI instruction, it loads the memory address stored at the PrintVariableX label, and stores it into the PC register. Note that we don't need to increment the PC register here as it is already taken care of in the fetch part of the fetch-decode-execute cycle before it entered the subroutine. Program execution is resumed from where it was, and the program halts as the Halt instruction is executed.
Copyright © 2018 Jason Nguyen, Saurabh Joshi, Eric Jiang, Felix Salim, Guido Tack, Monash University
Documentation
MARIE Instructions Set with Opcode
Register Transfer Language (RTL)
More Reading
The Essentials of Computer Organization and Architecture-Chapter 4.2
Tutorials
MARIE.js Documentation