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

If you would, kindly comment your code so I can understand what each line does.

ID: 647300 • Letter: I

Question

If you would, kindly comment your code so I can understand what each line does. Be sure to show the program running the sample array, preferrably in the MARS IDE. Thank you!

Write a MIPS-32 assembly language function, SUM, which computes the sum of the positive values and the sum of the negative values in an array X of length N words (the array is stored in memory). This function will be tested as a component of the complete program, as described:

Write a complete MIPS-32 program to test the function. Your program should:

Explanation / Answer

Pseudocode: $v0 = 0; $v1 = 0; for ( ; $a1 > 0; $a1 = $a1-1) {$t0 = Mem($a0); $t1 = $t0 & 1; $a0 = $a0 + 4; if ($t0 > 0 & $t1 = 0) $v0 = $v0 + $t0; if ($t0 < 0 & $t1 != 0) $v1= $v1+ $t0; } return; Label Op-Code Dest. S1, S2 Comments .globl SUM .text SUM: li $v0, 0 li $v1, 0 # Initialize v0 and v1 to zero LOOP: lw $t0, 0($a0) # Get a value from the array addi $t2, $t0, 1 # Increment loop count addi $a0, $a0, 4 # Increment array pointer to next word bltz $t0, NEG # If value is negative Branch to NEG bnez $t2, CHK add $v0, $v0, $t0 b CHK # break to chk NEG: beqz $t2, CHK add $v1, $v1, $t0 # $v1=$v1+$t0 CHK: addi $a1, $a1, -1 # Decrement loop count bgtz $a1, LOOP #if ($a1>0),Branch jr $ra #RETURN