Leanpub Header

Skip to main content

Machine Code Mayhem

Writing ZX Spectrum Games in Z80 Assembly, from Six Complete Games

Machine Code Mayhem
This book is 80% completeLast updated on 2026-11-01

Learn Z80 assembly and ZX Spectrum game development by taking six complete, original 128K games apart line by line: the machine, the language, the engine every game shares, one signature technique per game, and how to test, playtest and ship a machine-code game. For programmers who know a modern language and have never written assembly.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

The ZX Spectrum had 48 kilobytes of memory, a processor running at three and a half megahertz and a screen that allowed two colours in every eight-by-eight block of pixels. People wrote thousands of games for it anyway, in machine code, and the best of them still feel astonishing. *Machine Code Mayhem* shows a modern programmer how it is done, by taking six complete, original Spectrum games apart line by line.

You will steer an electric mower round a lawn with fixed-point maths and a cosine table, catch falling letters and check them against a dictionary packed into a few kilobytes, fly through a space station where gravity has turned upside down, scroll a jungle on the 128K's hidden second screen, run a 1987 software studio on tea, and lift weights in rhythm at a gym for men over 35. Each game teaches one big idea, and every line of code in the book is copied from a game that builds, loads from tape and plays. A script checks every extract against the games' source.

The book assumes you can program in a modern language and have never written assembly. Part I explains the machine and the Z80 in terms you already know. Part II builds the engine every game shares: the interrupt, the frame loop, movement, flicker-free drawing and sound. Part III spends a chapter on each game's signature technique. Part IV covers what period books never did: testing a machine-code game with recipes and bots, playtesting it, keeping a log of decisions, and shipping it on a tape with a loading screen. You finish with a roadmap for writing your own.

The six games come from Dalex Software, a fictional 1987 Manchester software house, and its staff tell the story chapter by chapter: Gareth on the Z80, Hyun-joo on sprites, Dez on the AY sound chip, Vik on testing and Emeka on playtesting. Every chapter has learning objectives, real code, a summary and hands-on exercises for an emulator.

Written by Daniel Vaughan, who made the six games through his Dalex Studio pipeline, in which AI coding agents write, test and playtest original ZX Spectrum games against written specifications.

Share this book

Author

About the Author

Daniel Vaughan

Daniel Vaughan is a technology leader and software architect based in the United Kingdom, specialising in agentic AI. He has spent approaching thirty years across enterprise, startup, and academic settings, with a career-long focus on engineering quality and developer productivity. His work now centres on the shift where AI stops being an experiment and becomes core to how organisations build software.

Daniel is Head of Forward Deployed Engineering at HCLTech AI Labs, where he leads a global practice embedding engineers and coding agents inside complex enterprise environments to take production AI from prototype to live systems. He built the practice from the ground up, running lean regional pods in which a shared architect, a small number of engineers, and a team of agents working through tools such as Codex, Claude Code, Cursor, and GitHub Copilot deliver at the output of a much larger team.

Before HCLTech AI Labs, Daniel was director of software engineering at Mastercard in London, leading cloud strategy and architecture for real-time payment products in a highly regulated financial services environment. Earlier, he spent eight years at the European Bioinformatics Institute in Cambridge, moving from software engineer into engineering leadership, working on the same problems of software quality and developer productivity that he now solves with agentic tooling.

