I'm trying to implement an algorithm of a division in MIPS, and I'm supposed to do he following:
- the remainder and the quotient are in the same register, the upper half is the remainder, and the lower half is the quotient, and this register is initialized to the value of the dividend.
- shift remainder register to left by 1 bit
- then subtract the divisor from the remainder part of the remainder register only
- check if rem is less than 0, if rem < 0 then rem = rem + divisor, if rem > 0, then just put the first bit (least significant bit as 1)
- keep doing this n-times, where n is the width of the divisor, in my case its 6-bit wide, so the loop is 6 times
here is the code I've written so far, i am trying to do it on positive operands first, and then want to make it for signed operands
# A program that divides two integers according to the approach described in Fig. 3.12
.data # Data declaration section
.text
main: # Start of code section
li $s2, 4 # dividend, will als0 be used as the remainder register which will hold remainder and quotient initialized to dividend
li $s3, 2 # divisor
li $s4, 2 # another register to hold the dividend shifted 6 bits, for adding and subtracting dividend from remainder
sll $s4, $s4, 6
li $t0, 0 # counter of the loop
LOOP: sll $s2, $s2, 1 # shift the remainder regitser by 1 bit to right
sub $s2, $s2, $s4 # subtract divisor from remainder part of the remainder register
slt $t1, $s2, $zero # to check if rem < 0
beq $t1, $zero, MORE # if rem no < 0 then branch to MORE label
nop
add $s2, $s2, $s4 # if rem < 0, to add the divisor to the remainder part of the remainder register
j LOOP # jump back to the loop
nop
MORE: # if rem > 0, then do arithmetic right shift and place 1 as the 0th position
rol $s2, $s2, 1 # rotate the number to the left by 1 bit which is arithmetic right shift
j LOOP # jump back to loop
nop
addi $t0, $t0, 1 # adding 1 to the counter of the loop
slti $t1, $t0, 6 # checking if the loop condition is working or not
bne $t1, $zero, LOOP
nop
add $a0, $zero, $s2 # putting the result in regitser a0
li $v0, 1 # printing out the result
syscall
# END OF PROGRAM
Can someone please check my code, and tell me where did I go wrong. Thanks