Input · Keyboard
The keyboard: reading one memory address
Like the screen, the keyboard has no input instruction. It is simply the word at RAM[24576]: while a key is held the word holds that key’s scan code; released, it is 0. All a program does is keep reading it.
01The keyboard lives at 24576
| Address | What it is |
|---|---|
| 16384 – 24575 | screen (8K words) |
| 24576 | keyboard (1 word) |
| 24577 – 32767 | unused |
@KBD in assembly is just @24576. The CPU has no idea a keyboard exists — it only reads an address.
02What the word holds: a scan code
Pressing a key makes the hardware write its code into 24576; releasing writes 0. The codes follow ASCII, so the common ones are easy to remember:
| Key | Scan code | Note |
|---|---|---|
| 0 – 9 | 48 – 57 | digit keys |
| A – Z | 65 – 90 | uppercase letters (lowercase is 97–122) |
| Space | 32 | space |
| Enter | 128 | return |
| Backspace | 129 | backspace |
| ← ↑ → ↓ | 130 – 133 | arrow keys |
| Esc | 140 | escape |
Only one value at a time: hold two keys and you read the last one pressed. There is no buffer and no queue.
03No interrupts, only polling
Hack has no “notify me on a key press”. To wait for a key you read it in a loop:
(WAIT) @KBD D=M // read the keyboard @WAIT D;JEQ // still 0 (no key): keep waiting // a key is down; D holds its scan code
04Hands-on: press a key and watch 24576 change
Click a key: the right side shows KBD in binary, decimal and as a character; “Release all” sets it back to 0.
05Three common patterns
Echo the key to the top-left corner: write the code into the word at 16384.
(LOOP) @KBD D=M @LOOP D;JEQ @SCREEN M=D // top-left 16 pixels = the code in binary
06Why design it this way
| Approach | Price |
|---|---|
| Memory mapping + polling (Hack) | the CPU spins; but the hardware is minimal and the CPU never learns a keyboard exists |
| Interrupts | the CPU needs interrupt control: saving and restoring state, priorities |
| DMA or a dedicated controller | extra chips and a protocol |
As with the screen, this is the same idea: turn devices into addresses. Input and output collapse into reading and writing memory, so the CPU has only one way to touch the world.