34.6k views
2 votes
Consider the MIPS code below. Match each line of MIPS with the correct comment that describes it. 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, O [Choose] addi $t1, $zero, 1 [Choose] loop: slt $t2, $a0,$t1 [Choose] bne $t2. $zero, finish [Choose] add $t0,$t0,$t1 [Choose ] addi $t1,$t1,2 [Choose jloop [Choose) finish: add $v0, $to, $zero [Choose] Please refer to the MIPS code in the question above. What does the code compute? a.The sum of integers from 0 to n b.The sum of odd integers from 0 to n c.The sum of even integers from 0 to n d.The ceiling of n/2

User Dagge
by
3.5k points

1 Answer

3 votes

Answer:

sum of odd integers from 0 to n

Step-by-step explanation:

#addi- add immediately

begin: addi $t0,$zero,0 #add $zero and 0,result stores in $t0

#addi - add immediately

#add $zero and 1,result stores in $t1

# $t1 = 0 + 1

addi $t1,$zero,1

#slt - set if less than

# if $a0 < $t1,set 1 to $t2

loop: slt $t2,$a0,$t1

#bne- branch for not equal

#if $t2 != 0,jump to label finish

bne $t2,$zero,finish

#add $t0 and $t1,result stores in $t0

# $t0 = $t0 + $t1

add $t0,$t0,$t1

#add immediately

#add $t1 and 2,result stores in $t2

# $t1 = $t1 + 2

addi $t1,$t1,2

# j - unconditional jump to given label

j loop

#add $t0 and 0,result stores in $v0

# $v0 = $t0 + 0

finish : add $v0,$t0,$zero

User Chibueze Opata
by
4.3k points