Zero Labs
Industry News / AI

I Wrote a Game in Machine Code, and It Fits in About 300 Bytes

Source · Towards AI By Yashraj Behera

From the publisher’s feed, collected in the ZeroLabs newsroom.

People love to say someone “wrote a game in pure binary,” in ones and zeros, by hand. It’s a great image, and it’s almost entirely a myth. Nobody types raw bits to make a game, because a single flipped one out of thousands would break everything with no way to find it. But there’s a real, honest version of that legend, writing a game in assembly, the human-readable layer that sits directly on top of the machine code, and then looking at the actual bytes the processor runs. So I wrote a small guessing game that way, assembled it into about 300 bytes of machine code, played it, and pulled out the real binary to show you. Here’s what “a game in binary” actually means.The phrase sounds heroic. Someone, usually in a story told with a bit of awe, “wrote the whole thing in binary,” in raw ones and zeros, no language, just bits. It is the programming equivalent of building a house with your bare hands. And it’s worth being honest right away, that’s essentially a myth, at least as a description of how anyone actually works.Here’s why. A real program is thousands upon thousands of bits, and every one of them has to be exactly right. Flip a single one and the program doesn’t politely tell you what went wrong, it crashes, or worse, quietly misbehaves, and you’re left staring at an ocean of ones and zeros with no way to tell which bit betrayed you. There’s no structure to read, no names, no hints. Doing that by hand for anything as complex as a game is not a feat of skill so much as a feat of self-punishment, and it’s not how even the most hardcore low-level programmers spend their time.But there’s a real thing underneath the myth, and it’s genuinely close to the metal, and you can actually do it. It’s called assembly, and it’s the honest version of “writing a game in binary.” So I wrote one, a small number-guessing game, in assembly, turned it into real machine code, played it to make sure it worked, and then pulled out the actual bytes the processor executes so you can see them. That’s what this piece is, the true version of the legend, with the real binary on display.What machine code and assembly actually areTo see what I built, you need the two layers just above the bits, and they’re simpler than they sound.At the very bottom, the thing your processor actually runs is machine code, which really is just bytes, numbers, that the chip interprets as instructions. A certain byte means “add these two things,” another means “jump to this location,” another means “copy this value here.” This is the ones and zeros the myth is about, and it’s genuinely what executes. The catch is only that writing it directly, byte by byte, is inhuman, for the reasons above.One thin layer up is assembly. Assembly is a human-readable name for each of those machine instructions. Instead of writing the byte, you write a short mnemonic like mov for move, add for add, cmp for compare, jmp for jump. Every one of those corresponds almost exactly to a machine instruction, so assembly isn't hiding the machine from you the way a normal programming language does, it's just giving the raw instructions names you can read. Then a tool called an assembler translates your mnemonics into the actual bytes. You write the readable version, and you get the binary out the other end, byte for byte predictable.So “writing a game in binary,” done honestly, means writing in assembly, one readable line per machine instruction, and then assembling that into the real machine code. You’re working at the level of individual processor instructions, which is as close to the bare metal as anyone realistically gets. That’s exactly what I did.The gameI kept the game simple on purpose, because the interesting part is the level it’s written at, not the gameplay. It is the classic number-guessing game. The program picks a secret number from 1 to 100, you type a guess, and it tells you whether the real number is higher or lower, until you get it, at which point it says you got it and exits.Simple as it is, it needs every basic ingredient of a real program. It has to generate a somewhat random number. It has to print text to the screen. It has to read what you type. It has to turn the characters you typed into an actual number. It has to compare two numbers and make a decision based on the result. And it has to loop, doing this over and over until you win. That’s genuine program structure, input, output, arithmetic, comparison, branching, and a loop, just written at the instruction level.And here’s the thing that makes the exercise land, the entire game, once assembled, is about 300 bytes of machine code. Three hundred numbers. That’s the whole playable thing.Writing it at the instruction levelTo give you a feel for what the code actually looks like, here is the part that prints the opening line. In assembly, asking the operating system to write text to the screen is done by filling a few processor registers, small named slots the CPU works with, with specific values and then triggering a system call, which is how a program asks the operating system to do something for it.mov rax, 1 ; 1 is the code for "write"mov rdi, 1 ; 1 means "to the screen"mov rsi, banner ; the address of the textmov rdx, banner_len ; how many characterssyscall ; ask the OS to do itEvery line is one machine instruction. The first three load values into registers, one says which operation we want, one says where to send the output, one points at the text, one gives its length. Then syscall hands control to the operating system to actually perform the write. This is the level you work at, there's no "print" command handed to you, you arrange the request yourself and ask the system to carry it out.The heart of the game is even more revealing, because it’s where the decision happens. After turning your typed guess into a number held in a register, the whole game logic comes down to a comparison and some jumps.cmp r8, rax ; compare your guess to the secretje correct ; if equal, jump to the winjl low ; if less, jump to "too low"; otherwise fall through to "too high"That’s the entire brain of the game. cmp compares your guess to the secret number. je means "jump if equal," sending the program to the win message. jl means "jump if less," sending it to the "too low" branch. If neither jump happens, the program simply continues into the "too high" case. Every decision a program makes, at the very bottom, looks like this, compare two numbers, then jump somewhere based on the result. What we call logic is, down here, comparison and jumping.The actual binaryNow the payoff, the part the myth is really about. Once I assembled that code, it became machine code, real bytes the processor runs. And you can look at them directly, each assembly instruction sitting next to the exact bytes it turned into. Here is the opening of the program, shown three ways at once, the location, the raw bytes, and the instruction they represent.address bytes instruction401000: b8 60 00 00 00 mov eax, 0x6040100f: 48 31 f6 xor rsi, rsi401012: 0f 05 syscall401032: b8 01 00 00 00 mov eax, 0x1Look at that first line. The instruction mov eax, 0x60, which loads the number 96 into a register, is quite literally the five bytes b8 60 00 00 00. The b8 is the part that means "load a value into this particular register," and the 60 is the value, 96, written in hexadecimal. The instruction and the bytes are the same thing, viewed at two levels. And syscall, the instruction that calls into the operating system, is just the two bytes 0f 05. That's the ones and zeros of the legend, sitting right there, except we got to them by writing readable instructions instead of typing bits and praying.This is what “a game in binary” honestly looks like. Not a person hand-toggling thousands of bits, but a person writing at the instruction level, one readable line per machine operation, and then being able to see and verify the exact bytes that resulted. The bytes are real and the processor runs them directly. The readability is the only thing we added, and it is the only reason the task is possible at all.The actual ones and zerosNow, those hex bytes like b8 are still a convenience. Hexadecimal is just a compact way of writing a byte, and a byte is really eight bits, eight actual ones and zeros. So to finally deliver the literal thing the legend promises, here are real instructions from the game written all the way down as the ones and zeros the processor truly sees. This is not a re-creation, it is the actual machine code of the game I built, in binary.mov eax, 0x60 (load a value, the start of picking the secret number) hex b8 60 00 00 00 binary 10111000 01100000 00000000 00000000 00000000syscall (hand control to the operating system) hex 0f 05 binary 00001111 00000101cmp r8, rax (compare your guess to the secret, the heart of the game) hex 49 39 c0 binary 01001001 00111001 11000000je correct (jump to the win if they match) hex 74 42 binary 01110100 01000010There they are, the actual ones and zeros. And this is exactly where the myth falls apart the moment you look at it honestly. That single instruction to load a value, mov eax, 0x60, is already forty bits, 10111000 01100000 and so on. The whole game is around 300 bytes, which is roughly 2,400 of these ones and zeros in a row. Reading that, let alone writing it by hand and getting every single bit right, is precisely the madness we ruled out at the start. One byte in three forms makes the point, the value b8 is the decimal number 184 is the binary 10111000, all the same thing, and only the readable name mov makes it something a person can actually work with.So yes, the game really is ones and zeros underneath, and here they are. But you get to them by writing readable instructions and letting the assembler produce the bits, not by typing 10111000 two thousand times and praying you never slip.Does it actually runIt does, and I made sure. After assembling and linking it into a real executable, I played it by letting a simple strategy guess against it. The program picked its secret number, and a binary search closed in, fifty is too low, seventy-five is too high, sixty-two is too low, sixty-eight is too high, sixty-five is correct. Five guesses, and the win branch fired exactly as written. Every path works, the too-low message, the too-high message, and the victory. It is a small game, but it is a real, playable one, and it’s running as pure machine code with no programming language sitting underneath it at runtime, just the bytes and the processor.The whole thing, source and all, is under a hundred lines of assembly, and it compiles down to that roughly 300-byte core. You could read every single instruction in it in a few minutes, which is part of the appeal of working this low, there’s nowhere for anything to hide.Why do this at allNobody is going to build a modern game this way, and that’s not the point. You write at this level to understand what is actually happening beneath all the comfortable layers we normally use. Every game you have ever played, underneath its engine and its language and its libraries, eventually becomes exactly this, bytes that tell a processor to move values around, compare them, and jump based on the result. Seeing a whole working game at that level, small enough to hold in your head, makes the machine feel less like magic and more like a thing you could actually understand all the way down.It also puts the myth in its proper place. “Writing a game in pure binary by hand” isn’t a real practice, and anyone claiming to casually do it is closer to telling a tall tale than describing their workflow. But the honest version, writing at the instruction level and seeing the exact machine code it produces, is real, it’s doable, and it’s genuinely illuminating. The magic was never in typing ones and zeros. It was in understanding that underneath everything, that is all the computer was ever doing, and that with a readable name for each instruction, you can actually go down there and see it for yourself.I wrote a game in about 300 bytes of machine code, played it, and showed you the bytes. That’s what a game in binary really means, and the readable layer that makes it possible isn’t a cheat. It’s the whole reason any of us can talk to a machine at all.This was built and run as a small teaching example on a standard Linux system, using an assembler to turn human-readable assembly into machine code. The specifics of instructions and system calls vary by processor and operating system, so the exact bytes shown are particular to this setup. The point is the idea, that assembly sits one readable step above the machine code a processor actually runs, not the precise numbers.I Wrote a Game in Machine Code, and It Fits in About 300 Bytes was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.