
The greatest program ever written - niyazpk
http://www.kuro5hin.org/story/2001/8/10/12620/2164
======
tezza
I grew up coding assembly for 6502... and you can fit a lot in 1k.

the 1k is aided and abetted by the architecture of the computer.

* Reading keyboard is reading a register/known address, not loading the keyboard library, allocating a structure and jumping into an OS routine.

* Clear screen is writing zeros to a known address space

* Drawing is writing a 1 to a known address space

* Yours is the only process on the machine

* Text compresses well with Huffman

With those assumptions a lot more can fit in the 1K

~~~
pvg
I don't see how these assumptions really help - 'greatest program ever
written' is a bit bombastic but working chess program in a few hundred bytes
of z80 code is still quite impressive.

 _Reading keyboard is reading a register/known address, not loading the
keyboard library, allocating a structure and jumping into an OS routine_

Or calling a ROM routine. Reading the keyboard directly at the lowest level
would probably just make the program bigger

 _Clear screen is writing zeros to a known address space_

Generally, another ROM routine call.

 _Drawing is writing a 1 to a known address space_

Not really. Just about all these machines had color. The Apple ][ had a fun
non-linear video buffer layout, to boot.

 _Text compresses well with Huffman_

Not a lot of text in a chess program.

~~~
roundsquare
_Reading the keyboard directly at the lowest level would probably just make
the program bigger_

 _Generally, another ROM routine call._

Thats @tezza's point. (Edit For Clarity): Since its just one instruction, you
can shrink the size of your program, making fitting it into a small space less
impressive (though, not unimpressive).

 _Not a lot of text in a chess program._

Fair enough, so that one doesn't help, but the other two do.

~~~
pvg
Except it's not one instruction - you still have to put the result somewhere,
potentially save registers (of which there is a great dearth) and so forth. My
point is none of the things listed (aside from not really being part of 'the
architecture') are significant factors on top of being largely inaccurate.
What does make a difference is that the instructions themselves are short, the
addresses are short, alignment is on the byte, etc. Can you fit a lot into 1k?
Sure, if you're clever, you can apparently fit a whole chess program in less.
But the fact that, say, there's just your process or that Huffman encoding is
neat has next to nothing to do with it.

~~~
roundsquare
I'm really not clear on where we disagree at all. We seem to agree that:

1) Reading keyboard input and clearing the screen can both be executed via a
ROM command.

2) Doing these things otherwise would have made the program bigger.

Am I right that we agree on these? If so, I don't see how you don't agree
that:

3) Having both of these as ROM commands helps make the program smaller.

As far as I can tell, we agree on these 3 points. You, however, seem to be
bringing up a few more points:

4) Instructions are short.

5) Alignment is on the byte.

...

And, from what I can tell, you are saying that these were _more_ helpful in
reducing the size of the program. Am I right so far?

If so, you maybe right that compared to modern computers, but that doesn't
mean that @tezza is wrong that the other factors helped.

Also, I believe its already been agreed that Huffman encoding doesn't help...

~~~
pvg
Heh, well, I'm not clear what is unclear. The poster said 'well, the program
is short because of these N things'. Except none of the N things were accurate
or relevant. There _are_ architectural factors that much more significantly
affect the size of the program, but poster didn't mention any of them. My
point is tezza either didn't know or didn't think what s/he was posting about.
It's sort of like having the discussion "So, airplanes fly because of magic" -
"Well, no actually airplanes fly because of [a bunch of physics]" - "Right,
you're making my point, airplanes fly!".

~~~
tezza
Dude, you're going over the top, under the bottom and around the sides.

8bit coding was very different. I listed some common tricks not necessarily
specifically ZX81, nor necessarily used in this chess game, but illustrative
of common scenarios.

Thus you could fit much more in 1k than you can today.

ENDE (please take a deep breath)

~~~
pvg
Please don't 'dude' me. My breath is perfectly even. And at this point I don't
think I was even replying to you. The things you said make make a program
short are simply inaccurate and not the significant factor in making programs
shorter. That's all. Nobody made their programs shorter by the 'tricks' you
describe. And it's not a ZX81 thing. To read the keyboard on, say, a 6502
apple ][ you have to do a store to clear the strobe, a read to get a value and
a check on the most significant bit of the value to check for keypress and
then you need to mask out the actual value. I don't need to go into endless
detail about the rest of them. The primary factor is the size of the
instructions and the size of their operands (8 and 16 bit addresses).

------
michael_dorfman
Not to knock Mr. Horne's achievement, but Peter Jennings wrote Microchess for
the KIM-1 (a 6502-based machine with 1K of RAM) back in 1976.

Source code here:
<http://users.telenet.be/kim1-6502/microchess/microchess.html>

I had the PET 2001 version (running in 7K of RAM) back in the day, and it
played quite well, all things considered.

~~~
michael_dorfman
In fact, just to show what we're talking about, here it is

    
    
         D8A2FF9AA2C886B2201F1F206A1FC5F3F0F685F3C90CD00FA21FB5709550CA10F986DCA9CCDO12C90ED00720B202A9EED007C914D00B20A20385FB85FA85F9D0BFC90FD006204B034C9D0l4C960110000304000702O50l06101711161215141373747077727571766067616662656463F0FF0l1011OFEFF1DFElEEF2120E1F210B0A0606040404O40202020202020202A6B5305

CA5B0F308EO08D004C5E6F02EF6E3C901D002F6E3501EA00FA5B1D96000
F0038810F8B9A000D5E4900494E695E4180875E595E528E004F00330316
0A5E885DDA90085B5204B0320B20220000220B202A90885B52009022031
034C8017E0F9DO0BA560C5B1D004A90085B46050FDA007A5B1D96000F00
588F0F110F6B9A000D5E2900295E2C6B5A9FBC5B5F003202503E6B560C9
08B01220EA03A21FB550C5FAF003CA10F786FB86BO4C000000A210A9009
5DECA10FBA91085B0C6B0100l60201E03A4B0A20886B6C0081041C00610
2EC004101FC00lF009100E208E02D0FBF0D9209C02DOFBF0D2A20486B62
09C02D0FBF0C7209C02A5B6C904D0F7F0BCA21086B6208E02A5B6C908D0
F7F0ADA20686B620CA025005300320000l201E03C6B6A5B6C9O5F0EB20C
A02708F308D20000lA5B129FOC920FOEE4C0D0220CA023003200001201E
03C6B66020CA02900250F930070820000l2850F0201E03C6B660A20F38B
460A977F550956094503550CA10EB60A5B1A6B618758F85B12988D042A5
B1A220CA300ED550D0F9E0103033A97F69017001B8A5B53024C90810204
808A9F985B585B4204B0320B202200902202E03286885B5A5B4300438A9
FF6018A90060A9FF18B860A6B0B55085B160204B0320B20220090220820
2BA86B3A6B29A6885B66885B0AA68955068AA6885B195504C7003BA86B3
A6B29AA5B148A8A21FD550F003CA10F9A9CC95508A48A6B0B5509450488
A48A5B648BA86B2A6B39A60A6E4E4A0D004A900F00AA6E3D006A6EEDO02
A9FFA20486B5C5FA900CF00A85FAA5B085FBA5B185F94C1F1FA6DC1017A
5F9D5DCD00FCAB5DC85FBCAB5DC85F9CA86DCD01A85DCA20C86B586FAA2
14200202A20486B5200002A6FAE00F9012A6FBB55085FA86B0A5F985B12
04B034C0000A9FF60A20406F926FACAD0F905F985F985B1600000000000
0018A98065EB65EC65ED65El65DF38E5F0E5F1E5E2E5E0E5DEE5EFE5E3B
002A9004A18694065EC65ED38E5E44A18699065DD65DD65DD65DD65El38
E5E4E5E4E5E5E5E5E5E0A6B1E033F016E034F012E022F00EE025F00AA6B
0F009B450C01010031869024C7703

There you go: a fully functional Chess program, with AI.

~~~
Goladus
To actually run that on the PC I'm using right now, I would presumably need to
also download an emulator for the machine it was designed for, which is
presumably larger than 1K. Just saying.

------
nadam
I don't want to sound negative, but 'greatest program ever written' is really
an overstatement. He has created this program, because he needed to fit into
1k. If it was needed nowadays, hundreds of programmers could achieve this
result. (I would be so happy to code these kind of things at my working place
instead of the bloated overcompilcated softwares...)

If someone would create a prize (a prize like the netflix prize or the Hutter
prize) with $100k or something, to create the smallest chess program, I
suppose hundreds of contestants would fit into 1k. But the competition would
be high, and the winners would be probably very sophisitcated algorithms,
which might deserve the 'greatest program ever written' title. But without
competition it is easy to be the best.

~~~
sophacles
Isn't this like saying "Orville and Wilbur wright really didn't do anything
that great. Thousands of hobbiests could easily build an airplane that is
safer, faster and more maneuverable than the wright flyer, and in their
garages on weekends!".

It is the nature of tech to build on what was done before. It's hard to
remember that something that now might be laughable, was at one point
considered impossible.

~~~
tedunangst
I haven't read a blog post titled "The Wright brothers built the best plane
ever" recently.

------
kqr2
Link to the _Full ZX-81 Chess in 1K_ article by David Horne:

<http://users.ox.ac.uk/~uzdm0006/scans/1kchess/>

And here's a ZX-81 emulator to play the actual game:

[http://www.zx81stuff.org.uk/zx81/emulate.php?tzx=0%2F1KZXChe...](http://www.zx81stuff.org.uk/zx81/emulate.php?tzx=0%2F1KZXChess%28Artic%29.tzx.zip%400&title=White+queen+pawn+moved+from+1K+ZX+Chess)

------
sophacles
Totally off-topic: Does anyone else miss the kuro5hin peak days? I can't seem
to find any single community to capture the soul of what it was. HN and Reddit
are close, but something is lacking. Anyone know of such places?

~~~
sjs382
I don't think either comes close to capturing the spirit of K5. Remember the
"Dorm Wars" and "Fast Times at Phillips 66" serials?

I'd love to find a place like K5 again. Rusty killed it with the $5
registration fee for new accounts. The dupe accounts, multiple personalities,
and role playing were part of the charm.

~~~
strlen
To be fair, $5 registration fee came at the tail of a fairly long decline in
quality.

That being said, it was great at its peak. Combination of a blog community,
debating club and much more.

~~~
ubernostrum
I miss trhurler.

~~~
Perceval
You should have told him _not_ to take a hike then.

------
blintson
I'm writing a javascript chess game write now and I'm currently at 200 lines +
50 of HTML. The source code of mine is larger than the byte code of his
program. I am a moron.

~~~
moron4hire
source code can often be larger than byte code.

My first attempt at a javascript chess game was almost 7 years ago. 159 lines
of javascript and html. Unfortunately, it's nothing more than a pieces-on-
board sim. It doesn't do anything to control play or prevent cheating. It was
college, it was fun
<http://members.gamedev.net/capn_midnight/chess/chess.html>

------
iuguy
When I saw the ZX81 reference I thought surely he meant 3d monster maze by
Malcom Evans which was the first 3d game for a home computer (although it did
need 16k of RAM).

~~~
zandorg
Try 3d Deathchase - it's better than Doom!

------
diego_moita
I respect the acomplishment but I can think about a lot of other computer
systems/programs with way more cultural, economic and scientific impact than
this program.

E.g.: Unix, Java, Sabre, VisiCalc, Mars Rover and Hubble controlling software,
etc.

------
abdulhaq
C Chess program in less that 1200 chars:
<http://home.hccnet.nl/h.g.muller/max-src2.html>

~~~
niyazpk
I think we should compare the size of the assembly code generated for this
with the original.

Other than reducing the readability, I cannot think of any advantage offered
by the mere _compression_ of the source code.

------
rivo
I remember when I was six, my dad programmed a chess game on the Sinclar ZX81.
He used to play real-life chess a lot so naturally, this was his first use for
this little box. It was slow as hell and he had to pause screen rendering to
make it faster. If you call simple backtracking "AI" then I think my dad wrote
that "greatest program ever" too without knowing David Horne. (And my dad is
definitely no urban myth, either...)

~~~
charltones
Are you sure the ZX81 he was using didn't have a 16Kb ram pack attached? Most
people I know (me included) had one, since it was hard to do anything without
it.

~~~
dirtbox
Very true, but I had this game, it was one of the few that ran without the
rampak. I think I used the pak primarily for Bomber and 3D Monster Maze.

Good times!

------
stcredzero
_The greatest program ever written_

The Best HN Hyperbole. (For today!)

------
ThinkWriteMute
This page would never show up in a search engine that de-lists ad-sense pages.

------
NathanKP
The code referenced in the article is located at:

<http://users.ox.ac.uk/~uzdm0006/scans/1kchess/>

------
Dav3xor
I suppose the theoretical limit is 1 bit -- if you define the program in terms
of a virtual machine that takes 1 instruction -- "play a game of chess".

