You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/part1/jumps.md
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,9 +27,8 @@ There are four of them:
27
27
| Call |`call`| Call a subroutine |
28
28
| Return |`ret`| Return from a subroutine |
29
29
30
-
We will focus on the first two, `jp` and `jr`, for now.
31
-
32
-
`jp`, such as the one on line {{#line_no_of "^\s*jp" ../assets/hello-world.asm}}, simply sets PC to its argument, jumping execution there.
30
+
We will focus on `jp` for now.
31
+
`jp`, such as the one line {{#line_no_of "^\s*jp" ../assets/hello-world.asm}}, simply sets PC to its argument, jumping execution there.
33
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).
34
33
35
34
:::tip:🤔
@@ -39,8 +38,6 @@ Don't worry, we will see later why it's required.
39
38
40
39
:::
41
40
42
-
`jr`, such as the one on line {{#line_no_of "^\s*jr" ../assets/hello-world.asm}}, is functionally the same as `jp`. However, it sets PC *relative* to the current PC value, and can only jump execution forward or backward by 128 bytes---a short distance given that each instruction takes one to three bytes. The advantage of `jr` is that it takes up two bytes instead of three like `jp` (since it encodes a one-byte relative distance instead of a two-byte absolute address), and also takes one CPU cycle less than `jp` to execute, so it's commonly used when you know that a jump will be short.
43
-
44
41
## Conditional jumps
45
42
46
43
Now to the _really_ interesting part.
@@ -97,7 +94,7 @@ There are four "conditions":
97
94
| Carry |`c`| C is set (last operation overflowed) |
98
95
| No carry |`nc`| C is not set (last operation did not overflow) |
99
96
100
-
Thus, `jr nz, CopyTiles` can be read as "if the Z flag is not set, then jump to `CopyTiles`".
97
+
Thus, `jp nz, CopyTiles` can be read as "if the Z flag is not set, then jump to `CopyTiles`".
101
98
Since we're jumping _backwards_, we will repeat the instructions again: we have just created a **loop**!
102
99
103
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.
0 commit comments