Z80golf
A real processor from 1976 as a golfing language: 64 KB of memory, two magic addresses for I/O, and HALT.
2018 · Turing complete: with unbounded memory · ●●● hard · esolangs.org
Tested with 4 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
Z80golf is a tiny fantasy machine built around the Zilog Z80, the chip in the ZX Spectrum. It is used on the anarchy golf site, where the score is the size of the binary. The language is Z80 machine code. What makes it a golfing language is how bare the machine is, which invites tricks: running through a sled of zero bytes, which are NOPs, or letting the stack pointer wrap around into the program and read its bytes as return addresses.
The machine
The program is loaded at address $0000 and the rest of the 64 KB is zero. Every register starts at 0, the stack pointer and the program counter included.
| Address | When the program counter reaches it |
|---|---|
$8000 | Print register A, then return as RET does |
$8003 | Read a byte into A and clear the carry flag; at the end of the input set carry and leave A. Then return |
HALT (byte 76) ends the program. So call $8000 is putchar and call $8003 is getchar. The program is written as an xxd hex dump, the way TIO shows it.
Example
ld a,(ix+14) loads the byte at IX plus 14, where the text starts.
ld a,(ix+14) loads the byte at IX plus 14, where the text starts. or a tests it, and a zero byte past the text reaches halt. Otherwise call $8000 prints it, inc ix moves on and jr loops back.
A cat: get a character, halt if the carry flag says the input is over, print it, jump back to the start.
The same cat in 7 bytes. After reading, the program does not call putchar. It runs on through 32,761 zero bytes, each a NOP, until it reaches $8000 by itself. The RET there pops the 0 that push de saved, which sends it back to the start.
Patterns it teaches
- Code golf: the score is the byte count, so the best answers bend every rule of the machine.
- Self-modifying code and code as data: code, data and the stack share one memory, and short programs read their own bytes as addresses.
Try this
- Print
Awithld a,65(3e 41),call $8000andhalt. - Print each input byte twice.
- Hard: beat the 27-byte hello world. The best known one is 19 bytes.
See also
Golfing languages
Write your own in Run Convert a program from Z80golf
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: z80golf.
Command line
shib run z80golf -e '00000000: f9 7c d2 03 80 af 76' --input 'ab'shib manual z80golfSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/z80golf.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org