Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following C code snippet. // C code void setArray(int num) { int i;

ID: 645184 • Letter: C

Question

Consider the following C code snippet.
// C code
void setArray(int num) {
   int i;
   int array[10];
   for (i = 0; i < 10; i = i + 1) {
       array[i] = compare(num, i);
   }
}
int compare(int a, int b) {
   if (sub(a, b) >= 0)
       return 1;
   else
       return 0;
}
int sub(int a, int b) {
   return a - b;
}

Implement the C code snippet in MIPS assembly language. Use $s0 to hold the variable i. Be sure to handle the stack pointer appropriately. The array is stored on the stack of the setArray function (You may see Section 6.4.6 of the textbook for further reference). You may use the MARS MIPS Assembly simulator provided at the URL: http://courses.missouristate.edu/KenVollmar/MARS/download.htm.

Explanation / Answer

We will create separate codes for setArray,compare,subtract and for the for loop and exit conditions.

setArray:addiu $sp,$sp,-40

li $s0,0

li $s1,10

sw $ra, 0($sp)

move $s2,$sp

for_loop:

bge $s0,$s1,Exit #exit the loop when i>=10

addi $s2,$s2,4

lw $a1, 40($sp)

move $r0,$s0

jal compare

sw $v0,($s2) #array[i]=compare(num,i)

sw $ra,0($sp)

addi $s0,$s0,1

b for_loop

Exit: addiu $sp,$sp,40 #free up the stack

jr $ra

compare:

addiu $sp, $sp,-8

sw $ra,0($sp)

lw $a1,4($sp)

move $a0, $r0

jal subt

sw $v0,8($sp)

li $t1,0

lw $a0,$0,return_1

addi $t1,$t1,0

jal compare_End

compare_End:

move $v0,$t1

addiu $sp,$sp,-16

jr $ra

subt: sub $v0,$a0,$a1

jr $ra

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote