Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 55 ยท Logic and Programs

Machine Code and Assembly

Learn what machine code and assembly are: instruction sets, opcodes and operands, the stored-program idea, and a toy assembler and CPU you can run and change.

A processor does not understand Python, JavaScript or even C. It understands exactly one thing: a list of numbers, each of which means "do this tiny operation". That list is machine code. Every program you have ever run, whatever language it was written in, ends up as machine code before the hardware executes it.

Reading raw numbers is painful, so people invented assembly language: the same instructions with short readable names such as ADD and JMP. A program called an assembler turns the names back into numbers. This lesson explains the pieces of an instruction, the big idea of the stored program, and lets you build a tiny assembler and processor of your own. It follows Boolean Logic and Logic Gates, whose adders are what execute an ADD.

The instruction set

Each processor family understands a fixed vocabulary called its instruction set architecture (ISA). It is the contract between software and hardware: it lists every instruction, what it does and how it is encoded. The most common ISAs today are:

  • x86-64 in most desktop, laptop and server processors from Intel and AMD.
  • ARM (including 64-bit AArch64) in nearly all phones and tablets, many laptops and a growing number of servers.
  • RISC-V, an open instruction set used in research and embedded devices.

A program compiled for one ISA does not run on another, because the numbers mean different things. That is why a download page offers separate builds for "Intel" and "ARM".

Anatomy of an instruction

Most instructions have two parts:

  • an opcode (operation code) saying what to do: add, load, jump;
  • zero or more operands saying what to do it with: a register, a memory address or a constant.

Typical categories are data movement (copy a value between memory and a register), arithmetic and logic (add, subtract, AND, compare), control flow (jump, branch if a value is zero, call a function) and input/output. The CPU runs them one after another in the fetch-decode-execute cycle from The CPU, except that a jump changes which instruction comes next.

A real example

The tiny C function int add(int a, int b) { return a + b; } becomes different assembly for each ISA. These are typical compiler results, shown read-only (they cannot run on this site, and a different compiler or setting may produce slightly different code):

TEXT
; x86-64 (Intel syntax)            ; ARM 64-bit (AArch64)
add:                               add:
    lea  eax, [rdi + rsi]              add  w0, w0, w1
    ret                                ret

Both do the same thing in two instructions: add the two arguments, leaving the answer in the return register, then ret (return to the caller). Notice the names differ and so do the registers. The numbers beneath them differ even more: x86-64 instructions have variable length (1 to 15 bytes), while AArch64 instructions are all exactly 4 bytes wide.

The stored-program idea

Early computers were rewired or reconfigured by hand to change their task. A key idea, usually credited to the von Neumann architecture, is that the instructions are just data kept in the same memory as the numbers they work on. The CPU fetches them like any other value and a program counter tracks where it is.

flowchart LR
  subgraph MEM["Memory"]
    I["Instructions<br/>addresses 0 to 31"]
    D["Data<br/>addresses 100 up"]
  end
  CPU["CPU<br/>program counter, registers"] -->|"fetch next instruction"| I
  CPU <-->|"load and store values"| D

One memory holds both the program and its data; the CPU reads whichever the next step needs.

This is why you can load a new program without touching hardware, why a compiler can write programs, and also why bugs and attacks that write into the wrong memory can change what a program does.

Building a toy machine

We invent a small ISA. It has one working register called the accumulator (A) and a memory of 128 cells. Every instruction is two cells: an opcode and an operand.

Mnemonic Opcode Meaning
HLT 0 stop
LDI n 1 A = n
LDA addr 2 A = memory[addr]
STA addr 3 memory[addr] = A
ADD addr 4 A = A + memory[addr]
SUB addr 5 A = A - memory[addr]
JNZ addr 6 jump to addr if A is not zero
OUT 7 print A

First, the assembler. It makes two passes: the first records where each label sits, the second replaces mnemonics and labels with numbers.

JS A toy assembler

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The text became 12 numbers. That list is the whole "executable". Now the other half: a processor that runs it. It is a short loop: fetch the opcode and operand at the program counter, advance the counter, then act.

JS A toy CPU running a loop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output is 15, the sum of 5+4+3+2+1. Here the loop: label and JNZ make the machine repeat: control flow in its simplest form, and the origin of every for and while statement. This is a stand-in, not a real chip: real processors have many registers, flags, caches and thousands of instructions. The principle, though (fetch, decode, execute, repeat), is exactly the same, and you will build a fuller version in Project: A Tiny CPU Simulator.

Try changing the starting LDI 5 to a different number, or replace ADD 100 with another instruction. Add a second OUT inside the loop to see the running total on each pass.

Why anyone still reads assembly

Few programmers write assembly daily, yet it matters. Compiler authors, operating system and driver writers and embedded developers use it where they need exact control of the hardware. Security researchers read it to analyse programs when source code is unavailable. And performance engineers inspect it to see what a compiler produced. Even if you never write it, knowing that a high-level line like total += x becomes a load, an add and a store makes performance and bugs easier to understand. The next lesson shows the bridge from readable languages down to this level: Programming Languages, Compilers and Interpreters.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is an opcode?

    The opcode selects the operation (add, load, jump). Operands say what to apply it to.
  2. 2. What does an assembler do?

    An assembler is a translator from the readable assembly text to the numbers the processor executes.
  3. 3. What is the stored-program idea?

    Keeping instructions in memory means the machine can be given a new task by loading new numbers, with no rewiring.
  4. 4. Why does a program built for ARM not run on an x86-64 processor?

    Each ISA defines its own instructions and encoding. Machine code is specific to the ISA it was built for.
  5. 5. In the toy machine, what does JNZ do?

    JNZ means jump if not zero. It lets the program repeat a block until A becomes zero.

Summary

  • Machine code is the numeric language the processor executes; assembly is the same thing with readable names.
  • An instruction set architecture defines the instructions of a processor family (x86-64, ARM, RISC-V).
  • An instruction is an opcode plus operands; categories include data movement, arithmetic, control flow and input/output.
  • The stored-program (von Neumann) idea keeps instructions and data in the same memory.
  • Jumps and conditional jumps create loops and decisions.
  • An assembler turns mnemonics and labels into numbers; a CPU loop fetches, decodes and executes them.

Next: how languages like Python and C get translated down to this level. Read Programming Languages, Compilers and Interpreters.