The Number That Ends Donkey Kong
Donkey Kong has no ending. There is no final cutscene, no credits, no congratulations. If you are good enough to survive 116 screens of barrels, fireballs and bouncing springs, the game simply kills you on the 117th. Mario walks out onto the first girder of level 22, takes a few steps, and falls over dead. Then he does it again with your next life, and the next, until the game is over.
Players call it the kill screen, and the 2007 documentary The King of Kong made it famous. What makes it worth a closer look is that nobody designed it. It is the result of one small calculation in the original arcade code, a calculation that works perfectly for 21 levels and then quietly breaks.
This article walks through that calculation in the original 1981 program, the code that ran on the arcade boards.
The bonus box
Every screen in Donkey Kong starts with a number in a box at the top right, labelled BONUS. It counts down while you play. Reach the top and you score whatever is left. Let it reach zero and Mario dies.
The starting value goes up as you progress:
| Level | Bonus |
|---|---|
| 1 | 5000 |
| 2 | 6000 |
| 3 | 7000 |
| 4 and above | 8000 |
That pattern is the whole story, once you see how the game computes it.
The formula
The bonus is stored in hundreds, so 5000 is kept as 50. When a new screen starts, the game works out that number from the level with a formula that amounts to:
bonus = level × 10 + 40, but no more than 80
Level 1 gives 50, level 2 gives 60, level 3 gives 70, level 4 gives 80, and from level 5 on the result would be 90, 100, 110 and so on, so the cap holds it at 80. That is exactly the table above.
The routine that does this sits at address $0F7A in the program ROM and is
fourteen instructions long. The Z80 processor in the cabinet has no multiply
instruction, so the code builds "times ten" out of pieces: it doubles the level
three times to get eight times the level, then adds the level twice more.
ld a,(level) ; the level number, kept at $6229
ld b,a
... ; double it three times: A = level x 8
add a,b ; level x 9
add a,b ; level x 10
add a,$28 ; + 40
cp $51 ; 81 or more?
jr c,done ; no: keep it
ld a,$50 ; yes: cap at 80
Nothing looks wrong. The cap is there. The arithmetic is right. The problem is the size of the box the arithmetic happens in.
A number that wraps around
All of this happens in the Z80's A register, which holds a single byte: a number from 0 to 255. There is no 256. Add 1 to 255 and you get 0, the way a car's odometer rolls over from 99999 to 00000.
For levels 1 through 21 the formula never gets near the edge. Level 21 gives 21 × 10 + 40 = 250, which fits, and the cap turns it into 80.
Level 22 gives 22 × 10 + 40 = 260. That does not fit in a byte. It wraps around past 255 and lands on 260 − 256 = 4.
Now the cap check runs. It asks one question: is this number 81 or more? The number is 4. So the answer is no, and 4 goes straight through as the bonus for level 22. The cap was meant to stop the bonus from growing past 8000, and it does that job for every level from 5 to 21. It was never asked to notice that the number had already overflowed.
A bonus of 4 means 400 points instead of 8000. That alone would make level 22 brutally hard. What actually happens is stranger.
What the box says
The bonus box does not show the internal number directly. Another routine,
at $063A, converts it into the two decimal digits the screen draws. It does
that by counting how many times it can subtract 10 before reaching exactly
zero. For 50, that is five subtractions, so the first digit is 5. Every
legitimate bonus is a multiple of ten, so this always works.
Four is not a multiple of ten. Subtract 10 from 4 and the byte wraps around to 250. Keep subtracting and it keeps wrapping, until on the 26th subtraction it happens to land exactly on zero. Twenty-six is far too big for one decimal digit. The routine stuffs it into the digit slots anyway, and the first digit comes out as a value the screen has no number for.
On a real Donkey Kong screen, that digit draws as a blank. So level 22 opens with a bonus box that reads 100.
Then Donkey Kong throws his first barrel, and the box jumps to 4000.
That is the display's countdown meeting the garbage it was handed. Every throw subtracts one from the displayed digits using the processor's decimal arithmetic, and the decimal correction step turns the broken value into 40. From there the box counts down as if nothing were wrong: 3900, 3800, 3700. The number on screen and the number the game is actually counting have nothing to do with each other any more.
Four barrels
Here is the detail that turns a low bonus into a death sentence. On the barrel
screens, the bonus does not count down with time. It counts barrels. Every time
Donkey Kong throws one, the routine at $2CDA takes one off the hidden counter.
With a counter of 4, the bonus is gone after four barrels. The first one comes out almost immediately, since every screen opens with a throw. The game then waits 256 frames, a little over four seconds, as a grace period. When the grace period ends, it kills Mario the moment his feet are on the ground. It never kills him in the middle of a jump; a jump only postpones the end until he lands.
Four barrels plus four seconds is nowhere near enough time to climb the screen.
Nine seconds of level 22
Everything above comes from reading the program, and published disassemblies have been wrong before. So the original program was run under a test script that started a game, wrote 22 into the level number during the opening animation, and logged the bonus values every frame. A second run also removed every rolling barrel and fireball as soon as it appeared, leaving Donkey Kong's throws, which drive the countdown, alone, so the timer was the only thing that could kill Mario.
This is what happened, counted in frames from the moment Mario could move (the game runs at about 60 frames a second):
| Frame | What happened | Hidden counter | Box shows |
|---|---|---|---|
| 0 | Play starts | 4 | 100 |
| 2 | First barrel | 3 | 4000 |
| 28 | Second barrel | 2 | 3900 |
| 125 | Third barrel | 1 | 3800 |
| 298 | Fourth barrel; time is up | 0 | 3700 |
| 554 | Grace period over, 256 frames later | 0 | 3700 |
| 555 | Mario dies | 0 | 3700 |
Every prediction held: the starting value of 4, the box reading 100, the jump to 4000, one tick per barrel, and exactly 256 frames of grace before the end. The whole screen lasted about nine seconds. On the next life the fourth barrel came sooner, because Donkey Kong's throws are paced by a random roll, and the screen lasted a little over eight.
Could it have been fixed?
Easily. Don Hodges, who wrote the best-known analysis of the bug, points out that the formula only needed its cap applied to the level instead of the result: treat any level above 4 as 4, then multiply. The answer is always 80, nothing ever overflows, and the patch takes the same number of bytes. His article includes it as a MAME cheat, and according to Hodges the game then plays on past level 22.
But the original boards never got that fix, and that is how the kill screen became part of the game's legend. It is not a wall the designers built. It is a byte that was asked to hold the number 260.
Credits and further reading
- Don Hodges, "How High Can You Get?", the classic write-up of this bug and its fix. Hodges credits Jeff Kulczycki with first working out the formula.
- Kef Schecter and contributors, furrykef/dkdasm, the annotated disassembly of Donkey Kong that the addresses here were first found in.