Assembly and Machine Code
Assembly Language · Assembly & OS track
5 min read
In this module
From Text to Binary
The CPU never sees assembly. It runs machine code: raw binary numbers, no exceptions. Assembly is a human-friendly text rendering of those numbers, mapped one-to-one to the bytes underneath. The tool that converts one to the other is called the assembler.
The Assembler's Job
The assembler chews through your .asm file and spits out an object file (.o). For every instruction it reads, it looks up the binary encoding (the opcode plus its operand bytes) and writes those bytes to disk. Labels in the same file and section are resolved along the way: the assembler computes each label's offset and patches it into every jump and call that references it. A label it can't see, such as a function in another file, gets a placeholder instead, which the next section covers.
mov rax, 7, as an example, can encode to the bytes 48 C7 C0 07 00 00 00 in x86-64 machine code. Those seven bytes are exactly what the CPU fetches, decodes, and runs.
The assembler converts human-readable assembly into binary machine code. Mnemonics like mov or add each correspond to a specific binary opcode. The output is an object file of raw bytes the CPU is happy to run.
Object Files and Linking
Most real programs are spread across many source files. Each one assembles into its own object file. None of those object files can run on their own. They likely call functions or reference data living in other files. The linker scoops up all the object files, resolves the cross-references between them, and produces a single executable binary.
Library code lives in this same machinery. When your program calls a standard-library function, the linker locates the library's object code and stitches it into your executable, or, for shared libraries, arranges for it to be found when the program starts.
Worked Example: The Hole the Linker Fills
How does the assembler encode a call to a function that lives in another file, when it has no idea where that function will end up? It leaves a hole and a note.
Assemble a file containing call puts and look inside the object file with objdump -d -r:
0: e8 00 00 00 00 call 5 <main+0x5> 1: R_X86_64_PLT32 puts-0x4The e8 is the opcode for a near call. The four zero bytes after it are a placeholder for the distance to puts. The second line is a relocation: an instruction to the linker saying "at offset 1 in this section, fill in the distance to puts". objdump even shows the call aimed at address 5, the very next instruction, because a distance of zero lands right there.
Once the linker has decided where everything goes, it works out the real distances and patches every hole. A call's operand is measured from the end of the call instruction. So a 5-byte call sitting at 0x401000 that targets 0x401020 stores 0x401020 − 0x401005 = 0x1b, giving the bytes e8 1b 00 00 00.
Storing distances instead of absolute addresses is deliberate. The code keeps working wherever the whole block is loaded in memory, which matters a great deal once ASLR starts moving programs around.
Predict: a 5-byte call at 0x401030 targets a function at 0x401000. What four bytes follow the e8?
cb ff ff ff. The call ends at 0x401035, so the distance is 0x401000 − 0x401035 = −0x35. As a 32-bit two's complement number that's 0xffffffcb, stored little-endian as cb ff ff ff. Backwards calls simply have negative distances.
The Full Pipeline
A C program's journey from source code to running process passes through several distinct stages. The first three arrows carry the gcc flag that stops the pipeline after that stage, so you can open the intermediate file and look:
You can see the assembly your compiler generates with your own eyes. Pass -S to GCC and you get a .s assembly file rather than a binary. Few exercises do more for understanding what your C code actually becomes once it reaches the metal.
This Is the Bottom
The linker's output is a file packed with encoded instructions and data. When you run it, the operating system maps those bytes into memory, sets the CPU's program counter to the entry point, and the CPU starts its fetch-decode-execute loop. Below machine code there's nothing but electrical signals on copper traces. Every piece of software you've ever touched, from your browser to the kernel underneath it, eventually melts into the same stream of ones and zeros sliding through the silicon.