Skip to content

Commit 566c5ca

Browse files
authored
Document the jr instruction and use it in hello-world.asm (#151)
1 parent c387679 commit 566c5ca

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/assets/hello-world.asm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ EntryPoint:
1919
WaitVBlank:
2020
ld a, [rLY]
2121
cp 144
22-
jp c, WaitVBlank
22+
jr c, WaitVBlank
2323

2424
; Turn the LCD off
2525
ld a, 0
@@ -39,7 +39,7 @@ CopyTiles:
3939
dec bc
4040
ld a, b
4141
or a, c
42-
jp nz, CopyTiles
42+
jr nz, CopyTiles
4343
; ANCHOR_END: memcpy
4444

4545
; Copy the tilemap
@@ -53,7 +53,7 @@ CopyTilemap:
5353
dec bc
5454
ld a, b
5555
or a, c
56-
jp nz, CopyTilemap
56+
jr nz, CopyTilemap
5757

5858
; Turn the LCD on
5959
ld a, LCDC_ON | LCDC_BG_ON
@@ -66,7 +66,7 @@ CopyTilemap:
6666
; ANCHOR_END: bgp_write
6767

6868
Done:
69-
jp Done
69+
jr Done
7070

7171

7272
SECTION "Tile data", ROM0

src/part1/jumps.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ There are four of them:
2727
| Call | `call` | Call a subroutine |
2828
| Return | `ret` | Return from a subroutine |
2929

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.
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.
3233
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).
3334

3435
:::tip:🤔
@@ -38,6 +39,8 @@ Don't worry, we will see later why it's required.
3839

3940
:::
4041

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+
4144
## Conditional jumps
4245

4346
Now to the _really_ interesting part.
@@ -94,7 +97,7 @@ There are four "conditions":
9497
| Carry | `c` | C is set (last operation overflowed) |
9598
| No carry | `nc` | C is not set (last operation did not overflow) |
9699

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

100103
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

Comments
 (0)