This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlab3.asm
390 lines (296 loc) · 6.84 KB
/
lab3.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
.model small
.stack 256
.data
.386
a dw 0
b dw 1
c dw 0
d dw 0
checker dw 0
not_a_number dw 0
is_Negative dw 0
result_is_negative dw 0
enter_num db 13,10,'Enter number: $'
enter_divident db 13,10,'Enter divident: $'
enter_divider db 13,10,'Enter divider: $'
num db 13,10,'Your number.....: $'
task1 db 13,10,'----- TASK 1 --------$'
task2 db 13,10,'----- TASK 2 --------$'
task3 db 13,10,'----- TASK 3 --------$'
str db 13,10,'---------------------$'
Result db 10, 13,'Result :$'
strOF db 10, 13,'Overlim. Please, repeat :$'
strCLS db 10, 13,'$'
strCMD db 10, 13,'> $'
strDZ db 10, 13,'Divide by zero. Please, repeat: $'
.code
main:
mov ax, @data
mov ds, ax
; -------------TASK 1----------------
mov ax, -7
push ax
mov ah, 9
mov dx, offset task1
int 21h
call clear_regs
mov ah, 9
mov dx, offset num
int 21h
call clear_regs
pop ax
call Show_AX
; -------------TASK 2----------------
mov ah, 9
mov dx, offset str
int 21h
call clear_regs
mov ah, 9
mov dx, offset task2
int 21h
call clear_regs
mov ah, 9
mov dx, offset enter_num
int 21h
call clear_regs
call readInput
push ax
mov ah, 9
mov dx, offset num
int 21h
call clear_regs
pop ax
call Show_AX
; -------------TASK 3----------------
mov ah, 9
mov dx, offset str
int 21h
call clear_regs
mov ah, 9
mov dx, offset task3
int 21h
call clear_regs
;ввод и вывод делимого
mov ah, 9
mov dx, offset enter_divident
int 21h
call clear_regs
call readInput
push ax
mov ah, 9
mov dx, offset num
int 21h
call clear_regs
pop ax
call Show_AX
;проверка на отрицательность
cmp is_Negative, 0
je positive_devidence
neg ax
inc result_is_negative
positive_devidence:
mov a, ax
;ввод и вывод делителя
entering_divider:
mov ah, 9
mov dx, offset enter_divider
int 21h
call clear_regs
call readInput
push ax
mov ah, 9
mov dx, offset num
int 21h
call clear_regs
pop ax
call Show_AX
;проверка на отрицательность
cmp is_Negative, 0
je positive_divider
neg ax
inc result_is_negative
positive_divider:
mov b, ax
;проверка на 0
cmp b, 0
je entering_divider
;деление
divide:
mov ax, a
idiv b
push ax
mov ah, 9
mov dx, offset Result
int 21h
call clear_regs
pop ax
cmp result_is_negative, 1
jne result_positive
neg ax
result_positive:
call Show_AX
mov ah,4Ch
mov al,00h
int 21h
readInput proc
call clear_regs
xor cx, cx
mov not_a_number, 0
mov checker, 0
mov is_Negative, 0
InputBegin:
call clear_regs
mov ah, 08h ;считывает символ, который нажимается, но не выводит его
int 21h
cmp al, 13 ;сравнивает код клавиши в al (13-код энтера)
jz return
cmp al, 8
jz backspace
cmp al, 27
jz escape
cmp al, '-'
jz negative
cmp al, '+'
jz plus
cmp al, '9'
ja InputBegin
cmp al, '0'
jb InputBegin
push ax ; --- Сохраним al
sub ax, '0' ; - Приводим в привычный вид
mov bl, al
mov ax, cx
mov dx, 10
mul dx
jo overlim
add ax, bx
jo overlim
mov cx, ax
pop ax
cmp cx, 32768
ja overlim
mov checker, 1
mov ah, 02h
mov dl, al ; вывод символа на экран (работает с dl)
int 21h
jmp InputBegin
overlim:
mov cx,0
mov ah, 9
mov dx, offset strOF
int 21h
mov is_Negative, 0
mov checker, 0
jmp InputBegin
plus:
cmp not_a_number, 1
je middle
cmp checker, 0
jne middle
mov not_a_number, 1
mov ah, 02h
mov dl, '+'
int 21h
middle:
jmp InputBegin
negative:
cmp not_a_number, 1
je middle
cmp checker, 0
jne negReturn
mov not_a_number, 1
mov is_Negative, 1
mov ah, 02h
mov dl, '-'
int 21h
negReturn:
jmp InputBegin
escape:
mov cx, 0
lea dx, strCMD
mov ah, 9
int 21h
jmp InputBegin
backspace:
mov ax, cx
mov bx, 10
div bx
lea dx, strCMD
push ax
mov ah, 9
int 21h
pop ax
push ax
cmp is_Negative, 0
jz positiveBackSpace
mov dl, '-'
mov ah, 2
int 21h
positiveBackSpace:
pop ax
cmp ax, 0
jnz notNull
mov is_Negative, 0
mov checker, 0
notNull:
call Show_AX
mov cx, ax
jmp InputBegin
return:
cmp checker, 0 ; Проверка на пустой ввод
jz InputBegin
cmp is_Negative, 1
jnz positive
neg cx
positive:
mov ax, cx
ret
readInput endp
Show_AX proc
push ax
push bx
push cx
push dx
push di
mov cx, 10 ; cx - основание системы счисления
xor di, di ; di - кол. цифр в числе
; если число в ax отрицательное, то
;1) напечатать '-'
;2) сделать ax положительным
or ax, ax
jns @@Conv
push ax
mov dx, '-'
mov ah, 2 ; ah - функция вывода символа на экран
int 21h
pop ax
neg ax
@@Conv:
xor dx, dx
div cx ; dl = num mod 10
add dl, '0' ; перевод в символьный формат
inc di
push dx ; складываем в стэк
or ax, ax
jnz @@Conv
; выводим из стэка на экран
@@Show:
pop dx ; dl = очередной символ
mov ah, 2 ; ah - функция вывода символа на экран
int 21h
dec di ; повторяем пока di<>0
jnz @@Show
pop di
pop dx
pop cx
pop bx
pop ax
ret
Show_AX endp
clear_regs proc
xor ax, ax
xor bx, bx
xor dx, dx
ret
clear_regs endp
end main