📄 div68k.lib
字号:
*
* div68k.lib 32/32 divide routine for the 68000
*
* This code is based on the original developed by Greg Garner
* for the Commodore Amiga. He released this code to the
* public domain with the restriction that his name be included
* in any source this code is used in.
*
* I have modified this code to save crucial registers
* and to streamline the exit somewhat.
*
*
* 32-bit division, using shift and subtract.
* Enter with numerator in D0, denominator in D1.
* Returns quotient in D0, remainder in D1.
*
*
* Enter at _div32u for unsigned division.
*
_div32u
movem.l d2-d7,-(a7) save some regs
clr.l d7 start with a positive number
bra _div1 do the divide
*
* Enter at _div32s for signed division.
*
_div32s
movem.l d2-d7,-(a7) save some regs
move.l #$00,d7 assume we have two positive numbers
tst.l d1 see if we are dividing by zero
beq _divfail branch if fail
bpl _div2 at least this number is positive
not.l d7 show that we have a negative number
neg.l d1 change D1 to a positive number
_div2
tst.l d0
bpl _div1 is this a positive number?
not.l d7 two negs make positive
neg.l d0 turn it into a positive number
_div1
move.l #32,d4 number of bits-1 in quotient
clr.l d2 clear two temporary registers
clr.l d3
move.w #0,ccr clear carry for first time
_div3
roxl.l #$01,d0 rotate the left bit into the Extend bit
roxl.l #$01,d2 rotate the Extend bit into temp register
move.l d2,d3 move D2 into temporary D3
sub.l d1,d3 D3=D3-D1. Is D3>D1?
eori.b #$ff,ccr invert the carry
bcc _div4 no, shift some more into D2; extend=0
move.l d3,d2 save the remainder; extend=1
_div4
move.w sr,d5 save the Extend bit
subq.l #$01,d4 decrement the bit count
beq _div5 if we are done, then leave
move.w d5,ccr get the Extend bit back
bra _div3
_div5
move.w d5,ccr get last Extend bit
roxl.l #$01,d0 rotate it into the answer
move.l d2,d1 put the remainder in D1
move.w #$0,ccr
tst.l d7 is the answer positive or negative?
bpl _divex branch if positive
neg.l d0 change the quotient to negative
bra _divex all done
_divfail
move.l d0,d1 use numerator as remainder
clr.l d0 use 0 as quotient in failure
_divex
movem.l (a7)+,d2-d7 restore regs
rts
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -