A counter only goes up by 1 every clock. So a program only runs one line at a time from top to bottom. To do the same thing ten times, we have to write the same instruction ten times, and if how many times depends on a number in a register, we can’t build it at all. We need instructions that go back and repeat earlier instructions and stop when a condition is met.
JUMP Addr: change the next address to AddrJZ Addr: jump to Addr only when the Zero Flag is 1HALT: stopJUMP and JZ read the last 4 bits as the address of the next instruction to run,
so let’s put them in kind 11 as 1100 and 1101. HALT is 1111. The control
table gets a Next address column.
| 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) |
Here’s an example that adds 1 through R0 and leaves the sum in R1.
Address 0: ADD R1, R0
Address 1: SUBI R0, 1
Address 2: JZ 4 ← if it's 0, go to address 4
Address 3: JUMP 0 ← otherwise, go to address 0
Address 4: HALT
The control unit sends the table’s Next address column as a select signal NEXT. The next post adds one more choice, more than 2 bits can hold, so NEXT gets 3 bits. Let’s decode NEXT with a decoder and turn on the jump signal J when it’s 001, or 010 with Z being 1, and the stop signal HOLD when it’s 011. From now on we’ll call this circuit JUMP LOGIC.
Change NEXT to 010 (Address if Z). J turns on only when Z is also 1. With 001, J is always on, and with 011, HOLD turns on.
The counter that counts the next address to run is called the Program Counter (PC). A jump puts a different address into the PC instead of PC + 1.
Below, a MUX is inserted in front of each register of the counter circuit. Normally it picks PC + 1 from the half adders, and when J is 1 it picks the address to jump to (A0~A3). HOLD goes through a NOT to the registers’ W, so when HOLD is 1 the PC stays put even when the clock comes.
From now on, let’s draw this circuit as a single PC box, and JUMP LOGIC as a box too.
Let’s attach these to the machine from the ALU post. The PC’s LOAD gets the instruction’s address, and J and HOLD get JUMP LOGIC. R0 holds 3.
Keep giving clocks. R1 builds up 3, 5, and 6, and when R0 reaches 0 the machine goes to slot 4 and stops.
JUMP doesn’t remember where it came from. The PC box’s +1 output, which we haven’t used yet, is the address right after the current one. In the next post, let’s use this value to make instructions that go and come back.
Without JUMP, we built a loop with JZ alone. When R0 starts at 1 or more, what must go at address 3 so that ADDI R1, 3 runs as many times as R0's starting value and then reaches address 5? The Zero Flag is changed only by ADD, SUB, ADDI, and SUBI, and R2 is not used in this program.
Address 0: ADDI R1, 3 Address 1: SUBI R0, 1 Address 2: JZ 5 Address 3: (blank) Address 4: JZ 0 Address 5: (next job)