RSS Forums RSS
Please support our Assembly advertiser: Programming Forums
Views: 3280 | Replies: 0
Reply
Join Date: Apr 2006
Posts: 1
Reputation: newassemblyuser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
newassemblyuser newassemblyuser is offline Offline
Newbie Poster

Help Unsigned Division

  #1  
Apr 19th, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:22 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC