0

I'm trying to implement an algorithm of a division in MIPS, and I'm supposed to do he following:

  1. 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.
  2. shift remainder register to left by 1 bit
  3. then subtract the divisor from the remainder part of the remainder register only
  4. 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)
  5. 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

hakuna matata
  • 3,243
  • 13
  • 56
  • 93
  • Have you tried stepping through this in the debugger, to find where its behaviour diverges from what you expect? – Oliver Charlesworth Dec 26 '11 at 20:42
  • i dont know how to use the debugger in pcspim, but if u can tell me how to debug it, that would be great – hakuna matata Dec 26 '11 at 20:48
  • 2
    I don't know how to use this particular debugger, but I strongly suggest that you learn. Otherwise, it will be virtually impossible to solve this kind of problem on your own (other than by just staring at the code, or by asking other people to just stare at the code). Once you know how to use the debugger, identifying the problem should be easy. – Oliver Charlesworth Dec 26 '11 at 20:50
  • It used to be f10 key for the step debugging if I remember correctly. but i forgot whether it was for pcspim or xspim. You can try both I guess. – prongs Dec 26 '11 at 21:16

1 Answers1

0

See this question and answer for a reference code in C. Direct division of signed integers is rather complicated. If you are brave, look up Booth's Division Algorithm.

Community
  • 1
  • 1
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180