- Rule 1: Everything Is a Sequence of Instructions
- Rule 2: Jumps Make the Sequence Non-Linear
- Rule 3: Tests Control Jumps
- Rule 4: Storage Controls Tests
- Rule 5: Input/Output Controls Storage
- Putting It All Together
Rule 2: Jumps Make the Sequence Non-Linear
A sequence of simple instructions like LOAD_CONST 10 is not very useful. Yay! You can load the number 10! Amazing! Where code starts to become useful is when you add the concept of the “jump” to make this sequence non-linear. Let’s look at a new piece of Python code:
1 while True: 2 x = 10
To understand this code we have to foreshadow a later exercise where you learn about the while-loop. The code while True: simply says “Keep running the code under me x = 10 while True is True.” Since True will always be True, this will loop forever. If you run this in Jupyter, it will never end.
What happens when you dis() this code? You see the new instruction JUMP_ABSOLUTE:
1 dis("while True: x = 10") 2 3 0 LOAD_CONST 1 (10) 4 2 STORE_NAME 0 (x) 5 4 JUMP_ABSOLUTE 0 (to 0)
You saw the first two instructions when we covered the x = 10 code, but now at the end we have JUMP_ABSOLUTE 0. Notice there’s numbers 0, 2, and 4 to the left of these instructions? In the previous code, I cut them out so you wouldn’t be distracted, but here they’re important because they represent locations in the sequence where each instruction lives. All JUMP_ABSOLUTE 0 does is tell Python to “jump to the instruction at position 0”, which is LOAD_CONST 1 (10).
With this simple instruction we now have turned boring straight line code into a more complex loop that’s not straight anymore. Later, we’ll see how jumps combine with tests to allow even more complex movements through the sequence of bytes.
Why is this backward?
You may have noticed that the Python code reads as “while True is True set x equal to 10” but the dis() output reads more like “set x equal to 10, jump to do it again.” That’s because of Rule #1, which says we have to produce a sequence of bytes only. There are no nested structures, or any syntax more complex than INSTRUCTION OPTIONS, allowed.
To follow this rule Python has to figure out how to translate its code into a sequence of bytes that produces the desired output. That means moving the actual repetition part to the end of the sequence so it will be in a sequence. You’ll find this “backward” nature comes up often when looking at byte codes and assembly language.
Can a JUMP go forward?
Yes, technically a JUMP instruction is simply telling the computer to process a different instruction in the sequence. It can be the next one, a previous one, or one in the future. The way this works is the computer keeps track of the “index” of the current instruction, and it simply increments that index.
When you JUMP, you’re telling the computer to change this index to a new location in the code. In the code for our while loop (below) the JUMP_ABSOLUTE is at index 4 (see the 4 to the left). After it runs, the index changes to 0 where the LOAD_CONST is located, so the computer runs that instruction again. This loops forever.
1 0 LOAD_CONST 1 (10) 2 2 STORE_NAME 0 (x) 3 4 JUMP_ABSOLUTE 0 (to 0)