Machine code

Also known as: machine language

On this page 7

Machine code, also called machine language, is the set of binary-encoded instructions that a processor executes directly, without any further translation. Among the software fundamentals, it marks the boundary between software and hardware: every program, whether written in C, Rust, Java or Python, must ultimately become machine code before a CPU can act on it.

Each instruction is a pattern of bits defined by the processor’s instruction set architecture (ISA), such as x86-64, ARM64 or RISC-V. Machine code is therefore specific to a processor family, which is why an executable file built for one architecture does not run natively on another.

How machine code is structured

A machine code instruction is a fixed or variable sequence of bytes that encodes one elementary operation. The instruction typically contains an opcode, which names the operation, and one or more operand fields, which identify registers, memory addresses or immediate constant values.

The processor runs machine code through the fetch-decode-execute cycle. It fetches the bytes at the address held in the program counter, decodes them into control signals, performs the operation and then advances to the next instruction or jumps elsewhere when a branch is taken. Modern CPUs pipeline and reorder this work internally, but the visible result must match strict sequential execution.

The following table lists the main fields found in a typical instruction encoding.

FieldPurposeExample content
OpcodeSelects the operationAdd, load, branch, compare
Register specifierNames a source or destination registerRegister x5 or RAX
Immediate valueEmbeds a constant in the instructionThe number 42
Addressing modeDefines how a memory address is formedBase register plus offset
Prefix or modifierChanges operand size or behavior64-bit operand override on x86
Common fields in a machine code instruction

Instruction length depends on the architecture. Fixed-length encodings such as the 32-bit instructions of ARM64 simplify decoding, while variable-length encodings such as x86-64, where an instruction spans from 1 to 15 bytes, achieve denser code at the cost of more complex decoders.

Machine code compared with assembly, bytecode and source code

Machine code sits at the lowest level of the abstraction hierarchy, and nearby representations are often confused with it. Assembly language is a human-readable notation in which each mnemonic usually maps one-to-one to a machine instruction, whereas source code in a high-level language maps to many instructions per statement.

The table below compares machine code with the representations that precede or replace it.

RepresentationReadable byExecuted byPortability
Source codeProgrammersCompiler or interpreter firstHigh
Assembly languageSpecialistsAssembler firstTied to one ISA
BytecodeTools and virtual machinesVirtual machine or JIT compilerHigh across platforms
Machine codeThe processorCPU hardware directlyTied to one ISA
Machine code and related program representations

Bytecode for the Java Virtual Machine or WebAssembly is sometimes called machine code for a virtual machine, but it is not executed by physical hardware. A runtime either interprets it or uses just-in-time (JIT) compilation to generate real machine code while the program runs.

How programs become machine code

Programs reach machine code through a translation pipeline that differs by language and toolchain. In ahead-of-time compiled languages, a compiler translates source code to object files containing machine code, and a linker combines them with libraries into an executable file.

The next table summarizes the main paths from source code to executed instructions.

StrategyWhen machine code appearsTypical languagesMain trade-off
Ahead-of-time compilationBefore distributionC, C++, Rust, GoFast startup, separate builds per ISA
Just-in-time compilationDuring executionJava, C#, JavaScriptPortable, but warm-up cost
InterpretationNever for the program itselfClassic Python, shell scriptsSimple, but slower execution
Assembly by handAt assembly timeFirmware, boot codeFull control, low productivity
Translation strategies and when machine code is produced

The executable does not contain raw instructions alone. Container formats such as ELF on Linux, PE on Windows and Mach-O on macOS wrap machine code with headers, symbol tables, relocation data and references to shared libraries that the operating system loader resolves at launch.

Instruction set architectures and compatibility

The instruction set architecture defines the contract between machine code and hardware: the available instructions, registers, memory model and encoding rules. Two chips from different vendors can run the same machine code when they implement the same ISA, even though their internal designs differ greatly.

The following table compares widely used instruction set architectures.

ISADesign styleInstruction lengthTypical use
x86-64CISC1 to 15 bytesDesktops, laptops, servers
ARM64 (AArch64)RISC4 bytesPhones, Apple silicon, cloud servers
RISC-VRISC, open standard2 or 4 bytesMicrocontrollers, research, embedded
Xtensa and similarConfigurable RISC2 or 3 bytesEmbedded software in IoT devices
Major instruction set architectures

When software must run on a foreign architecture, systems use binary translation or emulation. Translation layers convert x86-64 machine code to ARM64 either before execution or on the fly, typically with a moderate performance penalty that depends on the workload.

Limitations, risks and common misconceptions

Machine code offers maximum control and minimal overhead, but it is costly to write, read and maintain. A single source change can alter thousands of bytes, and disassembly recovers only instructions, not variable names, comments or the original program structure.

  • Machine code is not always faster by definition. Optimizing compilers usually produce better instruction sequences than hand-written code for large programs.
  • Binary does not mean secret. Disassemblers and decompilers can reconstruct much of a program’s logic, so machine code offers no real protection for secrets.
  • Same operating system is not enough. A Windows executable for ARM64 does not run natively on an x86-64 Windows machine.
  • Memory errors become exploitable. Flaws such as buffer overflows let attackers redirect execution, which is why protections like non-executable memory and address space layout randomization exist.

Self-modifying machine code, once common for saving memory, is now discouraged and often blocked by operating systems that separate writable memory from executable memory. JIT compilers handle this carefully by switching page permissions between writing and executing.

In everyday language

Machine code is the only language a computer chip truly understands. It is a long series of numbers, each one telling the processor to do one very small thing, such as adding two values or jumping to another step.

People rarely write it directly. Instead, they write programs in friendlier languages, and tools translate those programs into machine code. A useful comparison is a piano roll: the holes in the paper are meaningless to most people, yet the player piano reads them perfectly. The key point is that every app eventually becomes machine code, and that code fits only one family of chips.

Seen in the real world

A development team maintains a command-line tool written in Go and distributes it to customers on both Intel-based Linux servers and ARM-based cloud instances. The compiler turns the same source code into two different machine code binaries: one for x86-64 and one for ARM64, each packaged as an ELF executable of about 12 MB.

When a customer accidentally deploys the x86-64 build to an ARM64 server, the system refuses to start it with an “exec format error”. The team adds an automated build step that produces both binaries and labels each by architecture. Deployment failures of this kind disappear, and the ARM64 build runs roughly as fast as the x86-64 one on comparable hardware, at a lower hourly cost.