Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Machine architecture and assembly language

Fundamental concepts of machine architecture and assembly language: instruction sets, formats, addressing modes, pipeline basics and simple assembly rules, with formulas, worked problems and exam-style guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Engineers use machine architecture and assembly language when writing low-level system code (boot loaders, device drivers, OS kernels), when debugging compilation output (linkers/loaders stage), and when designing fast hardware or compilers. Knowing how instructions are represented, addressed, fetched and executed is essential for correct, efficient systems software and for reasoning about performance (CPI, pipelining and hazards).

Key ideas

Every bold term below is defined before it is used.

  • Instruction: a binary-coded command that a CPU executes. Example: arithmetic, memory access, control-transfer.
  • Instruction set architecture (ISA): the abstract interface between software and hardware that defines the set of instructions, their binary encoding, register set, addressing modes, and memory model. (ISA is what assembly language programs target.)
  • Assembly language: a human-readable textual form of an ISA. An assembler converts it into machine code.
  • Opcode: the portion of an instruction encoding that selects which operation (add, load, branch) the CPU performs.
  • Register: a small, fast storage location inside the CPU. A CPU design specifies a set of named registers (for example, R0..R31). A register used for temporary values in instructions is called a general-purpose register.
  • Addressing mode: the rule that tells how to interpret operand fields in an instruction to produce an effective address or operand value. Common addressing modes: immediate (value encoded in instruction), register (operand is in a register), direct/absolute (instruction contains memory address), register indirect (address is in a register), base+offset (effective address = base register + signed offset), indexed (base register + index register), PC-relative (effective address = PC + signed offset; used by branches and some loads/stores).
  • Instruction format: how bits in an instruction are partitioned into fields (opcode, registers, immediate). Typical formats are R-type (register-register), I-type (immediate), J-type (jump/absolute).
  • Machine word: the standard unit of data width that the CPU processes (for many ISAs this is 32 bits). When I say "word", I mean the ISA-defined word size. Endianness (byte order) determines how multi-byte words are laid out in memory (little-endian vs big-endian) and is defined by the ISA or platform.
  • Microarchitecture: the hardware implementation of an ISA: how pipelines, functional units, caches, and control logic are organized to execute ISA instructions.
  • Instruction cycle: the classical fetch–decode–execute sequence. In pipelined designs this sequence is overlapped across instructions.
  • Pipeline: hardware that splits instruction execution into sequential stages (for example, IF, ID, EX, MEM, WB). Each stage performs part of the instruction; different instructions occupy different stages simultaneously to increase throughput.
  • Pipeline stage: one of the sequential steps in a pipeline (common 5-stage sequence: IF = instruction fetch; ID = instruction decode and register read; EX = execute/address calculation; MEM = memory access; WB = write back to register).
  • Hazard: a condition that prevents the next instruction in program order from executing in the following clock cycle. Types: data hazard (true data dependence), control hazard (branch or jump changes PC), structural hazard (hardware resource conflict). In a simple in-order 5-stage RISC pipeline, only RAW (read-after-write) data hazards occur; WAR and WAW hazards are avoided by in-order reads and writes.
  • Forwarding (bypassing): hardware technique to resolve some data hazards by routing an operand from a later pipeline stage directly to an earlier stage that needs it, avoiding a pipeline stall.
  • Stall (bubble): inserting one or more cycles where no useful instruction progress occurs in a particular pipeline stage to preserve correctness.

Exact statements and derivations universities commonly ask for:

  1. CPU execution time derivation. Start from definition:

    • CPU execution time = number of clock cycles required to execute a program × clock period.
    • Number of clock cycles = Instruction count × average cycles per instruction (CPI). Combine: Execution time = Instruction count × CPI × Clock period. Assumptions: CPI is an average over the instruction mix; clock period is fixed by the implementation.
  2. Ideal pipeline speedup (derivation sketch). Let k be number of pipeline stages. For a program of n instructions and zero pipeline hazards or overhead, cycles without pipeline = n × k (each instruction takes k stage-times sequentially). Here “cycles” are counted in stage-times; equivalently, an unpipelined design would take n CPU cycles with cycle time equal to k stage-times. With an ideal pipeline, after filling, throughput is one instruction per stage-time, so cycles ≈ n + (k − 1). For large n, asymptotic speedup ≈ k. Assumptions: perfect balancing of stage delays, zero pipeline register overhead, no hazards.

  3. Pipeline hazards and solution correctness conditions. For a data dependency where instruction Ij reads a register written by Ii (i < j): to avoid incorrect read, the earliest cycle where Ij needs the value must be at or after the cycle where Ii produces it. If forwarded and timing allows, a stall may not be necessary; otherwise insert stalls until condition holds.

Diagrams to draw (describe exactly):

  • Draw a horizontal table with columns labelled by cycle numbers (1,2,3,...). In each row place one instruction and in each cell put the pipeline stage abbreviation (IF, ID, EX, MEM, WB) such that stages for different instructions shift right by one column each row. To show a stall, put the word "bubble" or "—" in a cell where a stage would have been but is idle. Label the stage boxes and the horizontal axis cycles.
  • Draw a 5-box pipeline diagram vertically: top box IF, below ID, EX, MEM, WB. Draw arrows left-to-right showing how one instruction flows top-to-bottom and then to the next instruction in the following cycle.

Formulas

ExecutionTime = InstructionCount × CPI × ClockPeriod — InstructionCount: number (no units), CPI: cycles per instruction (cycles·instruction⁻¹), ClockPeriod: seconds (s). Applies to any synchronous CPU where CPI averaged over program. Equivalently, ExecutionTime = (InstructionCount × CPI) / ClockFrequency when a fixed clock frequency is given.

