B. 7 pts) Reverse Engineering 1. (4 pts) The following MIPS code snippet impleme
ID: 3910395 • Letter: B
Question
B. 7 pts) Reverse Engineering 1. (4 pts) The following MIPS code snippet implements a common coding design pattern in C. Your task is to reverse engineer the code to determine what this pattern is. Do not be concerned about the MIPS calling conventions, such as saving registers on the stack. Assume the following $s0 holds the C integer variable b; $s1 holds the C integer variable i; $s2 holds the C integer constant 10; $s3 holds the C pointer to an array of integers a; The code is as follows, with space for comments following the # sign at the right: add $s0, $zero, $zero add $s1, $zero, $zero addi $s2, $zero, 10 x: slt $t0, $s1, Ss2 bne $t0, $zero, Y ll $tl, $s1, 2 add $t2, $s3, $t1 sw $sl, 0 (St2) add $s0, $s0, $s1 addi $sl, $s1, 1 #b-0; # $32 -const 10; Y: # exit: Partial credit will be determined, in part, by the quality of your line-by-line comments. Please provide the comments in pseudocode format.Explanation / Answer
Considering the line numbers respectively starting from 1 in code answier is given:
4 th line:
slt - Set if less than (this sets the value to 0 or 1 based on the logic)
$t0 = true if ($s1 <$ s2) else false
# $t0 = true/1 if ( i < 10 ) else $t0 = false/0
5th line
bne = branch not equal (If the values are not equal it goes to that label)
#go to label Y if ($t0 !=0)
6th line
sll = shift left logical (shifts the bit to lef)
$t1= $s1 << 2
#$t1 = i <<2
7th line
add $t2,$s3,$t1
add - adds the two registers values mentioned.
#$t2=$s3+$t1
8th line
sw $s1,0($t2)
sw- store word(offset of a register is stored in another register)
#$s1 = mem($t2+0)
9th line
add $s0,$s0,$s1
add- adds register
$s0 = $s0+$s1
# b= b+i
10th line
addi $s1,$s1,1
addi -adds the immediate to registers
$s1= $s1+1
#i= i+1;
11th line
j X
j -jump
go to label X
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.