Skip to content

Commit 982f773

Browse files
committed
Correct the line number of the jump target
Fixes #120
1 parent 0c2600f commit 982f773

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/part1/jumps.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ So far, all the code we have seen was linear: it executes top to bottom.
1010
But this doesn't scale: sometimes, we need to perform certain actions depending on the result of others ("if the crêpes start sticking, grease the pan again"), and sometimes, we need to perform actions repeatedly ("If there is some batter left, repeat from step 5").
1111

1212
Both of these imply reading the recipe non-linearly.
13-
In assembly, this is achieved using *jumps*.
13+
In assembly, this is achieved using _jumps_.
1414

1515
The CPU has a special-purpose register called "PC", for Program Counter.
1616
It contains the address of the instruction currently being executed[^pc_updates], like how you'd keep in mind the number of the recipe step you're currently doing.
17-
PC increases automatically as the CPU reads instructions, so "by default" they are read sequentially; however, jump instructions allow writing a different value to PC, effectively *jumping* to another piece of the program.
17+
PC increases automatically as the CPU reads instructions, so "by default" they are read sequentially; however, jump instructions allow writing a different value to PC, effectively _jumping_ to another piece of the program.
1818
Hence the name.
1919

2020
Okay, so, let's talk about those jump instructions, shall we?
2121
There are four of them:
2222

23-
Instruction | Mnemonic | Effect
24-
--------------|----------|---------------------------------------------
25-
Jump | `jp` | Jump execution to a location
26-
Jump Relative | `jr` | Jump to a location close by
27-
Call | `call` | Call a subroutine
28-
Return | `ret` | Return from a subroutine
23+
| Instruction | Mnemonic | Effect |
24+
| ------------- | -------- | ---------------------------- |
25+
| Jump | `jp` | Jump execution to a location |
26+
| Jump Relative | `jr` | Jump to a location close by |
27+
| Call | `call` | Call a subroutine |
28+
| Return | `ret` | Return from a subroutine |
2929

3030
We will focus on `jp` for now.
3131
`jp`, such as the one line {{#line_no_of "^\s*jp" ../assets/hello-world.asm}}, simply sets PC to its argument, jumping execution there.
32-
In other words, after executing `jp EntryPoint` (line {{#line_no_of "^\s*jp EntryPoint" ../assets/hello-world.asm}}), the next instruction executed is the one below `EntryPoint` (line <!-- should be {{#line_no_of "^\s*EntryPoint:" ../assets/hello-world.asm}} + 1 --> 16).
32+
In other words, after executing `jp EntryPoint` (line {{#line_no_of "^\s*jp EntryPoint" ../assets/hello-world.asm}}), the next instruction executed is the one below `EntryPoint` (line <!-- should be {{#line_no_of "^\s*EntryPoint:" ../assets/hello-world.asm}} + 1 --> 11).
3333

3434
:::tip:🤔
3535

@@ -40,7 +40,7 @@ Don't worry, we will see later why it's required.
4040

4141
## Conditional jumps
4242

43-
Now to the *really* interesting part.
43+
Now to the _really_ interesting part.
4444
Let's examine the loop responsible for copying tiles:
4545

4646
```rgbasm,linenos,start={{#line_no_of "" ../assets/hello-world.asm:memcpy}}
@@ -56,7 +56,7 @@ If you're having trouble, try going to the next lesson, watch the code execute s
5656

5757
First, we copy `Tiles`, the address of the first byte of tile data, into `de`.
5858
Then, we set `hl` to $9000, which is the address where we will start copying the tile data to.
59-
`ld bc, TilesEnd - Tiles` sets `bc` to the length of the tile data: `TilesEnd` is the address of the first byte *after* the tile data, so subtracting `Tiles` to that yields the length.
59+
`ld bc, TilesEnd - Tiles` sets `bc` to the length of the tile data: `TilesEnd` is the address of the first byte _after_ the tile data, so subtracting `Tiles` to that yields the length.
6060

6161
So, basically:
6262

@@ -87,22 +87,19 @@ See, it's possible to **conditionally** "take" a jump depending on the state of
8787

8888
There are four "conditions":
8989

90-
Name | Mnemonic | Description
91-
---------|----------|----------------------------------------------------
92-
Zero | `z` | Z is set (last operation had a result of 0)
93-
Non-zero | `nz` | Z is not set (last operation had a non-zero result)
94-
Carry | `c` | C is set (last operation overflowed)
95-
No carry | `nc` | C is not set (last operation did not overflow)
90+
| Name | Mnemonic | Description |
91+
| -------- | -------- | --------------------------------------------------- |
92+
| Zero | `z` | Z is set (last operation had a result of 0) |
93+
| Non-zero | `nz` | Z is not set (last operation had a non-zero result) |
94+
| Carry | `c` | C is set (last operation overflowed) |
95+
| No carry | `nc` | C is not set (last operation did not overflow) |
9696

9797
Thus, `jp nz, CopyTiles` can be read as "if the Z flag is not set, then jump to `CopyTiles`".
98-
Since we're jumping *backwards*, we will repeat the instructions again: we have just created a **loop**!
98+
Since we're jumping _backwards_, we will repeat the instructions again: we have just created a **loop**!
9999

100-
Okay, we've been talking about the code a lot, and we have seen it run, but we haven't really seen *how* it runs.
100+
Okay, we've been talking about the code a lot, and we have seen it run, but we haven't really seen _how_ it runs.
101101
Let's watch the magic unfold in slow-motion in the next lesson!
102102

103103
---
104104

105-
[^pc_updates]:
106-
Not exactly; instructions may be several bytes long, and PC increments after reading each byte.
107-
Notably, this means that when an instruction finishes executing, PC is pointing to the following instruction.
108-
Still, it's pretty much "where the CPU is currently reading from", but it's better to keep it simple and avoid mentioning instruction encoding for now.
105+
[^pc_updates]: Not exactly; instructions may be several bytes long, and PC increments after reading each byte. Notably, this means that when an instruction finishes executing, PC is pointing to the following instruction. Still, it's pretty much "where the CPU is currently reading from", but it's better to keep it simple and avoid mentioning instruction encoding for now.

0 commit comments

Comments
 (0)