Skip to content

Commit c387679

Browse files
authored
Introduce local labels in hello-world.asm (#154)
* Introduce local labels in hello-world.asm * There is no `.copyTiles` label
1 parent 4a88fc5 commit c387679

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/assets/hello-world.asm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ WaitVBlank:
2929
; Copy the tile data
3030
ld de, Tiles
3131
ld hl, $9000
32-
ld bc, TilesEnd - Tiles
32+
ld bc, Tiles.End - Tiles
3333
CopyTiles:
3434
; ANCHOR: memcpy_first_two
3535
ld a, [de]
@@ -45,7 +45,7 @@ CopyTiles:
4545
; Copy the tilemap
4646
ld de, Tilemap
4747
ld hl, $9800
48-
ld bc, TilemapEnd - Tilemap
48+
ld bc, Tilemap.End - Tilemap
4949
CopyTilemap:
5050
ld a, [de]
5151
ld [hli], a
@@ -71,6 +71,7 @@ Done:
7171

7272
SECTION "Tile data", ROM0
7373

74+
; ANCHOR: tiles
7475
Tiles:
7576
db $00,$ff, $00,$ff, $00,$ff, $00,$ff, $00,$ff, $00,$ff, $00,$ff, $00,$ff
7677
db $00,$ff, $00,$80, $00,$80, $00,$80, $00,$80, $00,$80, $00,$80, $00,$80
@@ -142,10 +143,12 @@ Tiles:
142143
db $54,$ff, $aa,$ff, $54,$ff, $aa,$ff, $54,$ff, $aa,$ff, $54,$ff, $00,$ff
143144
db $15,$ff, $2a,$ff, $15,$ff, $0a,$ff, $15,$ff, $0a,$ff, $01,$ff, $00,$ff
144145
db $01,$ff, $80,$ff, $01,$ff, $80,$ff, $01,$ff, $80,$ff, $01,$ff, $00,$ff
145-
TilesEnd:
146+
.End:
147+
; ANCHOR_END: tiles
146148

147149
SECTION "Tilemap", ROM0
148150

151+
; ANCHOR: tilemap
149152
Tilemap:
150153
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, 0,0,0,0,0,0,0,0,0,0,0,0
151154
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, 0,0,0,0,0,0,0,0,0,0,0,0
@@ -165,4 +168,5 @@ Tilemap:
165168
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, 0,0,0,0,0,0,0,0,0,0,0,0
166169
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, 0,0,0,0,0,0,0,0,0,0,0,0
167170
db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, 0,0,0,0,0,0,0,0,0,0,0,0
168-
TilemapEnd:
171+
.End:
172+
; ANCHOR_END: tilemap

src/part1/memory.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ Writing out a label's name is equivalent to writing the address of the byte it's
9595
For example, consider the `ld de, Tiles` at line {{#line_no_of "ld\s+de\s*,\s*Tiles" ../assets/hello-world.asm}}.
9696
`Tiles` (line {{#line_no_of "^\s*Tiles:" ../assets/hello-world.asm}}) is referring to the first byte of the tile data; if we assume that the tile data ends up being stored starting at $0193, then `ld de, Tiles` is equivalent to `ld de, $0193`!
9797

98+
There's a shorthand for writing *local* labels: start their name with a dot `.` and it will implicitly be prefixed with the name of the most recent non-local label.
99+
For example, consider the two `.End:` labels at lines {{#line_no_of "^\.End:" ../assets/hello-world.asm:tiles}} and {{#line_no_of "^\.End:" ../assets/hello-world.asm:tilemap}}.
100+
The first one is implicitly defining "`Tiles.End:`" because of the non-local `Tiles:` label before it on line {{#line_no_of "^\s*Tiles:" ../assets/hello-world.asm}}, and the second one is defining "`Tilemap.End:`" because of the non-local `Tilemap:` label on line {{#line_no_of "^\s*Tilemap:" ../assets/hello-world.asm}}.
101+
Local labels are useful because as your project gets larger, you'll need more and more very similar names, and it would be tedious to have to make up unique prefixes for them everywhere.
102+
98103
## What's with the brackets?
99104

100105
Right, we came into this because we wanted to know what the brackets in `ld a, [de]` mean.
@@ -128,7 +133,7 @@ So, if we look at the first two instructions of `CopyTiles`:
128133
...we can see that we're copying the byte in memory *pointed to* by `de` (that is, whose address is contained in `de`) into the byte pointed to by `hl`.
129134
Here, `a` serves as temporary storage, since the CPU is unable to perform `ld [hl], [de]` directly.
130135

131-
While we're at this, let's examine the rest of `.copyTiles` in the following lessons!
136+
While we're at this, let's examine the rest of `CopyTiles` in the following lessons!
132137

133138
---
134139

0 commit comments

Comments
 (0)