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 17
/
Copy pathlab2.asm
185 lines (140 loc) · 2.14 KB
/
lab2.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
.model small
.stack 100
.data
digits_counter dw 0
ten dw 10
wrong_input db "...Incorrect input$"
zero db "You tried to divide by zero...Nothing happened.$"
.code
endl proc
push dx
push ax
mov dl, 0AH
mov ah, 02h
int 21h
pop ax
pop dx
ret
endl endp
input proc
push bx
push cx
push dx
mov bx, 0
mov dx, 0
mov digits_counter, 0
reading:
mov ah, 01h
int 21h
cmp al, 8 ; backspace
jz backspace
cmp al, 13
jz good_input
cmp al, 48 ; '0' = 48
jc bad_input
cmp al, 58 ;
jnc bad_input
mov cl, al ; bx = bx * 10 + al - 48
mov ax, bx
mul ten
mov bx, ax
cmp dx, 0 ; dx == 0 else too large
jnz bad_input
sub cl, 48
mov ch, 0
add bx, cx
inc digits_counter
jc bad_input
jmp reading
backspace:
cmp digits_counter, 0
jz reading
mov ax, bx
div ten
mov bx, ax
mov dl, ' '
mov ah, 02h
int 21h
mov dl, 8
mov ah, 02h
int 21h
mov dl, 0
dec digits_counter
jmp reading
good_input:
cmp digits_counter, 0
jz reading
mov ax, bx
jmp finish
bad_input:
lea dx, wrong_input
mov ah, 09h
int 21h
mov bx, 0
mov dx, 0
mov ax, 0
mov digits_counter, 0
call endl
jmp reading
finish:
pop dx
pop cx
pop bx
ret
input endp
output proc
push ax
push cx
push dx
mov cx, 0
mov dx, 0
division :
div ten
push dx
mov dx, 0
inc cx
cmp ax, 0
jnz division
cmp cx, 0
jnz non_zero_came
push 0
mov cx, 1
non_zero_came:
pop dx
add dx, '0'
mov ah, 02h
int 21h
loop non_zero_came
pop dx
pop cx
pop ax
ret
output endp
main:
mov ax, @data
mov ds, ax
call input
call output
call endl
mov cx, ax
call input
call output
call endl
mov dx, cx
mov cx, ax
mov ax, dx
mov dx, 0
cmp cx, 0
jz div_by_zero
div cx
call output
call endl
jmp end_main
div_by_zero:
lea dx, zero
mov ah, 09h
int 21h
end_main:
mov ax, 4c00h
int 21h
end main