Machine Code & Assembly: Mnemonics, Assemblers, ISA

From MediaWiki
Jump to navigation Jump to search

Machine Code & Assembly: Mnemonics, Assemblers, ISA

This page explains what machine code is, how assembly language represents it symbolically, and how an assembler translates it into executable code.

Machine code

A computer program is a sequence of instructions that the CPU executes. Each instruction is encoded as a pattern of bits that the CPU understands.

Example: The bit pattern 10011100 in x86 machine language tells the CPU to push the flags register onto the stack. This instruction can also be written as 0x9C in hexadecimal.

Such patterns are called machine code or machine language. Writing programs directly in machine code is possible but extremely hard to read and maintain.

Assembly language

Assembly language gives each machine instruction a mnemonic (short name) to make it human-readable. Example: 10011100 → 0x9C → PUSHFQ

The mnemonic PUSHFQ corresponds exactly to the binary instruction 10011100. This 1:1 relationship between mnemonic and instruction is why assembly is called a low-level language.

Assembly provides some convenience features:

  • Constants (named values).
  • Symbolic labels (named memory addresses).
  • Macros (reusable instruction sequences).

Still, it is tied directly to a specific instruction set architecture (ISA). An assembly program written for x86 will not run on ARM without modification.

The assembler (compiler)

A CPU cannot execute mnemonics directly—it only understands binary. A special program called an assembler translates assembly code into machine code.

Process: 1. Write code in assembly language. 2. Assemble it using an assembler (e.g., NASM). 3. The assembler generates a binary object file containing machine code.

Example: PUSHFQ → Assembler → 10011100

The general term for such a translation program is compiler. When used specifically for assembly, it is called an assembler.

Why assembly matters

Assembly reveals the connection between human-readable code and the hardware.

  • It teaches how the CPU executes instructions.
  • It shows how registers, memory, and control flow interact.
  • It builds intuition for optimization and debugging.

Even though assembly is rarely used for full applications today, understanding it forms a foundation for systems programming and computer architecture.