944,098 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 4995
  • Assembly RSS
Apr 19th, 2006
0

Unsigned Division

Expand Post »
Hello Everyone:
I am new to assembly and am trying to learn division and multiplication . I have the code below that I wrote to perform unsigned division, I have looked it over in detail but cant figure out what I'm doing wrong. Note that for division the divisor apparently needs to start in the left half of the 64 bit register but I cant figure out what that means for my algorithm below:

# This program performs restorative division
# based on the quotient, remainder and the dividend
# and the divisor



# STRING CONSTANTS
.data
GETA : .asciiz "Enter A "
GETB : .asciiz "Enter B "
NL : .asciiz "\n"
ERR : .asciiz "Divide by zero error\n"
preshiftmsg: .asciiz "Preshift value="
postshiftmsg: .asciiz "Postshift value="
iniRemMsg: .asciiz "Initial remainder before shifting="
preRemMsg: .asciiz "Initial remainder="
postRemMsg: .asciiz "Final remainder="
preDivMsg: .asciiz "The initial divisor="
divisorMsg: .asciiz "The shifted divisor="
remainder: .word 0
quotient: .word 0
dividend: .word 0
divisor: .word 0
repetitions: .word 32
QMSG : .asciiz "Q = "
RMSG : .asciiz "R = "

.text
.globl main

main:

# read in the first number
li $v0,4
la $a0,GETA
syscall

# store the integer in a register
li $v0,5
syscall
move $s0,$v0

# enter a newline
li $v0,4
la $a0,NL
syscall

li $v0,4
la $a0,GETB
syscall


# store the second integer in a register
li $v0,5
syscall
move $s1,$v0



# The restoring division algorithm
# Step 1: subtract the divisor register from the remainder register
# and place the result in the remainder register
# Step 2: if (remainder>=0)
# {
# shift the quotient register to the left
# setting the new rightmost bit to 1
# }
# else
# {
# restore the original value by adding
# the divisor register to the remainder
# and place the sum in the remainder register
# Shift quotient register to the left setting
# the new least significant bit to 0
# }
# Step 3: Shift the Divisor register right 1 bit
# Step 4: if less than 33rd repetition repeat
# Step 5 :else
# Step 6: quit
# $s0 = Dividend
# $s1 = Divisor
# $s2 = Remainder
# $s3 = quotient
# $s4 = Repetitions

Start:
li $s4,32
move $s2,$s0

Loop:
#li $v0,1
#move $a0,$s2
#syscall
#move $s2,$a0
#li $v0,4
#la $a0,NL
#syscall

li $v0,1
move $a0,$s3
syscall
move $s3,$a0
li $v0,4
la $a0,NL
syscall


sub $s2,$s2,$s1
bltz $s2,Label2b
sll $s3,$s3,1
ori $s3,$s3,1
j Label3

Label2b:
add $s2,$s2,$s1
sll $s3,$s3,1

Label3:
srl $s1,$s1,1
addi $s4,$s4,-1
bgtz $s4,Loop


end:
li $v0,4
la $a0,QMSG
syscall

li $v0,1
move $a0,$s3
syscall

# enter a newline
li $v0,4
la $a0,NL
syscall

# print the remainder message
li $v0,4
la $a0,RMSG
syscall

# print the remainder
li $v0,1
move $a0,$s2
syscall

# enter a newline
li $v0,4
la $a0,NL
syscall

#exit
li $v0,10
syscall



An extra pair of eyes would be much appreciated.

Thanks Again
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newassemblyuser is offline Offline
1 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: lc3 assembly language to count the occurances of variables in strings
Next Thread in Assembly Forum Timeline: What resulit of this code?!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC