Here is the answer of the tricker program.
$2 = 4 16 | 24 |
$4 = 20 20 | 20 |
$5 = 20 24 | 16 |
The detailed steps are described below. First the program
and intial states:
addi $2, $0, 4 # (1) Memory
There: sub $4, $5, $2 # (2) address contents
lw $5, 0($4) # (3) 12 | ... |
sw $4, 0($5) # (4) 16 | 24 |
bne $4, $5, There # (5) 20 | 20 |
24 | |
$2 = ..., $4 = ..., $5 = 20 28 | ... |
- (1) $2 gets 0 + 4 = 4.
- (2) $4 = $5 - $2 = 20 - 4 = 16.
- (3) $5 = value at memory address 0($4) (which is 16) = 24.
- (4) Value in $4 (which is 16) is stored at memory address 24.
- (5) $4 = 16, $5 = 24, they are not equal, go back to (2)
- (2) $4 = $5 - $2 = 24 - 4 = 20.
- (3) $5 = value at memory address 20 (0+$4 =20) = 20.
- (4) Value in $4 (=20) is stored in memory address 20 (0+$5). So
20 is stored again in address 20.
- (5) $4 = 20 and $5 = 20, so $4 equals $5, program finishes.