-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab4.asm
293 lines (265 loc) · 5.6 KB
/
Lab4.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
assume CS:code,DS:data
data segment
maxRadix db 15
inputBuf1 db 97, 99 dup ('$'); max - actual - 97*data - $
inputBuf2 db 97, 99 dup ('$')
illegalCharStr db "Illegal character was encountered, exiting...", 13, 10, '$'
incorrectOrderStr db "Change the order to + -, exiting...", 13, 10, '$'
minusStr db "-$"
result db 0, 99 dup ('$')
newline db 13, 10, '$'
data ends
SSEG segment stack
db 400h dup (?)
SSEG ends
code segment
printNewLine proc
mov dx, offset newline
mov ah, 09h
int 21h
ret
printNewLine endp
arrCharToInt proc ; char* arr
push bp ; preserving caller base pointer
mov bp, sp; specifying new base pointer
xor si, si
mov si, [bp+4] ;first parameter
xor cx, cx
mov cl, [si+1] ;moving actual string size to cl
add si, 2 ; si points to first input character
arrLoop:
cmp byte ptr [si], '0'; checks whether character is
jae aboveCheck
cmp byte ptr [si], '-'
jne illegalChar
inc si
loop arrLoop
aboveCheck:
cmp byte ptr [si], '9'
ja illegalChar
sub byte ptr [si], '0'
inc si
loop arrLoop
mov sp, bp ; freeing space for locals
pop bp ; restoring old base pointer
ret 2; returns and frees params
illegalChar: ;prints string and exiting
mov dx, offset illegalCharStr
mov ah, 09h
int 21h
mov ah, 4Ch
int 21h
arrCharToInt endp
unsignedAdd proc
push bp ; preserving caller base pointer
mov bp, sp; specifying new base pointer
sub sp, 2
xor si, si
xor di, di
mov si, [bp+4] ; first string adress
mov di, [bp+6] ; second string adress
mov al, [si+1]
mov [bp-1], al ;[bp-1] = actual length of str1
mov ah, [di+1]
mov [bp-2], ah ;[bp-2] = actual length of str2
xor cx, cx
mov cl, al ; cl = length str1
mov di, offset result
cmp al, ah
jae firstLenBigger
mov cl, ah ; cl = length str2
mov si, [bp+6] ; si = *str2
firstLenBigger:
xor dx, dx
mov dl, cl
add si, 1 ; si points to first digit of string
add si, cx
add di, cx
copyLoop:
mov bl, [si]
mov [di], bl
dec si
dec di
loop copyLoop
mov si, [bp+4] ; first string adress
mov di, [bp+6] ; second string adress
mov al, [si+1]
mov [bp-1], al ;[bp-1] = actual length of str1
mov ah, [di+1]
mov [bp-2], ah ;[bp-2] = actual length of str2
xor dx, dx
mov si, [bp+6]
mov dl, al ;dl stores len of biggest
cmp al, ah
jae firstLenBigger1
mov si, [bp+4]
mov dl, ah
mov ah, al
firstLenBigger1:
mov di, offset result
xor cx, cx
mov cl, ah ;cl stores len of shortest
add si, 1
add si, cx
add di, dx
mov bx, 10
xor dx, dx
addLoop:
xor ax, ax
mov al, [si]
add al, dh
add al, [di]
div bl
mov dh, al ;carry stored in dh
mov [di], ah
dec di
dec si
loop addLoop
add [di], dh
mov sp, bp ; freeing space for locals
pop bp ; restoring old base pointer
ret 4; returns and frees params
unsignedAdd endp
unsignedSub proc
push bp ; preserving caller base pointer
mov bp, sp; specifying new base pointer
sub sp, 2
mov si, [bp+4] ; first string adress
mov di, [bp+6] ; second string adress
mov al, [si+1]
mov [bp-1], al ;[bp-1] = actual length of str1
mov ah, [di+1]
mov [bp-2], ah ;[bp-2] = actual length of str2
xor cx, cx
mov cl, al
mov di, offset result
add si, 2
copyLoop1:
mov bl, [si]
mov [di], bl
inc si
inc di
loop copyLoop1
;mov di, offset result
mov si, [bp+6]
;add di, [bp-1]
xor bx, bx
mov bl, byte ptr [bp-2]
add si, bx
inc si
dec di
xor cx, cx
mov cl, [bp-2]
xor dx, dx
subLoop:
xor ax, ax
mov al, [si]
mov ah, [di]
cmp dl, 0
je continueSub
cmp ah, 0
jne continueSub1
mov dl, 1
add ah, 10
continueSub1:
sub ah, 1
continueSub:
cmp ah, al
jae notNeedsCarry
mov dl, 1
add ah, 10
notNeedsCarry:
sub ah, al
mov [di], ah
dec di
dec si
loop subLoop
sub [di], dl
mov sp, bp ; freeing space for locals
pop bp ; restoring old base pointer
ret 4; returns and frees params
unsignedSub endp
arrIntToChar proc ; char* arr
push bp ; preserving caller base pointer
mov bp, sp; specifying new base pointer
xor si, si
mov si, [bp+4] ;first parameter
arrIntToCharLoop:
cmp [si], byte ptr '$'
je arrIntToCharEnd
add [si], byte ptr '0'
inc si
jmp arrIntToCharLoop
arrIntToCharEnd:
mov sp, bp ; freeing space for locals
pop bp ; restoring old base pointer
ret 2; returns and frees params
arrIntToChar endp
compute proc
mov ah, 0Ah
mov dx, offset inputBuf1
int 21h
call printNewLine
push offset inputBuf1
call arrCharToInt
mov ah, 0Ah
mov dx, offset inputBuf2
int 21h
call printNewLine
push offset inputBuf2
call arrCharToInt
mov si, offset inputBuf1
mov di, offset inputBuf2
add si, 2
add di, 2
cmp [si], byte ptr '-'
je firstMinus
cmp [di], byte ptr '-'
je firstPlusSecondMinus
;both are +
push offset inputBuf2
push offset inputBuf1
call unsignedAdd
jmp endOfSelection
firstPlusSecondMinus:
;+-
mov [di], byte ptr 0
push offset inputBuf2
push offset inputBuf1
call unsignedSub
jmp endOfSelection
firstMinus:
cmp [di], byte ptr '-'
je firstMinusSecondMinus
;-+
mov dx, offset incorrectOrderStr
mov ah, 09h
int 21h
mov ah, 4Ch
int 21h
firstMinusSecondMinus:
mov [di], byte ptr 0
mov [si], byte ptr 0
push offset inputBuf2
push offset inputBuf1
call unsignedAdd
mov ah, 09h
mov dx, offset minusStr
int 21h
jmp endOfSelection
endOfSelection:
push offset result
call arrIntToChar
mov ah, 09h
mov dx, offset result
int 21h
ret
compute endp
start:
mov AX, data
mov DS, AX
call compute
mov ah, 4Ch
int 21h
code ends
end start