Module 6 · Fetch, Decode, Do It Again

How does a list of steps do something over and over?

Lesson 6.4 · Jump

Rob · Jenny · Sarah

Three, two, one, jump.

Rob wants a countdown: 3, 2, 1.

Jenny says that’s a loop, and a loop needs one more kind of instruction. Not load, add or store. Jump.

Go back to an earlier step, unless the number has hit zero. Sarah says that’s a condition from Module 4.

Jenny says exactly, and the processor checks it with the ALU from Module 5.

The short answer

With a jump. The program counter normally steps forward, and a jump sends it back. Make the jump depend on a condition, like the count hitting zero, and you get a loop that knows when to stop. That’s Part II: gates, adders and a loop, all built from switches.

Try it yourself

This program counts down. Step through it and watch the arrow jump back.

Next instruction at0
Working number0

Memory

0LOAD 7
1OUT
2SUB 8
3STORE 7
4JUMPZ 6
5JUMP 0
6HALT
73count
81one

Screen

0 steps so far

What you just did has a name

Registers and jumps

Registers are a handful of tiny storage spots right inside the processor, like the working number and the program counter, the number that says which instruction is next.

Normally the program counter just goes up by one. A jump instruction changes it, sending the processor somewhere else.

A conditional jump only does that if a condition is true, like “the answer was zero.”

Loops, if-thens and every decision a program makes come down to that.

A few more things

Registers are the fastest storage

A processor has only a few dozen registers, but they’re the fastest memory in the computer, right next to the ALU. Everything else is slower and farther away.

The bus

Numbers travel between the processor and memory on a set of wires called a bus. Module 14 shows how the motherboard is mostly buses.

Infinite loops

Forget the condition and the loop never ends. The program freezes, spinning forever. That’s a classic bug, and why apps sometimes hang.

Go deeper

The longer version, if you want it.

How a game runs

A video game is one giant loop: read the controller, update the world, check for hits, draw the frame, jump back to the top. It runs that loop sixty or more times a second.

Inside that loop are thousands of smaller loops and conditions, all built from the same instructions you just stepped through. Load, add, compare, jump. Scale it up and you get a whole world on screen.

Quick check

What does a JUMP instruction change?

Let’s take it slower

Think of a board game that says “if you land on a ladder, go up to square 40.” Normally you move forward one space at a time. A space that says ‘go back to start’ is a jump. A space that says ‘go back to start unless you rolled a six’ is a conditional jump. Every loop in every program is a board full of ladders and chutes.

More examples

1

Your music playlist on repeat

Play the next song. At the end of the list, jump back to the first song. A loop with one jump.

2

Typing a password

Check the password. If it’s wrong and you have tries left, jump back and ask again. If it’s right, jump to unlocking.

Back to the question

So how does a list of steps repeat?

Show the answer

With a jump. The program counter normally steps forward, and a jump sends it back. Make the jump depend on a condition, like the count hitting zero, and you get a loop that knows when to stop. That’s Part II: gates, adders and a loop, all built from switches.

More on this: CPU vs. GPU: what’s the difference?

One last question

Mark asks how a program does something over and over. Who got it right?

Talk Card

Talk Card · 6.4

A program repeats itself with one trick: jump back to an earlier step, and check a condition to know when to stop.

How Computers Work · pettigrewlab.com/learn

Next lesson