ClockFrequency = 1 / ClockPeriod — ClockFrequency in hertz (Hz), ClockPeriod in seconds (s). Applies to a fixed clock.

Cycles_unpipelined ≈ n × k — n instructions, k stages, measured in stage-times (each instruction uses all k stage-times sequentially). Measured in unpipelined CPU clock cycles it is n (with a longer clock period equal to the sum of stage delays).

Cycles_pipelined ≈ n + (k − 1) + stalls — n instructions, k stages, plus any stall cycles (assumes one stage-time per pipeline register tick). Applies to synchronous pipelines. Therefore average CPI = (n + (k − 1) + stalls)/n; for large n with no stalls, CPI → 1; with stalls, CPI ≈ 1 + (stalls/n).

IdealSpeedup ≈ k — ratio of unpipelined to pipelined throughput for large n, when there are zero stalls and zero register overhead. Applies under ideal assumptions listed above.

When you study assembly syntax, remember these items (syntax to remember):

  • Label definition: label: placed at the start of a line; refers to the address of the next instruction/data.
  • Immediate operand syntax: e.g., for many ISAs ADD R1, R2, #10 (R1 = R2 + 10). Branch/PC-relative syntax example: e.g., BEQ R1, R2, label (uses a PC-relative displacement; exact form varies by ISA).
  • Load/store syntax (common form): LW R1, offset(R2) meaning R1 = MEM[R2 + offset].
  • Directive examples: ORG address (set location counter), EQU (symbol equate), DB/WORD (define bytes/words; reserving uninitialized space uses directives like RESB/RESW or .space, depending on the assembler). Exact directive names vary by assembler.

Worked examples

Example 1 — CPU execution time (numerical) Given: InstructionCount = 2.0×10^6 instructions, average CPI = 1.8 cycles/instruction, ClockFrequency = 2.5 GHz. Step 1: Compute ClockPeriod = 1 / ClockFrequency = 1 / (2.5×10^9 Hz) = 0.4×10^−9 s = 0.4 ns. Step 2: Use ExecutionTime formula. ExecutionTime = InstructionCount × CPI × ClockPeriod = (2.0×10^6) × (1.8) × (0.4×10^−9 s) = (3.6×10^6) × (0.4×10^−9 s) = 1.44×10^−3 s = 1.44 ms. Final answer: 1.44 ms.

Example 2 — Pipeline timing and a load-use hazard (step-by-step trace) Assume a 5-stage pipeline with stages IF, ID, EX, MEM, WB. Instruction sequence: I1: LW R1, 0(R2) ; load word from memory into R1 I2: ADD R3, R1, R4 ; uses R1 produced by I1 I3: SUB R5, R3, R6 ; depends on R3 produced by I2 Assume forwarding is present but a load's data becomes available only at the end of MEM stage, so the dependent instruction following a load needs one stall (common MIPS behaviour). Compute cycles: Without stalls, cycles = n + (k − 1) = 3 + 4 = 7. With one load-use stall, cycles = 8. So total cycles = 8 for 3 instructions. CPI = 8 / 3 ≈ 2.667 cycles/instruction. How to draw the trace: draw a table with cycles 1..8 on top and each instruction as a row. Fill I1 with IF, ID, EX, MEM, WB occupying cycles 1..5. For I2 put IF in cycle2, ID in cycle3, a bubble in cycle4, EX in cycle5, MEM in cycle6, WB in cycle7. For I3 shift accordingly so it completes at cycle8. Final answer: 8 cycles total, CPI ≈ 2.667.

Example 3 — Addressing mode arithmetic (short) Given base register R2 contains 0x1000 (4096 decimal). An instruction uses base+offset addressing with offset = 20 (decimal). Effective address = base + offset = 0x1000 + 20 = 0x1014 (decimal 4116). Final answer: 0x1014 = 4116.

Common mistakes

  • In pipeline traces, forgetting that a load-use dependency typically requires one stall even if forwarding is present.
  • Miscounting the pipeline fill/drain cycles: for k stages, the pipeline adds k − 1 extra cycles beyond n for n instructions (when no stalls).
  • Using ideal pipeline speedup = k without stating the assumptions (balanced stages, zero overhead, large n).
  • Mixing up addressing modes: treating an immediate value as an address, or vice versa.

For your exam

What question types appear for this topic: definitions and short notes (define ISA, pipeline stages, hazards), derivations (derive CPU execution time formula or pipeline speedup), numerical problems (compute execution time, CPI, pipeline cycle count), trace problems (draw pipeline timing for a sequence, show stalls), small assembler tasks (explain or write simple assembly fragments, show label address resolution), diagram drawing (pipeline, IF/ID/EX latches), and short programs showing addressing modes.

What a full-marks answer includes:

  • For definitions: concise, correct definition and a short example.
  • For derivations: start from definitions, show each algebraic step, explicitly state assumptions.
  • For numericals: show formulas used, substitute numbers with units, show intermediate calculations and final numeric result with correct units.
  • For pipeline traces: draw or tabulate cycles clearly, mark stalls/bubbles, and compute total cycles and CPI.
  • For assembly or addressing questions: show final addresses, label resolution steps and the assembled instruction bytes or fields if asked.

Refer to other topics (do not re-teach): assembler design and two-pass assembly (for label resolution), linkers/loaders (for final addresses), code generation and storage allocation (for how instructions are emitted), and branch prediction (for advanced control-hazard handling).

Quick check

  1. State the ISA in one sentence. 2) Write the CPU time formula. 3) For a 5-stage pipeline and 10 instructions with no stalls, how many cycles?

Answers: 1) The ISA is the abstract interface that defines the CPU's instructions, registers, addressing modes and binary encodings. 2) ExecutionTime = InstructionCount × CPI × ClockPeriod. 3) 10 + (5 − 1) = 14 cycles.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?