Add comments to each line of the following MIPS code Then de

Add comments to each line of the following MIPS code. Then, describe what it computes (one or two sentences). Assume that $a0 is used for the input and initially contains n, a positive integer. Assume that $v0 is used for returning the output value.

begin: addi $t0, $zero, 0   
addi $t1, $zero, 1   

   loop: slt $t2, $a0, $t1   
bne $t2, $zero, finish   
add $t0, $t0, $t1   
addi $t1, $t1, 2
j loop   

finish:    add $v0, $t0, $zero

Solution

begin: addi $t0, $zero, 0 # Initialize $t0 to 0 addi $t1, $zero, 1 # Initialize $t1 to 1 loop: slt $t2, $a0, $t1 # If $a0 is less than $t1, then set $t2 to 1 # Otherwise set $t2 to 0 bne $t2, $zero, finish # If $t2 is not equal to 0 (i.e. $a0 was less # than $t1), then branch to the instruction # labelled finish add $t0, $t0, $t1 # Increment $t0 by $t1 addi $t1, $t1, 2 # Increment $t1 by 2 j loop # Jump to the instruction labelled loop finish: add $v0, $t0, $zero # Set $v0 to $t0 This code computes the sum of the odd numbers between 1 and the value in $a0 (which is the same as taking the value in $a0, dividing by 2, rounding up and taking the square).
Add comments to each line of the following MIPS code. Then, describe what it computes (one or two sentences). Assume that $a0 is used for the input and initiall

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site