00000000: d52e 76e5 2e07 e5cd 0380 38f6 ..v.......8.
Try it online!
Z:ld l, $76
ld l, G
jr c, Z+1 ; halt on EOF, else fallthrough
Z80golf is a small Z80 computer hooked up to
$8000=putchar, $8003=getchar. (
putchar prints byte A;
getchar reads a byte into A or sets the carry flag on EOF.) Our code is placed at
$0000 and all registers start at 0, including PC. The rest of memory is filled with zeroes.
Let's run through some sample input:
We push three addresses to the stack:
Then (here's label
G), we call
getchar(), and on EOF jump to the HALT instruction (
$76) hidden in the argument to
ld l, $76.
Otherwise, control flow falls through a sea of NOPs into
$8000. This is hardcoded to perform
putchar(A); ret, so we print
u and return back to
The next time we get here, we print
v and return to
$0076, amid the NOP sea: control flow runs back to
$8000, we print
v again, then return to
$0000, where the code starts over.
Repeating this process, the output is
uvvwxxyzz → HALT.