Back

My first Z80 assembly language program

116 points3 yearsnanochess.org
zh33 years ago

Back in '78 or so, my first computer was a hand-assembled Nascom I (2MHz Z80)[0] with 1kb of RAM. No assemblers, no disk drives, not even a cassette tape. Just a keyboard and a simple ROM that let you load hex values in memory. The low-res video display was memory-mapped, so after much study of circuit diagrams,the Mostek Z80 manual and the ROM I got to understand memory addressing, where the screen was and how the hardware worked. So my first Z80 program was just 3 instructions entered as hex opcodes; all it did was splat an asterisk on the screen but what a buzz I got at the time :)

      ld a,2a    # Load ASCII '*' in to acc
      ld hl,d80  # Set HL to point to video-mapped RAM
      ld (hl),a  # Put character on screen.
Some years later I wrote one of the first multi-user games, picked up by British Telecom to run on their Prestel/Micronet network. By that time I've graduated to 4MHz Z80s, with home-made 2MB RAM disks and 256Kb of main RAM accessed with a crude TTL MMU; we got up to 64 users per Z80, albeit accessing the system via 1200/75 baud modems.

[0] It graced the cover of Issue #1 of Personal Computer World; I still have a copy.

Renaud3 years ago

We should have a site where these experiences are retained.

My teen years were in the 80s and I was mad about the Z80 in my cheap Laser 200 but was too isolated to have access to much information and equipment.

