Course contentsShow
← Computer Science
Lesson 1165 of 2,87227. Pipelining and PerformanceFree lesson

Data Hazards: Read-After-Write (RAW)

Understanding RAW hazards where an instruction needs a result that hasn't been written by a prior instruction yet.

Data Hazards: Read-After-Write (RAW)

What You'll Learn

You'll understand how Read-After-Write hazards occur when an instruction tries to read data before a previous instruction has finished writing it.

The Core Problem

In a pipelined processor, multiple instructions execute simultaneously across different stages. A Read-After-Write (RAW) hazard — also called a data dependency — happens when one instruction needs to read a register that a previous instruction hasn't finished writing to yet.

Simple Example

Consider these two instructions:

ADD R1, R2, R3    ; R1 = R2 + R3 (writes to R1)
SUB R4, R1, R5    ; R4 = R1 - R5 (reads from R1)

The SUB instruction needs the value in R1, but ADD hasn't completed writing it yet! If both instructions move through the pipeline simultaneously, SUB might read the old value of R1 instead of the newly computed one.

Pipeline Timing

In a five-stage pipeline:

  • Cycle 1: ADD fetches
  • Cycle 2: ADD decodes, SUB fetches
  • Cycle 3: ADD executes (computes result), SUB decodes (identifies it needs R1)
  • Cycle 4: ADD memory access, SUB executes (but R1 still has old value!)
  • Cycle 5: ADD write-back (R1 updated), SUB memory access

The problem? SUB tries to read R1 in cycle 3-4, but ADD doesn't write the new value until cycle 5.

Real-World Analogy

Imagine you're baking: Recipe step 1 says "mix ingredients, let rise for 1 hour." Step 2 says "punch down the dough." If you try step 2 immediately, the dough isn't ready — you're reading (using) something before it's been written (prepared).

Key Takeaway: RAW hazards occur when an instruction depends on data from a previous instruction that hasn't reached the write-back stage yet, requiring careful handling to maintain correct program execution.