Say we need to add 3 to R0 in two places in a program. Let’s write those instructions just once, in slot 4, and go there with JUMP. When it’s done, it has to go back to slot 2 the first time and slot 3 the second time.
Runs from address 0.
Press Next. The returning JUMP 2 has a single fixed address, so it goes back
to slot 2 the second time too, and loops without ever reaching HALT.
JUMP can’t come back once it goes, so we end up writing the same instructions again everywhere we use them.
We just need to write down the return address somewhere before going. On this machine, let’s write it in R3.
CALL Addr: write the return address (the next address) into R3 and jump to
AddrRET: go back to the address written in R3Let’s give CALL 1110 and RET 0011.
| Instruction | Number | Register write | Memory write | Subtract | Keep Z | Value to write | Register to write | Register to read | ALU 2nd value | Next address |
|---|---|---|---|---|---|---|---|---|---|---|
| LDI | 0110 | 1 | 0 | 0 | 0 | Immediate | Rd | · | · | 000(+1) |
| LOAD | 1000 | 1 | 0 | 0 | 0 | Memory | R0 | · | · | 000(+1) |
| STORE | 1001 | 0 | 1 | 0 | 0 | · | · | R0 | · | 000(+1) |
| MOVE | 0000 | 1 | 0 | 0 | 0 | Register | Rd | · | · | 000(+1) |
| ADD | 0001 | 1 | 0 | 0 | 1 | ALU | Rd | Rd | Rs | 000(+1) |
| SUB | 0010 | 1 | 0 | 1 | 1 | ALU | Rd | Rd | Rs | 000(+1) |
| ADDI | 0100 | 1 | 0 | 0 | 1 | ALU | Rd | Rd | Immediate | 000(+1) |
| SUBI | 0101 | 1 | 0 | 1 | 1 | ALU | Rd | Rd | Immediate | 000(+1) |
| JUMP | 1100 | 0 | 0 | 0 | 0 | · | · | · | · | 001(Address) |
| JZ | 1101 | 0 | 0 | 0 | 0 | · | · | · | · | 010(Address if Z) |
| HALT | 1111 | 0 | 0 | 0 | 0 | · | · | · | · | 011(Stay) |
| CALL | 1110 | 1 | 0 | 0 | 0 | PC + 1 | R3 | · | · | 001(Address) |
| RET | 0011 | 0 | 0 | 0 | 0 | · | · | R3 | · | 100(R3) |
Let’s fix the program from the start with CALL and RET.
Runs from address 0.
Press Next. Addresses 4–5 are written only once but run twice, and the program stops with R0 at 7.
The first RET returns to slot 2, and the second to slot 3. Wherever we call from, CALL writes that place into R3.
Let’s attach these to the machine from the jump post. CALL writes the PC’s +1 output into R3, and RET reads R3 and sends it to the PC’s LOAD. So we added a PC+1 input to the MUX in front of the register’s IN, an R3 input to the MUXes in front of the register numbers to write and read, and a MUX (PC SEL) in front of the PC’s LOAD. JUMP LOGIC turns on PC SEL along with J when the next address is R3.
Change CLK to 1 and back to 0, eight times over. The PC moves 0, 1, 4, 5, 2, 4, 5, 3, and the return addresses 2 and 3 are written into R3 in turn. At the end, R0 is 7.
CALL writes the return address into R3 before going, and RET comes back to that address. Thanks to this, a group of instructions is written once and called from many places. We now have thirteen instructions. In the next post, let’s gather the instructions we’ve made.
R3 holds only one return address. What happens when we run the program below, where the group at address 4 calls another group inside it?
Address 0: LDI R0, 0 Address 1: CALL 4 Address 2: HALT Address 3: (empty) Address 4: CALL 7 Address 5: ADDI R0, 1 Address 6: RET Address 7: ADDI R0, 2 Address 8: RET