-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathm_fixed.asm
137 lines (119 loc) · 5.23 KB
/
m_fixed.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
;
; Copyright (C) 2023 Frenkel Smeijers
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2
; of the License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
;
cpu 8086
bits 16
; Divide FFFFFFFFh by a 32-bit number.
; Based on https://github.com/FDOS/kernel/blob/master/kernel/ludivmul.inc
;
; input:
; dx:ax = divisor
;
; output:
; dx:ax = quotient of division of FFFFFFFFh by divisor
global FixedReciprocal
FixedReciprocal:
xchg bx, ax ; bx = divisor-lo
mov ax, 0ffffh ; ax = FFFFh
test dx, dx ; divisor > 2^16-1 ?
jnz big_divisor ; yes, divisor > 2^16-1
mov cx, ax ; ax = FFFFh, bx = divisor-lo, cx = FFFFh, dx = 0
div bx ; quotient-hi in ax
xchg ax, cx ; cx = quotient-hi, ax = FFFFh
div bx ; ax = quotient-lo
mov dx, cx ; dx = quotient-hi
retf ; return quotient in dx:ax
big_divisor:
mov cx, dx ; cx = divisor-hi
mov dx, ax ; ax = FFFFh, bx = divisor-lo, cx = divisor-hi, dx = FFFFh
push si ; save temp
push di ; variables
mov si, bx ; divisor now in
mov di, cx ; di:si and cx:bx
shift_loop:
shr dx, 1 ; shift both dividend = FFFFFFFFh
shr cx, 1 ; and divisor
rcr bx, 1 ; right by 1 bit
jnz shift_loop ; loop if di non-zero (rcr does not touch ZF)
div bx ; compute quotient FFFFh:FFFFh>>x / cx:bx>>x (stored in ax; remainder in dx not used)
mov cx, ax ; save quotient
mul di ; quotient * divisor hi-word (low only)
not ax ; dividend high - divisor high * quotient, no overflow (carry/borrow) possible here
mov bx, ax ; save dividend high
mov ax, cx ; ax=quotient
mul si ; quotient * divisor lo-word
mov ax, cx ; get quotient
sub bx, dx ; subtract (divisor-lo * quot.)-hi from dividend-hi
sbb dx, dx ; 0 if remainder > 0, else FFFFFFFFh
add ax, dx ; correct quotient if necessary ax += dx
xor dx, dx ; clear hi-word of quot (ax<=FFFFh) dx := 0
pop di ; restore temp
pop si ; variables
retf ; return quotient in dx:ax
; Divide FFFFFFFFh by a 16-bit number.
;
; input:
; ax = divisor
;
; output:
; dx:ax = quotient of division of FFFFFFFFh by divisor
global FixedReciprocalSmall
FixedReciprocalSmall:
xchg bx, ax ; bx = divisor
mov ax, 0ffffh ;
mov cx, ax ;
xor dx, dx ; ax = FFFFh, bx = divisor, cx = FFFFh, dx = 0
div bx ; quotient-hi in ax
xchg ax, cx ; cx = quotient-hi, ax = FFFFh
div bx ; ax = quotient-lo
mov dx, cx ; dx = quotient-hi
retf ; return quotient in dx:ax
; Divide FFFFFFFFh by a 32-bit number.
;
; input:
; dx:ax = divisor, dx != 0
;
; output:
; ax = quotient of division of FFFFFFFFh by divisor
global FixedReciprocalBig
FixedReciprocalBig:
xchg bx, ax ; bx = divisor-lo
mov cx, dx ; cx = divisor-hi
mov ax, 0ffffh ; ax = FFFFh
mov dx, ax ; ax = FFFFh, bx = divisor-lo, cx = divisor-hi, dx = FFFFh
push si ; save temp
push di ; variables
mov si, bx ; divisor now in
mov di, cx ; di:si and cx:bx
shift_loop_big:
shr dx, 1 ; shift both dividend = FFFFFFFFh
shr cx, 1 ; and divisor
rcr bx, 1 ; right by 1 bit
jnz shift_loop_big ; loop if di non-zero (rcr does not touch ZF)
div bx ; compute quotient FFFFh:FFFFh>>x / cx:bx>>x (stored in ax; remainder in dx not used)
mov cx, ax ; save quotient
mul di ; quotient * divisor hi-word (low only)
not ax ; dividend high - divisor high * quotient, no overflow (carry/borrow) possible here
mov bx, ax ; save dividend high
mov ax, cx ; ax=quotient
mul si ; quotient * divisor lo-word
mov ax, cx ; get quotient
sub bx, dx ; subtract (divisor-lo * quot.)-hi from dividend-hi
sbb dx, dx ; 0 if remainder > 0, else FFFFFFFFh
add ax, dx ; correct quotient if necessary ax += dx
pop di ; restore temp
pop si ; variables
retf ; return quotient in dx:ax