-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIV.s
114 lines (82 loc) · 2.28 KB
/
DIV.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
ROM EQU 0000H
RAM EQU 8000H
ORG RAM
X: DS 2
Y: DS 2
Z: DS 2
; MAIN
ORG ROM
LD SP, RAM+0FFFH
CALL DIV_XY
HALT
; DIV_XY
DIV_XY:
PUSH HL
PUSH BC
PUSH DE
PUSH AF
LD HL,(Y)
LD A, L
CP 000H
JP NZ, DIV_START_UP
LD A, H
CP 000H
JP NZ, DIV_START_UP
; 0で割ることはできないのでerror
; ひとまずエラーをしらせるため0xffffをいれる
JP DIV_OVERFLOW
DIV_START_UP:
LD BC, 0000H
LD A, 10H
DIV_LOOP:
LD HL,(Y)
SRA H
RR L
RR B
RR C
LD (Y), HL
; Zフラグをたたせるため
DEC H
INC H
JP NZ, DIV_EXCEPTION_ACTION
DEC L
INC L
JP NZ, DIV_EXCEPTION_ACTION
JP DIV_DIFFERENCE_CHECK
DIV_END_ACTION:
LD HL,(X)
LD (Y),HL
LD (X),DE
; オーバーフロー処理
LD A, D
CP 28H
JP P, DIV_OVERFLOW
POP AF
POP DE
POP BC
POP HL
RET
DIV_EXCEPTION_ACTION:
AND A
RL E
RL D
JP DIV_CHECK_LOOP_COUNT
DIV_DIFFERENCE_CHECK:
LD HL, (X)
SBC HL, BC
JP C, DIV_EXCEPTION_ACTION
LD (X),HL
SCF
RL E
RL D
JP DIV_CHECK_LOOP_COUNT
DIV_CHECK_LOOP_COUNT:
; Aレジスタの値を減らす
DEC A
JP NZ, DIV_LOOP
JP DIV_END_ACTION
DIV_OVERFLOW:
LD HL, 0FFFFH
LD (X), HL
HALT
END