-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.s
121 lines (115 loc) · 1.84 KB
/
calc.s
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
;
; Mathematical subroutines
;
; Steve Maddison, 12/02/2007
;
; Name: calc_2s_comp_16
; Desc: Calculate 2's complement of 16-bit value
; In: HL = value to complement
; Out: HL = complemented value
calc_2s_comp_16:
push af
ld a,h
cpl
ld h,a
ld a,l
cpl
ld l,a
inc hl
pop af
ret
; Name: calc_int_div_8
; Desc: Divide two 8-bit integers with 8-bit result
; In: E = dividend, C = divisor
; Out: A = quotient, B = remainder
calc_int_div_8:
push af
push de
xor a
ld b,8
calc_int_div_8_loop:
rl e
rla
sub c
jr nc,calc_int_div_8_no_add
add a,c
calc_int_div_8_no_add:
djnz calc_int_div_8_loop
ld b,a
ld a,e
rla
cpl
pop de
pop af
ret
; Name: calc_int_div_16
; Desc: Divide two 16-bit integers with 16-bit result
; In: BC = dividend, DE = divisor
; Out: BC = quotient, HL = remainder
calc_int_div_16:
push af
ld hl,0
ld a,b
ld b,16
calc_int_div_16_loop:
rl c
rla
adc hl,hl
sbc hl,de
jr nc,calc_int_div_16_no_add
add hl,de
calc_int_div_16_no_add:
djnz calc_int_div_16_loop
rl c
rla
cpl
ld b,a
ld a,c
cpl
ld c,a
pop af
ret
; Name: calc_int_mult_8
; Desc: Multiply two 8-bit integers with 16-bit result
; In: H,E = values to multiply
; Out: HL = result
calc_int_mult_8:
push bc
push de
ld d,0
ld l,d
ld b,8
calc_int_mult_8_loop:
add hl,hl
jr nc,calc_int_mult_8_no_add
add hl,de
calc_int_mult_8_no_add:
djnz calc_int_mult_8_loop
pop de
pop bc
ret
; Name: calc_int_mult_16
; Desc: Multiply two 16-bit integers with 32-bit result
; In: BC,DE = values to multiply
; Out: BCHL = result
calc_int_mult_16:
push af
ld a,c
ld c,b
ld hl,0
ld b,16
calc_int_mult_16_loop:
add hl,hl
rla
rl c
jr nc,calc_int_mult_16_no_add
add hl,de
adc a,0
jp nc,calc_int_mult_16_no_add
inc c
calc_int_mult_16_no_add:
djnz calc_int_mult_16_loop
ld b,c
ld c,a
pop af
ret