CS ATLAS A map of computing

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

AddressWhat it is
16384 – 24575screen (8K words)
24576keyboard (1 word)
24577 – 32767unused

@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:

KeyScan codeNote
0 – 948 – 57digit keys
A – Z65 – 90uppercase letters (lowercase is 97–122)
Space32space
Enter128return
Backspace129backspace
← ↑ → ↓130 – 133arrow keys
Esc140escape

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.

KBD[24576]0000000000000000
0000000000000000
decimal0
hex0x0000
character(no key)

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

ApproachPrice
Memory mapping + polling (Hack)the CPU spins; but the hardware is minimal and the CPU never learns a keyboard exists
Interruptsthe CPU needs interrupt control: saving and restoring state, priorities
DMA or a dedicated controllerextra 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.