-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfdivmul.asm
104 lines (100 loc) · 1.35 KB
/
fdivmul.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
; Reciprocal value
; In: HL denominator
; Out: HL reciprocal value
; Pollutes: AF
FREC: LD A,H
XOR $7F
LD H,DIVTAB/$100
LD L,(HL)
LD H,A
LD A,L
OR L
RET NZ ; not a power of 2
LD A,H
AND $80
SET 7,H
INC H
JR Z,FRECH ; overflow
XOR $80
XOR H
LD H,A
RET
FRECH: OR $7F
LD H,A
LD L,$FF
RET
; Floating-point division
; In: BC numerator, HL denominator
; Out: HL fraction
; Pollutes: AF,AF',BC,DE
FDIV: CALL FREC
EX DE,HL
; continues with FMUL
; Floating-point multiplication
; In: DE, BC multiplicands
; Out: HL product
; Pollutes: AF,AF',BC,DE
FMUL: LD A,B
XOR D
AND $80
PUSH AF
RES 7,B
RES 7,D
CALL FMULP
POP AF
OR H
LD H,A
RET
; Find the difference of two squares
; Input: HL=hypothenuse, DE=leg
; Output: HL=HL*HL-DE*DE computed as (HL-DE)*(HL+DE)
FSQDIF: PUSH HL
PUSH DE
CALL FSUBP
POP DE
EX (SP),HL
CALL FADDP
EX DE,HL
POP BC
; continue with FMULP
; Floating-point multiplication of positive numbers
; In: DE, BC multiplicands
; Out: HL product
; Pollutes: AF,AF',BC,DE
FMULP: LD A,D
ADD B
CP $C0
JR NC,FINFTY ; overflow
SUB $40
JR C,FZERO ; underflow
LD H,A
LD A,E
ADD C
LD L,A
PUSH HL
LD B,E
JR NC,FMULN
CALL MUL8
RL L
LD A,H
ADC A,0 ; rounding
POP HL
ADD L
RRA
ADC A,0
LD L,A
INC H
RET
FMULN: CALL MUL8
RL L
LD A,H
ADC A,0
POP HL
ADD L
LD L,A
RET NC
SRA L
JR NC,FMULL
INC L ; rounding
FMULL: INC H
RET