-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTHREEDIGIT.ASM
246 lines (190 loc) · 3.59 KB
/
THREEDIGIT.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
jmp start
nm1 db 3, 4 dup(0)
nm2 db 3, 4 dup(0)
quo db 0
rmd db 0
rs1 db 0
rs2 db 0
rsf db 0
fnm db 0
snm db 0
ms1 db 'Enter 1st number: $'
ms2 db 'Enter 2nd number: $'
ms3 db 'Sum: $'
clf db 13,10,'$'
start:
; clear registers
call clear_r
call clear_v
; display message
lea dx,ms1
mov ah,9
int 21h
; get input
lea dx,nm1
mov ah,0ah
int 21h
; print line
call n_line
; display message
lea dx,ms2
mov ah,9
int 21h
; get input
lea dx,nm2
mov ah,0ah
int 21h
; print line
call n_line
; convert inputs to number
call cnv_nm1
call cnv_nm2
; clear registers
call clear_r
; add converted input
mov al,rs1
add al,rs2
mov rsf,al ; store result int result final
; print sum message
lea dx,ms3
mov ah,9
int 21h
; clear registers
call clear_r
; if result is greater than 100
cmp rsf,100
jae call_d3
; if result is greater than 10
cmp rsf,10
jae d2
; if result is less than 10
jmp d1
; stop program
int 20h
cnv_nm1:
mov al, [nm1+2] ; get the first digit
sub al, '0' ; convert to a number
mov fnm, al ; store the first digit
; set result
mov rs1, al
call clear_r
; check if there is a second digit
mov al, [nm1+3]
CMP al, 13
je fun_ret
; if there's a second digit
mov al, [nm1+3] ; get the second digit
sub al, '0' ; convert to a number
mov snm, al ; store the second digit
; multiply first digit by 10
mov al, fnm
mov bl, 10
mul bl
; add second digit to result
add al, snm
mov rs1, al
ret
cnv_nm2:
mov al, [nm2+2] ; get the first digit
sub al, '0' ; convert to a number
mov fnm, al ; store the first digit
; set result
mov rs2, al
call clear_r
; check if there is a second digit
mov al, [nm2+3]
CMP al, 13
je fun_ret
; if there's a second digit
mov al, [nm2+3] ; get the second digit
sub al, '0' ; convert to a number
mov snm, al ; store the second digit
; multiply first digit by 10
mov al, fnm
mov bl, 10
mul bl
; add second digit to result
add al, snm
mov rs2, al
ret
call_d3:
call d3
jmp end
fun_ret:
ret
d1:
mov dl,rsf
or dl,30h
mov ah,2
int 21h
int 20h
d2:
; divide result by 10
mov al,rsf
mov bl,10
div bl
; store results
mov quo,al
mov rmd,ah
; print quotient
mov dl,quo
or dl,30h
mov ah,2
int 21h
; print remainder
mov dl,rmd
or dl,30h
mov ah,2
int 21h
int 20h
d3:
; divide result by 100
mov al,rsf
mov bl,100
div bl
; store results
mov quo,al
mov rmd,ah
; print quotient
mov dl,quo
or dl,30h
mov ah,2
int 21h
; divide remainder by 10
mov al,rmd
mov bl,10
div bl
; store results
mov quo,al
mov rmd,ah
; clear registers
call clear_r
; print quotient
mov dl,quo
or dl,30h
mov ah,2
int 21h
; print remainder
mov dl,rmd
or dl,30h
mov ah,2
int 21h
int 20h
end:
int 20h
ret
n_line:
lea dx,clf
mov ah,9
int 21h
ret
clear_r:
xor ax,ax
xor bx,bx
xor cx,cx
xor cx,dx
ret
clear_v:
mov ax,3
int 10h
ret