Everything Is a Number
A byte has no type. Discover that 65, $41, the letter 'A' and the bit-pattern %01000001 are one and the same byte — and meet hex, the way you'll read bytes from here on.
You've been moving bytes between registers. Now the question nobody asked in BASIC: what is a byte, exactly?
In BASIC, the kinds were kept firmly apart — 5 was a number, "A" was a string, and mixing them was an error. The machine has no such manners. A register holds a byte — a number from 0 to 255 — and that's the only kind of thing there is. Not a letter, not a colour, not a character. Those are meanings you decide to read into a number. The byte itself has no type.
The cleanest way to feel this is to load a letter into a number register and watch the machine shrug.
What you'll see by the end
A white border. Here's the strange part: we didn't load the number 1 (white's code). We loaded the letter 'A'. The machine didn't blink — to it, 'A' is 65, and we stored 65 at the border, where its low four bits land on white. A letter was a number all along.
One byte, four ways to write it
These four lines all put the same byte into A:
| You write | What it is | Value |
|---|---|---|
lda #65 | decimal | 65 |
lda #$41 | hex (the $ says "hex") | 65 |
lda #'A' | a letter (its character code) | 65 |
lda #%01000001 | binary (the % says "bits") | 65 |
Four notations, one byte: $41. They're not "equal" — they're identical. The assembler turns every one of them into the same eight bits before the machine ever sees them.
Hex ($41) is worth getting used to now, because the register view shows bytes in it. It's base 16: two hex digits cover one byte exactly, $00 to $FF. $41 is 4 × 16 + 1 = 65. You'll read plenty of hex from here on; it fits a byte more neatly than decimal does.
And that last one — %01000001 — shows the byte's eight actual bits, the on/off switches the machine stores. The border proves it: the value's low four bits are 0001, which is 1, which is white. ($D020 keeps only the low four bits — the C64 has sixteen colours, and four bits is exactly enough to count them.) We'll start flipping individual bits like these in Unit 13.
The program
; Meet the Machine - Unit 3: Everything Is a Number
; Assemble with: acme -f cbm -o letter.prg letter.asm
*= $0801
!byte $0c,$08,$0a,$00,$9e,$32,$30,$36,$31,$00,$00,$00
*= $080d
lda #'A' ; load the LETTER 'A' ... which to the machine is just 65
sta $d020 ; show that byte on the border
loop jmp loop
We load the letter and show it. Open the register view and you'll see A holding $41 — the machine's honest opinion of what a letter is.
Assemble and run
acme -f cbm -o letter.prg letter.asm
Load it, and the border is white — colour 1, which is 'A''s low four bits, which came from 65, which is $41, which is %01000001. All the same byte.
Try this: the same byte, four ways
Change lda #'A' to lda #65, assemble and run — same white border. Now try lda #$41, then lda #%01000001. Same white every time, because they're the same byte. You're not changing the program's behaviour; you're changing how you wrote a number the machine reads identically.
Try this: read a different letter
What number is the letter 'Z'? Change the load to lda #'Z', and before you run it, predict the value — then check the register view. (It's 90; the capital letters run from 'A' = 65 upward.) The border comes up light red — 90's low four bits are 1010, colour 10. The machine knows these as numbers; the letters are our idea.
If it doesn't work
acmeerrors on the'A'line. Use straight single quotes around exactly one character —'A', not‘A’(curly quotes) and not"A"(double quotes make a string, not a byte).- The border isn't white. Check you loaded a value whose low four bits are
0001—'A'(65), or 1, or 17. The border reads only the bottom four bits, so it's the value mod 16 that picks the colour. %01000001won't assemble. The binary prefix is%, with exactly eight bits and no spaces.
What you've learnt
A byte has no type: the same value can be written as a number, as hex, as a letter or as a bit-pattern, and the machine treats every one of them identically.
What's next
If a byte can be a letter or a colour, where do bytes live when they're not in a register? Next — A Street of Numbered Boxes — we meet memory: one long row of numbered slots, each holding a byte, all of it yours to write and read.