-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.asm
163 lines (131 loc) · 2.92 KB
/
Calculator.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
.MODEL SMALL
.STACK 100H
.DATA
;The string to be printed
StartMessage DB 'Simple Calculator( + , - , * , / , % )',0AH, '$'
.CODE
;****************************************************************MAIN
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
; load address of the string
LEA DX,StartMessage
;output the string
MOV AH,09H
INT 21H
CALL ReadBCD ;read number 1 in BX, Operation in AL
PUSH BX ;save read number
PUSH AX ;operation in AL
CALL ReadBCD ;read number 2 in BX
POP CX ;operation
POP AX ;number 1, number 2 in BX
CALL Math ;result in AX
CALL PrintBin ;Print AX
;interrupt to exit
MOV AH,4CH
INT 21H
MAIN ENDP
;****************************************************************Functions
;AX,BX as input
;AX as output(Multi is AX,BX as output)
;CL as operate(+/*-%)
Math PROC
PUSH DX
Operate:
CMP CL,'+'
JE Plus
CMP CL,'-'
JE Mines
CMP CL,'*'
JE Multi
CMP CL,'/'
JE Divid
CMP CL,'%'
JE Remain
;exit if not find
MOV AX,0000H
MOV BX,0000H
JMP ExitMath
Plus:
ADD AX,BX
JMP ExitMath
Mines:
SUB AX,BX
JMP ExitMath
Multi:
Mul BX
MOV BX,DX
JMP ExitMath
Divid:
MOV DX,0000H
DIV BX
JMP ExitMath
Remain:
MOV DX,0000H
DIV BX
MOV AX,DX
JMP ExitMath
ExitMath:
POP DX
RET
Math ENDP
;return BX(number) and AL(last enter char)
ReadBCD PROC
PUSH CX
PUSH DX
MOV DX,0000H ;result
MOV CX,0000H ;counter
MOV BX,0000H ;mult temp
Read:
;Read key from keyboard
MOV AH,01H
INT 21H
CMP AL,'0'
JB ExitReadBCD ;return if below of 0
CMP AL,'9'
JA ExitReadBCD ;return if above of 9
SUB AL,30H
MOV CH,00H
MOV CL,AL
MOV DX,10D
MOV AX,BX
MUL DX
ADD AX,CX
MOV BX,AX ;save result in BX
JMP Read
ExitReadBCD:
POP DX
POP CX
RET
ReadBCD ENDP
;AX as input(max 16bit)
;note: save the CX,SI,DX,AX
PrintBin proc
PUSH AX
PUSH CX
PUSH DX
PUSH SI
mov cx,5
mov si,10D
DivTo10:
mov dx,0000H
div si ;remain in dx
push dx ;push to save digits
loop DivTo10
mov cx,5 ;print counter
Print:
POP dx ;pop to load digits
add dx,30H ;change to ASCII
;interupt to show
MOV AH,02H
INT 21H
;/interupt to show
LOOP Print ;loop until end counter(cx)
;pop
POP SI
POP DX
POP CX
POP AX
Ret
PrintBin ENDP
END MAIN