Basically, it takes longer than common ones.Thanks. I didn't come across that algorithm until last Fall and was thinking about writing something, but it turned out that I didn't need it. It would be interesting to compare its speed to more conventional division methods. Maybe some rainy day.
John
;*****************************************
;** DIVF -- Divide wreg3 = wreg1 / wreg2 **
;*****************************************
divf movff bsr,tmpbsr ;save program BSR
BANKSEL wreg1 ;set bsr for float working space
movlw 1 ;2 operands
rcall getoper ; get from stack
clrf wtmpe+0
clrf wreg3+0 ;clear quotient
clrf wreg3+1
clrf wreg3+2
movf wreg1s,w,1 ;get status
xorwf wreg2s,w,1 ;compute result sign
andlw 1 ;mask it
movwf wreg3s,1 ;save new sign
bbs wreg1s,FLNAN,1,fretnan ;NAN / (any) = NAN
bbs wreg2s,FLNAN,1,fretnan ;(any) / (NAN) = NAN
bbs wreg1s,FLZERO,1,fretz ;0 / (any) = 0
bbs wreg2s,FLZERO,1,fretinf ;(any) / 0 = INF
bbs wreg2s,FLINF,1,fretz ;(any) / INF = 0
bbs wreg1s,FLINF,1,fretinf ;(INF) / (any) = INF
clrf wtmpd+0,1
clrf wtmpd+4,1
clrc
rrcf wreg1+2,w,1 ;put wreg1 >> 1 into wtmpd
movwf wtmpd+3,1
rrcf wreg1+1,w,1
movwf wtmpd+2,1
rrcf wreg1+0,w,1
movwf wtmpd+1,1
rrcf wtmpd+0,f,1
dfninf movf wreg2e,w,1 ;get exponent of divisor
subwf wreg1e,w,1 ; subtract from dividend
addlw 33 ; and offset for loops
movwf wreg3e,1 ; save
dfloop movf wreg2+0,w,1 ;subtract divisor
subwf wtmpd+1,w,1
movwf wtmpe+1,1
movf wreg2+1,w,1 ;subtract divisor
subwfb wtmpd+2,w,1
movwf wtmpe+2,1
movf wreg2+2,w,1 ;subtract divisor
subwfb wtmpd+3,w,1
movwf wtmpe+3,1
clrw
subwfb wtmpd+4,w,1
bm dfneg ;result negative?
movwf wtmpd+4,1 ;no, save back remainder
movff wtmpe+3,wtmpd+3
movff wtmpe+2,wtmpd+2
movff wtmpe+1,wtmpd+1
dfneg rlcf wtmpe+0,f,1
rlcf wreg3+0,f,1 ;roll bit into quotient
rlcf wreg3+1,f,1
rlcf wreg3+2,f,1
rlcf wtmpd+0,f,1 ;rotate divisor
rlcf wtmpd+1,f,1
rlcf wtmpd+2,f,1
rlcf wtmpd+3,f,1
rlcf wtmpd+4,f,1
decf wreg3e,f,1
btfss wreg3+2,7,1 ;are we done?
bra dfloop ;no
movlw 0x80 ;round result
addwf wtmpe,f,1
clrw
addwfc wreg3+0,f,1
addwfc wreg3+1,f,1
addwfc wreg3+2,f,1
bra fretflt ;fix result
I suspect that the point of the assignment was to do the math in binary and not to just convert the answer done in decimal to binary at the end.i assume that you really want to devide and not a circuit to devide.
You gave two examples.Work in the base you are most comfortable with.
72/12 =6 = 110 binary. 100/5 = 20 =10100 binary.
Some people imagine things to be harder than they really are. Yes, division is the same in any base number system.I suspect that the point of the assignment was to do the math in binary and not to just convert the answer done in decimal to binary at the end.
As indicated in a much earlier response, if his son knows how to do long division in base ten, then this should not present much of a problem at all since the long-division algorithm is completely base-agnostic.
At concept level, no doubt, but binary, specifically, allows shifts effectively implemented, for example, for quotient and reminder in a single operation.Some people imagine things to be harder than they really are. Yes, division is the same in any base number system.
Keep in mind that the TS was specifically referring to pen and paper computations.At concept level, no doubt, but binary, specifically, allows shifts effectively implemented, for example, for quotient and reminder in a single operation.
OK William. Sure I had that in mind. To reduce what could be a long thread inside a thread, could you show a concrete example of what you believe would be your concrete example for the TS / OP as an answer?Keep in mind that the TS was specifically referring to pen and paper computations.
It would be a lot easier with a monospace font or even a CODE block in which I can paste text while preserving formatting or even if I could SEE the code formatting without having to do a preview. But here goes:OK William. Sure I had that in mind. To reduce what could be a long thread inside a thread, could you show a concrete example of what you believe would be your concrete example for the TS / OP as an answer?
Let us say, to divide these two (decimal values): 237 / 18?
1101r11
---------
10010 )11101101
10010
-----
10111
10010
-----
10101
10010
-----
11
I didn't know how long it might take me to find an example on the Internet that divides 237 by 18 in binary.In the interim, why not just copy one of the many examples on the Internet for how to do binary division?
I thought about doing that, but I prefer to post original source for stuff like this when possible so that it can be quoted and edited if desired. An alternative would have been using TeX, but dealing with alignment is more than a bit of a pain.Alternatively, do it with whatever tools you are comfortable with and use the clip tool.
I wish @jrap would fix the code tags to have user defined tab sizes. Oops. This isn't the off-topic or suggestions forum, is it?...Most of the bells and whistles are in the form of add-ins. I'm sure that lots of add-ins exist for stuff like this, but every add-in that jrap incorporates is one more that is a source of work and frustration for him.
I liked the Table formatting options that were available under vB quite a bit more -- and there were more of them. But it is what it is.