Inspecting Binaries with Hexdump

Lately I’ve been working on an 8080 emulator in Swift. The process of having to take a binary apart and figure out which byte is associated with which instruction has gotten me interested in programs and how they work under the hood.

Instead of doing some private research and calling it a day, I wanted to try and put things together into one place.

What Are Binaries?

A binary is nothing more than an executable chunk of data arranged in a certain way. In fact, everything on a computer is a chunk of data. Images, music, movies – these are all just sets of ones and zeros arranged in such a way that they can produce some meaningful output when interpreted in a certain way.

Understanding Binaries

As mentioned, a binary is just data. Nothing but a long series of ones and zeros, the same as everything else on a hard disk. Here’s the trick, though: ones and zeros can mean different things based on how you interpret them.

Take the following series of bits: 0100 0001 0100 0010. A byte is 8 bits, so we’re looking at two bytes. If you were to consult a hex editor, it would take each nibble (half a byte, or 4 binary digits) and display it as a hex digit (a single hex digit is 4 bits). It would tell you that this particular string represents 4142. It’s not wrong – this string is definitely 4142 in hexadecimal… but it can mean something entirely different depending on who you ask.

A text editor would take each byte and figure out which character that should be. By consulting an ASCII table, we see that 41 in hex is A, and 42 is B. So a text editor’s verdict would be that this particular binary string represents AB. Just like a hex editor tells you that its understanding of the string is 4142, the text editor is also correct. It’s all a matter of interpretation.

If the data wasn’t intended to be human-readable, the result will be mostly garbled. You’ve probably tried to open up a program in a text editor before and seen this yourself.

So, we know about the hex editor’s opinion, as well as the text editor’s. But what would a CPU think? Now things are getting interesting. Before we delve down that rabbit hole, let’s build a binary of our very own.

Building Our Own Binary

With the following source code, and a bit of help from a compiler, we can produce a binary which can then be executed by our computer.