Bigger Than a Byte
A byte stops at 255 — too small for an address. See how registers pair up into 16-bit numbers, and use add hl, de to step a pointer by more than one, the way a game walks down a wall.
One byte holds 0 to 255. But a screen address runs up to 65535 — far past what a byte can carry. You've already been using numbers that big: $5800, $4000, $9000 are all sixteen-bit. So how does the machine hold them?
By pairing registers up. HL, DE and BC each join two 8-bit registers into one 16-bit number, 0 to 65535. That's why a pointer lives in HL — an address needs all sixteen bits.
And pairs do arithmetic too. In Beat 9 the finger stepped by one with inc hl. To step by more, you add a sixteen-bit number:
ld de, 32— put 32 into the pairDE.add hl, de—HL = HL + 32, a sixteen-bit add.
inc hl walked across the colour map (one cell). add hl, de with 32 walks down it (a whole row of 32 cells at once).
What you'll see by the end
Three red cells in a column, not a row. Each add hl, de moved the pointer 32 cells along — which, because the colour map is 32 cells wide, lands it exactly one row lower. Stepping by 32 is stepping down.
Stepping down by 32
; ============================================================================
; PRIMER — Beat 14: Bigger Than a Byte
; ============================================================================
; One byte stops at 255. But addresses run to 65535, far past a byte -- which
; is why registers PAIR UP: HL, DE and BC each hold a 16-bit number, 0-65535.
;
; In Beat 9 the finger stepped by one: inc hl. To step by MORE than one, you
; add a 16-bit number to it:
;
; ld de, 32 -- 32 = one cell-row down the colour map
; add hl, de -- HL = HL + 32 (a 16-bit add)
;
; inc hl walked ACROSS (Beat 9); add hl, de here walks DOWN. We colour three
; cells in a column by stepping 32 each time. (This is exactly how the tiny
; game draws the sides of a wall.)
; ============================================================================
org 32768
start:
ld hl, $5800 ; top-left colour cell
ld a, $17 ; red
ld de, 32 ; one cell-row = 32 cells
ld (hl), a ; colour the top cell
add hl, de ; step down one row (HL = HL + 32)
ld (hl), a ; colour the cell below
add hl, de ; down again
ld (hl), a ; and the next
.loop:
halt
jr .loop
end start
ld de, 32 loads the step; each add hl, de slides the pointer down one row; each ld (hl), a colours where it lands. This is the exact move the tiny first game uses to draw the sides of a wall — down a column, 32 at a time.
Assemble and run
pasmonext --sna bigger-than-a-byte.asm primer.sna
A red column, three cells tall, stepped out by sixteen-bit addition.
Try this: a different step
Change ld de, 32 to ld de, 33. Now each step goes down one row and one cell right — a diagonal. Try ld de, 1 and you're back to stepping across, the same as inc hl. The step is yours to choose.
Try this: a longer column
Add more add hl, de / ld (hl), a pairs to extend the column down the screen. (Feel the repetition? A loop — Beat 10's djnz — would walk it for you, counting the rows. Pointer plus step plus loop is how every wall, road and border gets drawn.)
When it's wrong, see why
pasmonexterrors on the add. Sixteen-bit adds go throughHL: it'sadd hl, de, notadd hl, 32. Load the amount intoDEfirst withld de, 32.- The cells go across, not down. Your step is too small —
ld de, 1steps one cell (across). One row down isld de, 32. - The cells scatter or vanish. A wrong step can walk
HLclean off the colour map. Keep the start at$5800and the step at 32 until the column behaves.
What you've learnt
Registers pair up (HL, DE, BC) to hold 16-bit numbers to 65535, and add hl, de steps a pointer by any amount — by 32, one whole row down the screen.
What's next
That's the last of the machine's everyday fluency with numbers. One thing the machine does is still untouched — the one you hear. Next — The Machine Speaks Back — and here the Spectrum is unlike every other machine in this course: it has no sound chip at all. The CPU itself makes the tone, by hand.