Code must be translated

Why Code Must Be Translated

A CPU can only execute instructions in one form: machine code - binary patterns (0s and 1s) that its circuits are designed to decode and act upon. Machine code is specific to a processor or family of processors: the binary instruction patterns used by an ARM processor differ from those used by an Intel x86 processor, and code for one will not work on the other.

Because machine code is extremely difficult for humans to write directly, virtually all software is written in either a high-level language or assembly language. However, neither can be executed by a CPU without first being converted into machine code. This conversion process is called translation, and it is an essential step in the life of every program.

Source code (HL or assembly) Translator Machine code (binary) CPU executes (processor runs)

Every program must follow this path before the CPU can run it.

How Translation Works for Different Languages

The translation process differs depending on whether the source code is written in a high-level language or in assembly language.

Translating High-Level Languages

High-level source code is written in human-readable languages such as Python, Java, or C++. Before the CPU can run it, a translator must convert it into the machine code of the target processor. There are two main types of translator for high-level languages:

  • A compiler translates the entire source program into machine code in one pass, producing an executable file. The executable can then be run directly by the CPU without the original source code or compiler being present.
  • An interpreter translates and executes the source code line by line, each time the program runs. The original source code and interpreter must be present every time the program is executed.

In both cases, a single line of high-level code typically translates into many machine code instructions. For example, a simple arithmetic statement in Python might expand into dozens of low-level binary operations when compiled.

Compiler: Source code Compiler Executable (.exe / binary) CPU runs (any time) Interpreter: Source code Interpreter CPU runs line by line every run

Compiler: translates once, runs many times. Interpreter: translates and runs simultaneously, every time.

Translating Assembly Language

Assembly language source code is translated by a program called an assembler. Because assembly language has a 1:1 correspondence with machine code - each mnemonic corresponds to exactly one binary instruction - the translation process is far simpler than compiling a high-level language.

The assembler replaces each mnemonic (e.g. ADD, MOV, JMP) with its corresponding binary pattern for the target processor. The result is machine code that the CPU can execute directly. Like compiled high-level code, the assembled machine code is specific to the processor it was assembled for - the same assembly source code would need to be reassembled with the correct instruction set to run on a different processor.

Because the translation is so direct, assembly programs run very efficiently. This is why assembly language is still used for performance-critical or hardware-specific tasks such as embedded system firmware and device drivers.

Machine Code: Binary and Processor-Specific

The machine code produced by translation is always expressed in binary. Each instruction is a specific sequence of 0s and 1s that the processor's hardware decodes using its own internal circuitry. Because different processor families (e.g. ARM, Intel x86, RISC-V) have different circuit designs, the binary patterns that mean "add two numbers" or "jump to an address" are completely different from one architecture to another.

This is why machine code is described as processor-specific: a binary executable compiled for Windows on an Intel processor cannot run on an Apple Silicon Mac, even though both computers run modern operating systems. The binary instruction patterns simply do not correspond to the circuits in the other processor. Translators are therefore always targeted at a specific processor architecture, producing machine code that only works on that type of hardware.

Source code ARM compiler x86 compiler ARM binary runs on ARM only x86 binary runs on x86 only

The same source code produces different machine code for different processor architectures.

 Key Takeaways

  • All code written in high-level or assembly languages must be translated into machine code before the CPU can execute it.
  • Machine code is expressed in binary and is specific to a processor or family of processors - it will not run on a CPU with a different instruction set.
  • High-level languages are translated by a compiler (whole program at once) or interpreter (line by line); one line of high-level code can produce many machine code instructions.
  • Assembly language is translated by an assembler; each mnemonic produces exactly one machine code instruction (1:1 correspondence).