-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.asm
441 lines (380 loc) · 7.51 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
; 2 labaratorine uzduotis 18 variantas Antanas Vasiliauskas
%include 'yasmmac.inc'
org 100h
section .text
startas:
macPutString 'Antanas Vasiliauskas 1 kursas 3 grupe', crlf, '$'
mov bl, byte [0x80] ;- command line argument length
mov bh, 0
mov byte [0x81+bx], 00 ;- remove 0D from command line argument
; Open file
mov dx, 0x82
call procFOpenForReading
mov ax, 0
adc ax, 0
cmp ax, 0
jz opened_input
macPutString 'Nepavyko atidaryti duomenu failo', crlf, '$'
jmp program_end
opened_input:
macPutString 'Iveskite rezultatu failo pavadinima', crlf, '$'
mov al, 128
mov dx, output_filename
call procGetStr
macNewLine
call read_line ; ignore first line
read_loop:
call read_line
cmp ax, 0 ; - ax - bytes read. If 0 means EOF
jz eof_global
call five_digit_numbers_in_line
call add_floating_number_in_line
add [number_count], ax
jmp read_loop
eof_global:
call procFClose
;; Total numbers
mov ax, [number_count]
mov dx, number_count_str
call procUInt16ToStr
mov ax, [decimal_sum]
mov dx, decimal_sum_str
cmp ax, -1
jg NotNegative2
neg ax
mov byte [is_negative], 1
NotNegative2:
call procInt16ToStr
mov ax, [floating_sum]
mov dx, floating_sum_str
cmp ax, -1
jg NotNegative
neg ax
mov byte [is_negative], 1
NotNegative:
call procInt16ToStr
cmp ax, 10
jg SkipAddZero
mov al, [floating_sum_str]
mov byte [floating_sum_str], 0x30
mov [floating_sum_str+1], al
mov byte [floating_sum+2], 00
SkipAddZero:
; Create file
mov cx, 0
mov dx, output_filename
mov ah, 3Ch
int 0x21
mov dx, output_filename
call procFOpenForWriting
mov dx, number_count_str
push bx
mov bx, -1
number_of_bytes_loop:
inc bx
cmp byte [number_count_str+bx], 00
jnz number_of_bytes_loop
mov byte [number_count_str+bx], 0x0A
mov byte [number_count_str+bx+1], 0x0D
inc bx
mov cx, bx
pop bx
call procFWrite
cmp byte [is_negative], 1
jnz OmitMinus
push ax
push cx
push dx
mov dx, minus
inc dx
mov cx, 1
call procFWrite
pop dx
pop cx
pop ax
OmitMinus:
mov dx, decimal_sum_str
push bx
mov bx, -1
number_of_bytes_loop2:
inc bx
cmp byte [decimal_sum_str+bx], 00
jnz number_of_bytes_loop2
mov byte [decimal_sum_str+bx], '.'
mov byte [decimal_sum_str+bx+1], 0x0D
inc bx
mov cx, bx
pop bx
call procFWrite
mov dx, floating_sum_str
push bx
mov bx, -1
number_of_bytes_loop3:
inc bx
cmp byte [floating_sum_str+bx], 00
jnz number_of_bytes_loop3
mov byte [floating_sum_str+bx], 00
cmp word [floating_sum], 10
jge MoreThanNine
cmp word [floating_sum], -10
jle MoreThanNine
dec bx
MoreThanNine:
mov cx, bx
pop bx
call procFWrite
call procFClose
program_end:
mov ah, 4Ch
int 21h
read_line:
; Input: bx - file descriptor, variable buffer 256 bytes reserved, variable cursor: dw storing offset from file orgin.
; Output ax - how many bytes read. If 0 means EOF. Buffer is filled with string line. Newline is replaced with '$'. Cursor is changed to point to next line.
push bx ; - File descriptor
; Read 256 bytes to the buffer
mov dx, buffer
mov cx, 256
mov ah, 0x3F
int 0x21
; If EOF, end
cmp ax, 0
jz eof
;find newline
mov bx, 0
cr_loop:
inc bx
cmp byte [bx + buffer - 1], 0Ah
jnz cr_loop
add [cursor], bx
mov byte [bx+buffer], '$'
; fseek to cursor (last newline)
pop bx
push ax
push bx
push cx
push dx
mov ah, 42h
mov al, 0
mov cx, 0
mov dx, [cursor]
int 21h
pop dx
pop cx
pop bx
pop ax
push bx
eof:
pop bx
ret
five_digit_numbers_in_line:
; returns result in ax
push bx
push cx
push dx
mov ax, 0 ; - number count
mov bx, 0 ; - index in buffer
mov cl, 1 ; - is number
mov ch, 0 ; - non-zero reached
mov dl, 0 ; - digit count
mov dh, 1 ; - current cell
mov bx, -1
buffer_loop:
mov ch, 0
inc bx
cmp byte [buffer+bx], 0x3B
jnz Mark2
inc dh
jmp Mark3
Mark2:
cmp byte [buffer+bx], 0x20
jnz Mark4
Mark3:
cmp cl, 0
jz Mark5
cmp dl, 5
jnz Mark5
inc ax
Mark5:
mov cl, 1
mov dl, 0
cmp dh, 2
jg return
jmp buffer_loop
Mark4:
cmp cl, 0
jz buffer_loop
cmp byte [buffer+bx], 0x31
jl Mark10
cmp byte [buffer+bx], 0x39
jg Mark10
inc dl
mov ch, 1
jmp buffer_loop
Mark10:
cmp byte [buffer+bx], 0x30
jnz Mark6
cmp ch, 0
jz buffer_loop
inc dl
jmp buffer_loop
Mark6:
cmp dl, 0
jnz Mark8
cmp byte [buffer + bx], 0x2B
jz Mark7
cmp byte [buffer + bx], 0x2D
jnz Mark8
Mark7:
jmp buffer_loop
Mark8:
mov cl, 0
jmp buffer_loop
return:
pop dx
pop cx
pop bx
ret
add_floating_number_in_line:
; Changes decimal_sum and floating_sum variables internally
; Register states are preserved
push ax
push bx
push cx
push dx
; ah = cell
; al = is_positive
; ch = which_number
; bx = bx
mov ah, 1
mov al, 1
mov ch, 1
mov bx, -1
Loop2:
inc bx
cmp byte [buffer+bx], 0x0A
jz OutLoop2
cmp byte [buffer+bx], 0x0D
jz OutLoop2
cmp byte [buffer+bx], 0x3B
jnz L1
inc ah
jmp Loop2
L1:
cmp ah, 6
jl Loop2
cmp byte [buffer+bx], 0x2B
jz Loop2
cmp byte [buffer+bx], 0x2D
jnz L2
mov al, 0
jmp Loop2
L2:
cmp ch, 1
jnz L3
inc ch
cmp al, 0
jz L4
mov dl, [buffer+bx]
mov dh, 0
sub dx, 0x30
add [decimal_sum], dx
jmp L5
L4:
mov dl, [buffer+bx]
mov dh, 0
sub dx, 0x30
sub [decimal_sum], dx
L5:
inc bx
jmp Loop2
L3:
cmp ch, 2
jnz L6
inc ch
cmp al, 0
jz L7
push ax
push dx
mov al, [buffer+bx]
mov ah, 0
sub ax, 0x30
mov cl, 10
imul cl
add [floating_sum], ax
pop dx
pop ax
jmp L8
L7:
push ax
push dx
mov ax, [buffer+bx]
mov ah, 0
sub ax, 0x30
mov cl, 10
imul cl
sub [floating_sum], ax
pop dx
pop ax
L8:
jmp Loop2
L6:
cmp al, 0
jz L10
mov dl, [buffer+bx]
mov dh, 0
sub dx, 0x30
add [floating_sum], dx
jmp OutLoop2
L10:
mov dl, [buffer+bx]
mov dh, 0
sub dx, 0x30
sub [floating_sum], dx
OutLoop2:
pop dx
pop cx
pop bx
pop ax
cmp word [floating_sum], 99
jg L12
cmp word [floating_sum], -99
jl L13
ret
L12:
sub word [floating_sum], 100
inc word [decimal_sum]
ret
L13:
add word [floating_sum], 100
dec word [decimal_sum]
ret
%include 'yasmlib.asm'
section .data
input_filename:
;'failas.csv', 00
times 256 db 00
output_filename:
times 256 db 00
debug:
db 00, 00, crlf, '$'
buffer:
times 256 db 00, crlf, '$'
cursor:
dw 0000
number_count:
dw 0000
number_count_str:
times 16 db 00, crlf, '$'
decimal_sum_str:
times 16 db 00, crlf, '$'
floating_sum_str:
times 16 db 00, crlf, '$'
decimal_sum:
dw 0000
floating_sum:
dw 0000
minus:
times 256 db 0x2d
is_negative:
db 00
section .bss