mirror of
https://github.com/FDOS/kernel.git
synced 2025-07-21 21:04:43 +02:00
Fix division for some large numbers such as 0xFFFF01FF/0x10101FF
See also https://sourceforge.net/p/freedos/bugs/106/ (Gerd Grosse)
This commit is contained in:
parent
fa36d8aa25
commit
eda671d079
@ -85,24 +85,28 @@
|
|||||||
push dx ; save
|
push dx ; save
|
||||||
push ax ; dividend
|
push ax ; dividend
|
||||||
mov si, bx ; divisor now in
|
mov si, bx ; divisor now in
|
||||||
mov di, cx ; di:bx and cx:si
|
mov di, cx ; di:si and cx:bx
|
||||||
%%shift_loop:
|
%%shift_loop:
|
||||||
shr dx, 1 ; shift both
|
shr dx, 1 ; shift both
|
||||||
rcr ax, 1 ; dividend
|
rcr ax, 1 ; dividend
|
||||||
shr di, 1 ; and divisor
|
shr cx, 1 ; and divisor
|
||||||
rcr bx, 1 ; right by 1 bit
|
rcr bx, 1 ; right by 1 bit
|
||||||
jnz %%shift_loop ; loop if di non-zero (rcr does not touch ZF)
|
jnz %%shift_loop ; loop if di non-zero (rcr does not touch ZF)
|
||||||
mov di, cx ; restore original divisor (di:si)
|
|
||||||
div bx ; compute quotient dx:ax>>x / cx:bx>>x (stored in ax; remainder in dx not used)
|
div bx ; compute quotient dx:ax>>x / cx:bx>>x (stored in ax; remainder in dx not used)
|
||||||
pop bx ; get dividend lo-word
|
pop bx ; get dividend lo-word
|
||||||
mov cx, ax ; save quotient
|
mov cx, ax ; save quotient
|
||||||
mul di ; quotient * divisor hi-word (low only)
|
mul di ; quotient * divisor hi-word (low only)
|
||||||
push di ; save divisor hi-word
|
push di ; save divisor hi-word
|
||||||
xchg ax, di ; save in di
|
mov di, ax ; save in di
|
||||||
mov ax, cx ; ax=quotient
|
mov ax, cx ; ax=quotient
|
||||||
mul si ; quotient * divisor lo-word
|
mul si ; quotient * divisor lo-word
|
||||||
add dx, di ; dx:ax = quotient * divisor
|
add dx, di ; quotient * divisor !!!! overflow possible !!!!
|
||||||
pop di ; restore divisor hi-word
|
pop di ; restore divisor hi-word
|
||||||
|
jnc %%skipcorrection
|
||||||
|
sub ax, si
|
||||||
|
sbb dx, di
|
||||||
|
dec cx ; correct quotient
|
||||||
|
%%skipcorrection:
|
||||||
sub bx, ax ; dividend-lo - (quot.*divisor)-lo
|
sub bx, ax ; dividend-lo - (quot.*divisor)-lo
|
||||||
mov ax, cx ; get quotient
|
mov ax, cx ; get quotient
|
||||||
pop cx ; restore dividend hi-word
|
pop cx ; restore dividend hi-word
|
||||||
|
Loading…
x
Reference in New Issue
Block a user