-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2.asm
More file actions
294 lines (224 loc) · 6.56 KB
/
Task2.asm
File metadata and controls
294 lines (224 loc) · 6.56 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
; Names:
; Renae Beckford
; Enoch Omoregie
; Kemar Knight
; Winroy Jennings
;
; Date: 29, October 2024
; Tutor: Ms. Karlene M. Black
; Topic: Task 2
.model small
.data
menu db 10, 13, "Single Digit Calculator", 10, 13, "1. Addition", 10, 13, "2. Multiplication", 10, 13, "3. Subtraction", 10, 13, "4. Exit", "$"
prompt_op db 10, 13, "Please select an option (1-4): $"
str1 db 10, 13, "Please enter the first digit (0-9): $"
str2 db 10, 13, "Please enter the second digit (0-9): $"
equal db " = ", "$"
invalid_str db 10, 13, "Invalid input, try again!", "$"
operator db " ", "$" ; placeholder for operation symbol
new_line db 10, 13, "$"
result db 0
.code
start:
; Load data segment
mov ax, @data
mov ds, ax
main_menu:
; Display menu
mov dx, offset menu
mov ah, 09h
int 21h
; Prompt for operation choice
mov dx, offset prompt_op
mov ah, 09h
int 21h
; Get user input for operation
mov ah, 01h
int 21h
cmp al, '1'
jb invalid_input ; Invalid if less than 1
cmp al, '4'
ja invalid_input ; Invalid if greater than 4
mov cl, al ; Store operation choice in CL
cmp cl, '4'
jne get_first_digit ; Exit if choice is 4
mov ah, 04ch
int 21h
invalid_input:
; Display invalid choice message
mov dx, offset invalid_str
mov ah, 09h
int 21h
jmp main_menu
; Prompt for first number
get_first_digit:
mov dx, offset str1
mov ah, 09h
int 21h
; Get first digit input
mov ah, 01h
int 21h
cmp al, '0'
jb invalid_input
cmp al, '9'
ja invalid_input
mov bh, al ; Store first digit in BH
; Prompt for second number
get_second_digit:
mov dx, offset str2
mov ah, 09h
int 21h
; Get second digit input
mov ah, 01h
int 21h
cmp al, '0'
jb invalid_input
cmp al, '9'
ja invalid_input
mov bl, al ; Store second digit in BL
; Perform chosen operation
mov al, bh ; Move first digit to AL
cmp cl, '1'
je addition
cmp cl, '2'
je multiplication
cmp cl, '3'
je subtraction
addition:
add al, bl
mov ah, 0
aaa
mov ch, ah
mov cl, al
mov operator, '+'
jmp display_result
multiplication:
; Convert ASCII to numeric value
sub bh, '0' ; Convert first ASCII digit to numeric value (0-9)
sub bl, '0' ; Convert second ASCII digit to numeric value (0-9)
; Multiply digits
mov al, bh ; Move first numeric digit to AL
mul bl ; Multiply AL by BL (result in AX)
; Check if the result is a two-digit number
cmp ax, 10
jb single_digit_result_multiplication
; If two-digit result, split into tens and units digits
; Get the tens digit
mov cx, 10 ; Set divisor to 10
xor dx, dx ; Clear DX for division
div cx ; Divide AX by 10 (quotient in AL, remainder in AH)
; Convert and display tens digit
mov dl, al ; Move the quotient (tens digit) to DL
add dl, '0' ; Convert to ASCII
mov ah, 02h
int 21h ; Display the tens digit
; Convert and display units digit
mov dl, ah ; Move the remainder (units digit) to DL
add dl, '0' ; Convert to ASCII
mov ah, 02h
int 21h ; Display the units digit
jmp output_result
single_digit_result_multiplication:
; Single-digit result
add al, '0' ; Convert result to ASCII
mov dl, al
mov ah, 02h
int 21h ; Display the single-digit result
; Output newline after single-digit result
mov dx, offset new_line
mov ah, 09h
int 21h ; Display newline
output_result:
; Output newline after result
mov dx, offset new_line
mov ah, 09h
int 21h
jmp main_menu
subtraction:
sub al, bl
mov ch, 0
mov cl, al
mov operator, '-'
js neg_substraction
jmp display_result
neg_substraction:
; Convert result to ASCII
neg cl
add cl, '0'
; Output newline
mov dx, offset new_line
mov ah, 09h
int 21h
; Output first digit
mov dl, bh
mov ah, 02h
int 21h
; Output operator
mov dx, offset operator
mov ah, 09h
int 21h
; Output second digit
mov dl, bl
mov ah, 02h
int 21h
; Output equal sign and result
mov dx, offset equal
mov ah, 09h
int 21h
mov dx, offset operator
mov ah, 09h
int 21h
mov ch, '0'
mov dl, ch
mov ah, 02h
int 21h
mov dl, cl ; Output result
mov ah, 02h
int 21h
; Newline after result
mov dx, offset new_line
mov ah, 09h
int 21h
jmp main_menu
display_result:
; Convert result to ASCII
add ch, '0'
add cl, '0'
; Output newline
mov dx, offset new_line
mov ah, 09h
int 21h
; Output first digit
mov dl, bh
mov ah, 02h
int 21h
; Output operator
mov dx, offset operator
mov ah, 09h
int 21h
; Output second digit
mov dl, bl
mov ah, 02h
int 21h
; Output equal sign and result
mov dx, offset equal
mov ah, 09h
int 21h
; Correctly output the result
mov dl, ch ; Output tens digit
mov ah, 02h
int 21h
mov dl, cl ; Output units digit
mov ah, 02h
int 21h
; Newline after result
mov dx, offset new_line
mov ah, 09h
int 21h
; Go back to main menu
jmp main_menu
exit_program:
; Exit program
mov ah, 4Ch
int 21h
end start