In a pipelined processor, when an instruction needs the result of a previous instruction that has not yet finished its execution, what specific type of hazard occurs?
When an instruction in a pipelined processor needs the result of a previous instruction that has not yet finished its execution, a Data Hazard occurs. This specific type of data hazard is known as a Read-After-Write (RAW) dependency, also referred to as a true data dependency.
A pipelined processor breaks down the execution of an instruction into several stages, such as Fetch, Decode, Execute, Memory Access, and Write-Back. It processes multiple instructions concurrently, with each instruction in a different stage, much like an assembly line. This overlapping execution improves the processor's throughput.
A hazard is any situation that prevents the next instruction in the pipeline from executing during its designated clock cycle, thereby potentially causing the pipeline to stall or introducing bubbles (empty cycles).
A Data Hazard arises when instructions are dependent on the data produced by preceding instructions, and the pipeline's structure allows these dependent instructions to reach a stage where they need that data before it is actually available. In the case of a Read-After-Write (RAW) hazard, a later instruction attempts to *reada register or memory location whose value is meant to be *writtenby an earlier instruction that is still in an upstream pipeline stage. For instance, consider two instructions:
1. `ADD R1, R2, R3` (Instruction 1 calculates `R2 + R3` and intends to store the result in `R1`)
2. `SUB R4, R1, R5` (Instruction 2 needs the value of `R1` to calculate `R1 - R5` and store the result in `R4`)
If Instruction 2 moves into its Execute stage and attempts to read `R1` before Instruction 1 has completed its Write-Back stage (where `R1` is updated), Instruction 2 will read an incorrect, old value of `R1`. To prevent this, the pipeline must stall Instruction 2 until `R1` is correctly updated, or employ a technique like data forwarding (bypassing) where the result from Instruction 1's Execute or Memory stage is directly forwarded to Instruction 2's Execute stage, making the data available sooner without waiting for it to be written back to the register file.