He is the author of Cloud Native Development with Google Cloud (O'Reilly, 2024) and Ext GWT 2.0: Beginner's Guide (Packt, 2010), a Google Developer Expert, and a Green Software Champion. He writes about agentic engineering and AI-assisted development at blog.danielvaughan.com.

The Leanpub Podcast

Podcast

Podcast Episode

Contents

Table of Contents

Copyright

About the Author

About the Team

Introduction

  1. Six games
  2. Who this book is for
  3. How the book is organised
  4. How each chapter works
  5. What you need
  6. How the games were made
  7. Conventions

Part I. The Machine and the Language

Chapter 1. Why Write Spectrum Games Now

  1. Learning Objectives
  2. What a home computer was
  3. A machine you can hold in your head
  4. What a game does every update
  5. Budgets
  6. The constraint is the game
  7. Six games, six lessons
  8. How the games were made
  9. Setting up your workbench
  10. Reading this book
  11. What you can now do
  12. Summary
  13. Exercises

Chapter 2. The Machine on Your Desk

  1. Learning Objectives
  2. Memory as numbered boxes
  3. Bits and bytes
  4. Hexadecimal
  5. By hand: a row of the Weed
  6. Addresses
  7. A map of 64 KB
  8. The screen as memory
  9. The screen’s strange layout
  10. Attributes: colour by the cell
  11. Clock ticks: the T-state
  12. Frames and T-states
  13. Contended memory
  14. Ports: talking to the ULA
  15. The interrupt in brief
  16. Banks and port $7FFD
  17. The AY-3-8912
  18. What you can now do
  19. Summary
  20. Exercises

Chapter 3. Z80 Assembly for Modern Programmers

  1. Learning Objectives
  2. What a processor does
  3. Machine code
  4. Python has bytecode too
  5. A different kind of program
  6. Registers: the processor’s hands
  7. Reading a line of assembly
  8. Moving data with ld
  9. The flags
  10. One instruction at a time
  11. Deciding with cp and jumps
  12. A whole function: absdiff
  13. The stack, call and ret
  14. Negative numbers and two’s complement
  15. Sixteen bits at a time
  16. Multiplying by shifting
  17. Bits
  18. Records and indexes with IX
  19. A jump table built from ret
  20. The second register set
  21. Talking to hardware with in and out
  22. What instructions cost
  23. What you can now do
  24. Summary
  25. Exercises

Chapter 4. From Text to Tape

  1. Learning Objectives
  2. What an assembler does
  3. Assembling two lines by hand
  4. Names and the symbol table
  5. Running sjasmplus
  6. The symbol file and the listing
  7. Directives
  8. Saving memory and banks
  9. The cost of ALIGN
  10. One source, two machines
  11. build.yaml and build_game.py
  12. Checking the memory map
  13. Running verify
  14. The tape format
  15. The BASIC loader
  16. The packed loader
  17. Loading it in an emulator
  18. Your own game from the template
  19. What you can now do
  20. Summary
  21. Exercises

Part II. The Engine Room

Chapter 5. The Frame Loop and the Interrupt

  1. Learning Objectives
  2. A doorbell while you cook
  3. A steady beat
  4. Six frames on paper
  5. Pointing the interrupt at your code
  6. What the interrupt does every frame
  7. Why every register is saved
  8. What the interrupt costs
  9. Living below $C000
  10. Latching keys
  11. Joysticks in the interrupt
  12. The main loop and its catch-up debt
  13. The contract, and where the game misses it
  14. What you can now do
  15. Summary
  16. Exercises

Chapter 6. Movement, Fixed Point and Collision

  1. Learning Objectives
  2. Moving by part of a square
  3. Fixed point: fractions in disguise
  4. Angles as bytes
  5. Rates that are not whole numbers
  6. A quarter of a cosine
  7. Speeding up and slowing down
  8. The pixel walk
  9. Row assist
  10. Boxes and the no-harm rule
  11. Mowing, the Battery and the random Lawn
  12. What you can now do
  13. Summary
  14. Exercises

Chapter 7. Drawing Without Flicker

  1. Learning Objectives
  2. What a sprite is
  3. Drawing the Mower on squared paper
  4. From paper to defb
  5. Putting the rows on the screen
  6. A second frame
  7. Masks
  8. Shifting to any pixel
  9. The unrolled chain
  10. Copying rectangles fast
  11. Why drawing on the screen flickers
  12. Three copies of the picture
  13. Compose: drawing only what changed
  14. When the lawn changes
  15. Present first, top to bottom
  16. Tinting the actors
  17. Whole screens and cards
  18. How verify measures flicker
  19. What you can now do
  20. Summary
  21. Exercises

Chapter 8. Music and Sound on the AY

  1. Learning Objectives
  2. What a sound is
  3. A chip that keeps playing
  4. A note as a number
  5. Music as a list of notes
  6. Patterns and the order list
  7. One frame of registers
  8. Playing from the interrupt
  9. Effects on channel C
  10. Giving channel C back
  11. The hum that masked the music
  12. The music toggle
  13. Effects off and the chord on channel C
  14. Proving the songs
  15. The beeper on the 48K
  16. What you can now do
  17. Summary
  18. Exercises

Part III. Six Games, Six Lessons

Chapter 9. Wordris: A Dictionary in a Few Kilobytes

  1. Learning Objectives
  2. The well on squared paper
  3. The well as a byte grid
  4. Finding a word in the well
  5. Why not a list of words
  6. A trie on paper
  7. From paper to packed bits
  8. The trie in bank 4
  9. Walking the trie in code
  10. What it costs
  11. Paging without tears
  12. Falling, locking and the soft-drop surge
  13. Spreading a burst over frames
  14. Scoring the winner
  15. Drawing letters as tiles
  16. What you can now do
  17. Summary
  18. Exercises

Chapter 10. Antigravity: Physics with Whole Numbers

  1. Learning Objectives
  2. Speed, and speed changing
  3. Ten updates with a pencil
  4. Sixteenths of a pixel
  5. Gravity that points up
  6. A clamp made from a subtraction
  7. Inertia and the brake
  8. Integration, and the frame behind
  9. Predicting a flight
  10. Boxes after the move
  11. One compare for a range
  12. Leaving early through the stack
  13. Ten sectors in 23 bytes each
  14. Air, score and grade
  15. What you can now do
  16. Summary
  17. Exercises

Chapter 11. Tiger Gorilla: Scrolling a Jungle

  1. Learning Objectives
  2. A window on a long picture
  3. By hand: a strip under a card
  4. Why redrawing is slow
  5. Tiles in planar pages
  6. Two screens, one of them awkward
  7. A budget of four frames
  8. The half-cell camera
  9. By hand: eight steps of walking
  10. The camera’s step rule
  11. Building a half cell
  12. Colour at the half step
  13. The black ground that flashed
  14. Correcting the colour
  15. Paying for the fix
  16. Vines and swinging
  17. The crocodile below row 128
  18. Choosing where the clash goes
  19. What you can now do
  20. Summary
  21. Exercises

Chapter 12. Gold Master: Rooms, People and Words

  1. Learning Objectives
  2. A game made of tables
  3. On paper first
  4. An office in a table
  5. Rooms and exits
  6. A route table instead of a search
  7. Walls as bits
  8. Wants as bits
  9. The tea economy
  10. Percy’s routine
  11. Touch, the rule that took longest
  12. Words on the screen
  13. Where it all lives
  14. What you can now do
  15. Summary
  16. Exercises

Chapter 13. WOLF: Timing, Waggling and Scripted Characters

  1. Learning Objectives
  2. Rhythm in frames
  3. Counting your own rhythm
  4. A gym on a 4-frame update
  5. Why the update cannot count the pump
  6. What fair means
  7. Reading the pump in the interrupt
  8. Accepting a press
  9. Smoothing the gap
  10. The form meter
  11. The comfy rhythm that failed
  12. Beats on a stamp ring
  13. Characters that follow a script
  14. Coaches as bytecode
  15. The buzzer’s restart bit
  16. One coach slot, three coaches
  17. Speech bubbles made of map tiles
  18. Calling code in another bank
  19. What you can now do
  20. Summary
  21. Exercises

Part IV. Making It Good

Chapter 14. Testing a Machine-Code Game

  1. Learning Objectives
  2. Why a game needs automatic tests
  3. What makes a test repeatable
  4. A recipe by hand
  5. The harness contract
  6. Deterministic by design
  7. The interrupt that changed the letters
  8. Recipes: tests as data
  9. A bot that plays
  10. Keeping bot tests honest
  11. Custom checks
  12. What verify checks
  13. Reading a real report
  14. Testing from the middle of an interval
  15. What verify cannot see
  16. What you can now do
  17. Summary
  18. Exercises

Chapter 15. Playtesting, Triage and Keeping a Decisions Log

  1. Learning Objectives
  2. What players find
  3. A finding and its grade
  4. Grading three findings by hand
  5. Playing at reduced speed
  6. The charter
  7. The report
  8. Tone rules
  9. From a vague report to numbers
  10. Triage: fix, defer or by design
  11. The decisions log
  12. The gate
  13. The outside view
  14. What you can now do
  15. Summary
  16. Exercises

Chapter 16. Shipping: Tapes, Loading Screens and the Box

  1. Learning Objectives
  2. Bytes as sound
  3. The arithmetic of one byte
  4. Estimating a real tape by hand
  5. The tape writer’s sum
  6. Where the minutes go
  7. The packed headerless loader
  8. The block table
  9. Unpacking as it lands
  10. What the game may not assume
  11. Loading screens
  12. The Dalex house style
  13. The box: inlay, advert and review
  14. The load video and the web demo
  15. What you can now do
  16. Summary
  17. Exercises

Chapter 17. Your First Game

  1. Learning Objectives
  2. A tiny idea
  3. What a one-page spec holds
  4. Writing a spec by hand
  5. Milestone 1 as a whole tiny game
  6. The 128K or the 48K
  7. A budget in T-states
  8. Choosing the update rate
  9. Starting from the template
  10. Keeping the harness contract
  11. Recipes for every boundary
  12. Verify, playtest, gate
  13. Mistakes the six games made
  14. What six games prove
  15. What you can now do
  16. Summary
  17. Exercises

Conclusion

  1. What you can do now
  2. What the checks did not cover
  3. What to do next
  4. The machine that rewards curiosity

Appendix A. Z80 Instruction Reference

  1. Moving data
  2. Arithmetic
  3. Logic, bits and shifts
  4. Jumps, calls and loops
  5. Input, output and the processor
  6. The flags

Glossary

Bibliography

  1. Manuals and data sheets
  2. Reference pages
  3. Tools
  4. Books from the period
  5. The games

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub