← All articles

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:

LevelBonus
15000
26000
37000
4 and above8000

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.

The bonus calculation on a one-byte dial A dial numbered 0 to 255. Level 21 computes 250, which lands in the capped zone and becomes 80. Level 22 computes 260, which rolls past 255 to 4, below the cap, so 4 is used. 0 64 128 192 81 ONE BYTE 0 TO 255 LEVEL 21 250 81 or more, capped to 80 LEVEL 22 260 → 4 rolls past 255, under 81: kept 0–80 used as is 81–255 capped
The bonus sum on a one-byte dial. Level 21's 250 lands in the capped zone; level 22's 260 rolls past 255 and lands on 4, below the point where the cap kicks in.

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.

What the bonus box shows versus what the game counts Across the start and four barrel throws, the bonus box shows 100, 4000, 3900, 3800, 3700, while the hidden counter goes 4, 3, 2, 1, 0. BOX SHOWS GAME COUNTS START 100 4 BARREL 1 4000 3 BARREL 2 3900 2 BARREL 3 3800 1 BARREL 4 3700 0
What the box shows against the number the game actually counts, barrel by barrel.

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):

FrameWhat happenedHidden counterBox shows
0Play starts4100
2First barrel34000
28Second barrel23900
125Third barrel13800
298Fourth barrel; time is up03700
554Grace period over, 256 frames later03700
555Mario dies03700
The kill screen, frame by frame Barrels are thrown at frames 2, 28, 125 and 298. The 256-frame grace period runs from 298 to 554, and Mario dies at frame 555, about nine seconds after play starts. 0 200 400 FRAMES (60 A SECOND) GRACE: 256 1 2 3 4 BARRELS THROWN DIES AT 555 About nine seconds in all.
The same run on a timeline: four barrels, then 256 frames of grace.

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.