Situation:
The variable x is stored
at location 200 in memory
The variable y is stored
at location 204 in memory
The variable z is stored
at location 206 in memory
How does the CPU execute z = x + y using its registers ?
Pre-requisite: CPU must fetch the operands into a register before it can perform any operation
In order to compute
x + y:
We must fetch
the variable x from
location 200 in memory
into a register
We must fetch
the variable y from
location 204 in memory
into another register
Step 1: CPU executes a "load from memory 200" operation and fetches the variable x into a register:
The CPU sends out
control signals to
fetch x from
memory:
CPU sends out
a "Read" operation to
memory
CPU sends out
the location 200 (x)
(in binary) to
memory
The memory will send the value in x which the CPU will save it in a register (R0)
Step 2: CPU executes a "load from memory 204" operation and fetches the variable x into a register:
The CPU sends out
control signals to
fetch y from
memory:
CPU sends out
a "Read" operation to
memory
CPU sends out
the location 20y (y)
(in binary) to
memory
The memory will send the value in x which the CPU will save it in a (different) register (R1)
Step 3: Only now can the CPU executes an "add" operation and add the copies of x and y together:
The result of the operation is (always) stored in some register (R2 in the example) !!!
Step 4 (final): CPU executes a "store to memory 206" operation and save the result into the variable z:
The CPU sends out
control signals to
write (update) z in
memory:
CPU sends out
a "Store" operation to
memory
CPU sends out
the location 206 (z)
(in binary) to
memory
The memory will update the value in z (location 206)