The ADD Instruction
The add (ADD) instruction reads numbers from two memory cells, adds them, and stores the sum in a specified memory cell. The ADD instruction format is:
add dst=destination src1=first src2=second
- first is the memory cell location of the first operand.
- second is the memory cell location of the second operand.
- destination is the location of the memory cell where the sum will be stored.
When an ADD instruction is executed:
- the value in memory cell #first will be added to the value in memory cell #second and the sum will be stored in destination.
- the PC increases by 1.
- memory cells other than destination do not change.
More precisely, if the following are true:
- instruction #i is
add dst=d src1=s1 src2=s2 - the PC at time t = i
Then we can conclude the following:
- value of cell d at time (t + 1) = (value of cell s1 at time t) + (value of cell s2 at time t)
- the PC at time (t + 1) = i + 1
For example, if the following are true:
- instruction #0 is
add dst=3 src1=1 src2=2 - the PC at time 0 = 0
- value of cell 1 at time 0 = 2
- value of cell 2 at time 0 = 6
Then we can conclude the following:
- value of cell 3 at time (0 + 1) = (value of cell 1 at time 0) + (value of cell 2 at time 0)
- the PC at time (0 + 1) = 0 + 1
Since the value of cell #1 is 2 and the value of cell #2 is 6, we know that: value of cell 3 at time (0 + 1) = 2 + 6
Then after some Algebra, we conclude:
- value of cell 3 at time 1 = 8
- the PC at time 1 = 1
Try executing the ADD instruction in the simulator:
| Memory Cells |
|---|
| Program Counter | Time |
|---|---|
| 0 | 0 |
PC is 0, so the simulator is going to execute instruction #0. src1 is 1 and src2 is 2. Thus, the operation is
value at memory cell #1 + value at memory cell #2
= 2 + 6
= 8
dst is 3, which means the result will be stored in memory cell #3.
Comments
Please log in to add comments