Yet, I reversed engineered the schematic of my little, made my own memory expansion (I couldn't afford the official one) out of stacked SRAM chips, mapped the ROM, build my own crude tools to see what was going on in the hardware, coded assembler by hand and poked the memory to store the programs, one byte at a time...

It taught me about electronics, fundamentals of gate logic and how microprocessors worked, programming (and no I wasn't ruined by BASIC), and those constant discoveries drove me to want to know more.

It was such a formative and fun experience for a curious teen to explore something in depth -with no help other than a few magazine and books- and uncovering the layers of what made a computer tick, however minimal that computer was.

varyherb3 years ago

> I was 9 years old and I wanted to write amazing games...After four years of writing games in BASIC language, I felt like I was stopped by the slowness inherent to an interpreted language.

Wasn't sure I was reading this correctly, so I navigated to the About Me page:

> Born in 1978, I started programming at age 5 in BASIC, learnt assembly language by age 9

kragen3 years ago

I was in a similar situation, and I think the slowness inherent to an interpreted language was a big frustration to all of us kids programming in BASIC in the 01980s.

Unfortunately I didn't have the guidance and direction Oscar Toledo G. had—my father did what he could, but he's just not an educator, and he's also not the same level of wizard as his father Oscar Toledo E.—so I didn't learn assembly until I was about 13, and then I learned from a book. (Also, I suspect Oscar Toledo G. has more innate aptitude than I do, but I think that in this case my lack of practice and feedback is what was holding me back.) I didn't write my first playable game until I was 29, which was http://canonical.org/~kragen/sw/inexorable-misc/tetris. (I wrote some games earlier, but they were just unplayably bad.)

gzalo3 years ago

This guy's family is quite odd: http://www.biyubi.com/eng_principal.html

They designed their own hardware, operating systems and utilities back in the 70s, and they seem to really like optimizing software so they must run quite fast.

But they say they have been "silenced" by big corporations, and that the mexican government doesn't like them and that's why they haven't been able to sell their computers.

I wish that they would release most of their hardware/software with a free license. I imagine their stuff can be used for educational purposes.

thom3 years ago

Always like seeing Oscar's site pop up here, so that I can tell people to check out the lovely annotated source code for Toledo Nanochess, his IOCCC-winning tiny chess engine:

https://nanochess.org/chess3.html#book

tyingq3 years ago

If you download his MSX rom, from https://nanochess.org/archive/karate2.zip and then unzip, and extract the karate2_msx.rom file, you can play it online.

https://webmsx.org/ is an online MSX emulator. Just click the "load files" link and give it the ROM file. Space bar and arrow keys for controls.

JKCalhoun3 years ago

Yes, I found it is simplest to drag the 'karate2_msx.rom' to your browser and select 'Cartidge 1' from the popup.

canada_dry3 years ago

I have a very similar stack of lined graph paper with my Z80 'trek' game that I developed when I was ~15yo. I found this book [i] to be a fantastic resource - it taught me so much about microprocessor and computer architecture.

[i] https://archive.org/details/programmingz8000zaks

elvis703 years ago

The author has made his book freely available : http://www.z80.info/zaks.html

mark-r3 years ago

I'm really happy to see I wasn't the only one who got their programming start on graph paper. Lost all my old sources though.

JoeAltmaier3 years ago

My first 8080 game was 'fly', where an X would flit around the screen and turn 45 degrees left when you hit the space bar. You tried to keep it on the screen and not touching some obstacle (a wall that extended down the top about 1/3 the way across?). Pointless but fun to write!

Schiphol3 years ago

I have a question for people reading this article. Where did you get ahold of the relevant docs, back in the 80s? I remember being dimly aware that there was something called 'machine code' which was like BASIC but much faster. But that was it.

Doctor_Fegg3 years ago

The Sinclair ZX81 manual had the full list of Z80 opcodes in it - Appendix A in http://zxnext.narod.ru/manuals/ZX81_Manual.pdf . So with that you could start PEEKing and POKEing and experimenting.

I never really did any Z80 coding on the ZX81 itself, but that section of the manual came in really useful when I got an Amstrad CPC, which is what I learned Z80 on. To that I added The Ins & Outs of the Amstrad (https://cpcrulez.fr/book_english-the_ins_and_outs_of_the_ams...), which documented the firmware and other CPC-specific stuff.

pmontra3 years ago

Not the author but I bought a book about assembler for my Sinclair ZX81. I just discovered it's got a Wikipedia page https://en.wikipedia.org/wiki/Programming_the_Z80

Edit: wow, see this comment of elvis70

> The author has made his book freely available : http://www.z80.info/zaks.html

Schiphol3 years ago

Thanks! I might just learn it, 35 years after the fact :)

bernds743 years ago

In Germany we had specialized magazines, and I'm sure there were similar things in most countries. I bought a CPC664 in 1985, and there was a magazine called CPC International. It had articles about all sorts of things - programming languages or documentation for chips like the CRTC. There were also program listings you could type in; I believe I got an assembler that way. Another magazine had a BASIC compiler. Similar things existed later in the Amiga days.

There were also books. One of my favourites was an annotated disassembly of the CPC's two major ROMs. 16KB basic operating system, and 16KB BASIC interpreter.

Renaud3 years ago

Depending on where you lived there were some electronic magazines available. I remember dreaming about articles in Elektor and other national magazines like that.

Libraries and book stores had a few books, especially if you lived in a university area.

Data sheets for the processor and co-processors and digital logic components were usually available in electronic stores (they also had some books).

But it's hard to imagine this world now that we have the Internet. I miss the focus needed to hunt down information and understand how thing worked. A lot of it was trial and error, trying to validate your understanding with experimentation.

jdswain3 years ago

I had an older student at my high school teach a machine language course, complete with a one page table of opcodes and their hex values so that we could type them in.

After that I purchased a copy of Programming the 6502 by Rodnay Zaks. Books took 3 months to arrive if they were a special order like that, sometimes longer. To this day it's still one of my favourite books.

awwaiid3 years ago

IIRC my commodore 64/128 manual came with BASIC and some assembly documentation. Different world when your turn on the machine and ... Get basically a REPL.

I did the same thing as the author, wrote BASIC until I needed a faster inner loop (doing some simulation like cellwar or something) and picked up asm to make it (just the inner loop) faster.

tyingq3 years ago

For me, mostly from hobbyist magazines that led to BBS dial up numbers that had made copies of software and docs. Though that was tricky, since long distance calls were expensive. So you dealt mostly with whatever a local BBS had, which I assume varied in depth, breadth, quality, etc.

pjmlp3 years ago

INPUT magazine series, books with programming listing either at the local libraries or some bookstores, local computer club, in Portugal the "A Capital" newspaper informatics section on Friday's issue or the Spooler magazine.

jacquesm3 years ago

Leventhal's CPU series was pretty good, also every manufacturer makes reference manuals for their instruction sets. I still have mine (pretty dog eared, just for sentimental value, unlikely to ever be used again).

elvis703 years ago

Personally, it was books!

mgkimsal3 years ago

Extremely impressive. I had a ZX81 when I was around his age, and was dimly aware of 'machine code', but couldn't quite get my head around doing anything substantial with it - BASIC was where I mostly stayed. Moved to a 128, then later the PC world (a Sanyo 550 - almost PC compatible!), and mostly stuck with BASIC in those in the 80s. I did get in to some assembly stuff on the C128 side of things, as it came with a built-in assembler.

I understood assembly basics, but didn't do much productive with it. I could move some boxes around the screen via keys, and I did do a bit with the '80 col' monochrome mode trying to do some custom sprite stuff (learned a lot about XOR) - but that's about as far as I got by my mid teens.

Reading stories like the OP make me feel inadequate...

jkh13 years ago

This brings back memories. My first Z80 assembly program was a jetpac game for the Sinclair ZX80 around 1984 because I couldn't get a ZX Spectrum to play it. I don't think any written copy still exists but there's still a copy on a cassette tape.

KingOfCoders3 years ago

If you do Z80, best book (one of the books I love most)

"Z80 Assembly Language Subroutines"

_joel3 years ago

Kudos, I think this trumps me writing my name in a goto loop in qbasic that I did when I was 9. Still the sense of accomplishment dictated the next 30 years of my life so not bad I guess :)

the_af3 years ago

His whole website is pretty cool and filled with retro gaming goodness. Thanks for sharing!

KingOfCoders3 years ago

My first larger code was the towers of Hanoi on CPC, where I found the algorithm while experimenting with coins of different sizes, somewhere around 12. Still very proud that I found a solution algorithm.