there are 6 error in the answer one error accounts the entire line
Consider the following fragment of ac program int [101, si int *pi for (p 131 p p++) Here is a buggy translation in MIPS assembly language, assuming s is insi6 and p is in S19 or $16, $0, $0 1 $19, k 12 bne SB, finish add $16, $19, $16 addi $19, 1 j loop finish
Expert Answer
int k[10], s;
int *p;
s=10;
for(p=&k[3]; *p!=0; p++)
s=s+p;
MIPS Code
or $16,$0,$0
lw $19,k+12
loop:
bne $8, finish
add $16,$19,$16
addi 19, 1
j loop
finish:
or $16,$0,$0
The syntax for the instruction ‘or’ is as follows
Or $d,$s,$t
$d=$s|$t
$16=$0|$0
$0 is a special purpose register so it contains value 0
$16=0|0
$16=0
It’s been told s is in $16
s=0
lw $19,k+12
The syntax for the instruction ‘lw’(load word) is as follows
lw $t, offset($s)
$t = MEM[$s + offset]
The register $t will contain the value present in the memory address given by offset +$s
The given instruction lw $19, k+12 is syntactically wrong.
bne $8, finish
The syntax for the instruction ‘bne’(branch on not equal) is as follows
bne $s,$t, offset
if($s!=$t) go to PC+4+offset
The given instruction bne $8, finish is syntactically wrong.
add $16,$19,$16
The syntax for the instruction ‘add’(add with overflow) is as follows
add $d, $s, $t
$d=$s+$t
add $16,$19,$16
$16=$19+$16
s is in $16 and p is in $19
s=p+s
from the first line(or $16,$0,$0) we know that s=0
0=p+0
0=p
addi $19, 1
The syntax for the instruction ‘addi’(add immediate) is as follows
addi $t, $s, imm
$t = $s + imm
The given instruction addi $19, 1 is syntactically wrong.
j loop
The syntax for the instruction ‘j’(jump to the calculated address) is as follows
j target
this is an unconditional jump statement