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

This is program is in a MIPS Prompt the user and read the numbers a and b to be

ID: 3770093 • Letter: T

Question

This is program is in a MIPS Prompt the user and read the numbers a and b to be divided. Examine the signs of a and b to determine whether the result will be negative or positive. Then calculate the quotient a / b and the remainder using |a| and |b|, and print the quotient and remainder, with the appropriate signs. Put this in a loop so that the user can enter multiple pairs of numbers and perform division on them. Set the quotient to 0. Shift the divisor left until it is greater than the dividend, then shift it right 1 position. This lines up the divisor under the leading digit of the dividend. Keep track of how many positions you shifted: you will use this to control the division loop. In the example shown, we "bring down" digits from the dividend until this intermediate dividend is greater than or equal to the divisor. Remember, in our code we don't "bring down" digits, instead we shift the divisor to the right at each step. For each position you shifted to line up the divisor and dividend: Shift the quotient left to add a zero to the end. If the divisor greater than dividend, we can't do anything. If divisor is left arrow dividend, this means that at this step we can subtract the divisor from the intermediate dividend and we get a 1 in the quotient. To put a 1 in the quotient, OR the quotient with 1. You have already shifted it, so you just need to change the low order digit from 0 to 1. Note that you could add 1, but OR is faster. Shift the divisor to the right to prepare for the next step. When you arc done, the dividend is the remainder.

Explanation / Answer

I have written the mips code for the same.

# Register map:
#   $ra - return of the code
#   $a0 - used to store dividend
#   $a1 - used to store divisor
#   $a2 - location to store reminder
#   $v0 - to return quotient
#   $t0 - used for shifting operations.
#   $t1 - used for calculations
      
divide:   move   $v0, $zero      
       move   $t0, $zero      
left_shift_for_divide:   and   $t1, $a1, 0x80000000  
       bne   $t1, $zero, loop  
       addi   $t0, $t0, 1      
       sll   $a1, $a1, 1      
       b   left_shift_for_divide  
loop:       sll   $v0, $v0, 1
       sltu   $t1, $a0, $a1      
       bne   $t1, $zero, loop1  
       subu   $a0, $a0, $a1      
       or   $v0, $v0, 1      
loop1:       srl   $a1, $a1, 1      
       addi   $t0, $t0, -1      
       bgez   $t0, loop      
end:       sw   $a0, 0($a2)      
       jr   $ra           # return.

The idea is to use subtract operation to do the divison as performed in the above mips code.

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