-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.asm
278 lines (209 loc) · 4.83 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
TITLE MASM Template (SempleCarlton_Calculator.asm)
; Carlton Semple
; Calculator Lab
; Date: 4/27/2014
INCLUDE Irvine32.inc
.data
welcome BYTE "---------- Carlton's Calculator ----------", 0
intro1 BYTE "1 = Addition", 0
intro2 BYTE "2 = Subtraction", 0
intro3 BYTE "3 = Multiplication", 0
intro4 BYTE "4 = Division", 0
intro5 BYTE "5 = Quit", 0
correctiveStatement BYTE "Please enter a valid choice", 0
farewell BYTE "Goodbye", 0
prompt1 BYTE "Enter the first number: ", 0
prompt2 BYTE "Enter the second number: ", 0
success BYTE "Result: ", 0
; symbols to make things look nicer
plus BYTE " + ", 0
minus BYTE " - ", 0
star BYTE " * ", 0
slash BYTE " / ", 0
equalsign BYTE " = ", 0
; zero error
NoZeroDenominator BYTE "The denominator cannot be 0. Reenter the divisor", 0
choice SDWORD ?
number1 SDWORD ?
number2 SDWORD ?
result SDWORD ?
.code
main PROC
Begin:
call menu
cmp choice, 1
jl Retry
cmp choice, 5
jg Retry
cmp choice, 5
je Quit ; quit if 5 is the choice
;; decide what to do based on the value of choice
call decide
jmp Begin
Retry:
;; remind the user to enter a valid choice
mov edx, OFFSET correctiveStatement
call WriteSTring
call crlf
call crlf
jmp Begin ; jump back to the beginning if none of the options were chosen
Quit:
mov edx, OFFSET farewell
call WriteString
exit
main ENDP
menu PROC
;; print out the options using edx and WriteString
mov edx, OFFSET welcome ; welcome message
call WriteString
call crlf
mov edx, OFFSET intro1 ; option 1
call WriteString
call crlf
mov edx, OFFSET intro2 ; option 2
call WriteString
call crlf
mov edx, OFFSET intro3 ; option 3
call WriteString
call crlf
mov edx, OFFSET intro4 ; option 4
call WriteSTring
call crlf
mov edx, OFFSET intro5 ; option to quit
call WriteString
call crlf
;; move the input into choice, which will be used by another function to determine what to do
call ReadInt
mov choice, eax
ret
menu ENDP
decide PROC
; Get the numbers first
;; get the first number
mov edx, OFFSET prompt1
call WriteString
call crlf
call ReadInt ;; prepare for signed numbers
mov number1, eax
;; get the second number
mov edx, OFFSET prompt2
call WriteString
call crlf
call ReadInt
mov number2, eax
; Decide what to do based on the value of choice
; addition
cmp choice, 1
je addition
; subtraction
cmp choice, 2
je subtraction
; multiplication
cmp choice, 3
je multiplication
; division
cmp choice, 4
je division
ret
decide ENDP
addition PROC
; add the numbers
mov eax, number1
add eax, number2
mov result, eax ; save the result
; display result
mov eax, number1
call crlf
call WriteInt ; display first number
mov edx, OFFSET plus
call WriteString ; display " + "
mov eax, number2
call WriteInt ; display second number
mov edx, OFFSET equalsign
call WriteString ; display " = "
mov eax, result
call WriteInt ; display result
call crlf
call crlf
ret
addition ENDP
subtraction PROC
; subtract the numbers
mov eax, number1
sub eax, number2
mov result, eax ; save the result
; display result
mov eax, number1
call crlf
call WriteInt ; display first number
mov edx, OFFSET minus
call WriteString ; display " + "
mov eax, number2
call WriteInt ; display second number
mov edx, OFFSET equalsign
call WriteString ; display " = "
mov eax, result
call WriteInt ; display result
call crlf
call crlf
ret
subtraction ENDP
multiplication PROC
; multiply the numbers
mov eax, number1
mov ebx, number2 ; move the number to a register first
imul ebx ; multiply eax by ebx
mov result, eax ; save the result
; display result
mov eax, number1
call crlf
call WriteInt ; display first number
mov edx, OFFSET star
call WriteString ; display " + "
mov eax, number2
call WriteInt ; display second number
mov edx, OFFSET equalsign
call WriteString ; display " = "
mov eax, result
call WriteInt ; display result
call crlf
call crlf
ret
multiplication ENDP
division PROC
; divide the numbers
mov eax, number1
cdq
mov ebx, number2 ; move the number to a register first
; make sure the divisor isn't 0
Check:
cmp ebx, 0
jz NoZeroesAllowed ; tell the user that 0 isn't allowed as the denominator
idiv ebx ; divide eax by ebx
mov result, eax ; save the result
; display result
mov eax, number1
call crlf
call WriteInt ; display first number
mov edx, OFFSET star
call WriteString ; display " + "
mov eax, number2
call WriteInt ; display second number
mov edx, OFFSET equalsign
call WriteString ; display " = "
mov eax, result
call WriteInt ; display result
call crlf
call crlf
jmp EndDivision
NoZeroesAllowed: ; tell the user that 0 isn't allowed as the denominator, then retry
mov edx, OFFSET NoZeroDenominator
call WriteString
call crlf
call ReadInt
mov number2, eax
jmp check
EndDivision:
ret
division ENDP
END main