Page 99 - Computer_Science_F5
P. 99
Computer Science Imagine fetching data from memory in customers write down their orders on their
one stage while another stage also needs to order slip. This slip acts like a program
access memory. Without separate memory
with instructions for Mama chapati to
units, a structural hazard would occur.
follow. Consider the two scenarios in the
The solution would be to use separate
Mama chapati kitchen, one that follows a
FOR ONLINE READING ONLY
memory units or add extra hardware to
resolve the conflict. normal program order and the other where
a customer makes a special request.
Data hazards (a) Normal order processing (Linear
Data hazards occur due to dependencies Instructions)
between instructions, where one (i) The customer gives their order
instruction depends on the result of a slip (such as “chapati”).
previous instruction that is still in the
pipeline. We must ensure that the next (ii) Mama Chapati reads the slip
instruction does not attempt to access data and prepares the chapati (takes
dough, rolls it, cooks it).
before the current instruction, because this
will lead to incorrect results, as shown in (iii) Mama Chapati serves the
Program Example 2.1. chapati and takes the next
customer’s slip.
Program Example 2.1: (b) Branching with special requests
Data hazard (i) Customer gives their chit (e.g.,
“chapati with butter”).
add $r1, $r2, $r3 (result is stored in (ii) Mama Chapati reads the chit and
$r1)
checks for a special instruction
sub $r4, $r1, $r5 (reads from $r1) (like “butter”).
Here, the sub-instruction depends on
the result of the add instruction stored Branch if Butter: If there›s a «butter»
in $r1. Data hazards can be solved instruction, Mama Chapati jumps to a
through stalling or forwarding. different set of instructions:
Apply butter to the rolled dough
Control hazards
One example of the common types of Otherwise (No Butter): If there is no
instructions in ISAs is the control flow “butter” instruction, she continues with
instructions such as branch, jump, etc. the regular chapati preparation.
To understand branch instructions, let us (i) Mama Chapati cooks the chapati
revisit the mama chapati analogy. Imagine (with or without butter) and serves it.
Mama Chapati’s kitchen which has grown (ii) Mama Chapati takes the next
now implements a ticket system where customer’s slip.
90
for Advanced Secondary Schools
Computer Science Form 5.indd 90 23/07/2024 12:33