Name 8. (7 pts) Reverse Engineering (4 pts) The following MIPS code snippet impl
ID: 3731263 • Letter: N
Question
Name 8. (7 pts) Reverse Engineering (4 pts) The following MIPS code snippet implements a common coding design pattern in c. 1- he code to determine what this pattern is. Do not be Your task is to reverse engineer t concerned about the MIPS calling conventions, such as saving registers on the stack. Assume the following Ss0 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 $80, $zero, $zero add $al, $zero, $zero addi $s2, $zero, 10 #b-0; # $s2 = const 10; x: slt $to, $s1, $82 bne $to, $zero, Y sll st1, $s1, 2 add t2, $83, $t1 sw $sl, 0($t2) add $so, $s0, $s1 addi $s1, $s1,1 Y: # exit: Partial credit will be determined, in part, by the quality of your line-by-line comments. Please provide the comments in pseudocode formatExplanation / Answer
add $s0, $zero, $zero # b = 0;
add $s1, $zero, $zero # i = 0;
addi $s2, $zero, 10 # $s2 = costant 10;
X: slt $t0, $s1, $s2 # if (i < 10) {$t0 = 1;} else {$t0 = 0;}
bne $t0, $zero, Y # if($t0 != 0) goto Y; // checks the condition if i < 10, otherwise go to end of loop
sll $t1, $s1, 2 # $t1 = $s1 * 2;
add $t2, $s3, $t1 # $t2 = a + t1; // the address of element at offset t1 bytes
sw $s1, 0($t2) # *(a + $t2) = i; // i.e. a[i] = i;
add $s0, $s0, $s1 # b = b + i;
addi $s1, $s1, 1 # i = i + 1;
j X #goto X; //this is end of loop and goes to beginning of loop
Y: #statement after end of loop
The code above stores stores numbers 0, 1, 2, ... 9 into the array a[] and also calculates the sum 0+1+2+...+9 and stores in b. Each array elements is of size 2 bytes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.