This repository was archived by the owner on Apr 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmean.asm
More file actions
807 lines (686 loc) · 12.6 KB
/
mean.asm
File metadata and controls
807 lines (686 loc) · 12.6 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
.286
.model small
.stack 100h
.data
overflow_message db "Overflow occurred", 0Ah, 0Dh, '$'
new_line_message db 0Ah, 0Dh, '$'
invalid_value_message db "Invalid value. Try again...", 0Ah, 0Dh, '$'
count_prompt_message db "Enter count of elements: ", '$'
count_invalid_message db "Count must be in range (0, 30]", 0Ah, 0Dh, '$'
array_prompt_start db "Enter ", '$'
array_prompt_end db " numbers: ", 0Ah, 0Dh, '$'
mean_message db "Mean value = ", '$'
sum_message db "Sum = ", '$'
is_negative_result dw 0
is_zero_result dw 0
division_value dw 0
divisor_value dw 0
division_result_int dw 0
division_result_frac dw 0
numbers_count_valid dw 0
numbers_count dw 0
numbers_array dw 30 dup (0)
numbers_sum dw 0
numbers_mean_int dw 0
numbers_mean_frac dw 0
is_number_result dw 0
buffer_max db 10
buffer_size db 0
buffer db 10 dup (0)
parse_result dw 0
parse_success dw 0
print_int_value dw 0
print_int_sign_flag dw 0
print_float_int_value dw 0
print_float_frac_value dw 0
.code
zero_set macro
mov is_zero_result, 01h
endm
zero_unset macro
mov is_zero_result, 00h
endm
new_line proc
pusha
mov dx, offset new_line_message
mov ax, 00h
mov ah, 09h
int 21h
popa
ret
new_line endp
invalid_value proc
pusha
mov dx, offset invalid_value_message
mov ax, 00h
mov ah, 09h
int 21h
popa
ret
invalid_value endp
count_prompt proc
pusha
mov dx, offset count_prompt_message
mov ax, 00h
mov ah, 09h
int 21h
popa
ret
count_prompt endp
count_error proc
pusha
mov dx, offset count_invalid_message
mov ax, 00h
mov ah, 09h
int 21h
popa
ret
count_error endp
array_prompt proc
pusha
mov dx, offset array_prompt_start
mov ax, 00h
mov ah, 09h
int 21h
mov dx, numbers_count
mov print_int_value, dx
mov print_int_sign_flag, 01h
call print_int
mov dx, offset array_prompt_end
mov ax, 00h
mov ah, 09h
int 21h
popa
ret
array_prompt endp
overflow proc
mov dx, offset overflow_message
mov ah, 09h
int 21h
jmp exit
ret
overflow endp
is_negative proc
push dx
mov dx, ax
shr dx, 15
mov is_negative_result, dx
pop dx
ret
is_negative endp
is_zero proc
cmp ax, 00h
je is_zero_set
jne is_zero_unset
is_zero_set:
zero_set
jmp is_zero_ret
is_zero_unset:
zero_unset
jmp is_zero_ret
is_zero_ret:
ret
is_zero endp
int_negative_divide proc
push dx
push cx
mov dx, 00h
int_negative_divide_for_loop:
push ax
add ax, bx
call is_negative
call is_zero
pop ax
mov cx, is_negative_result
or cx, is_zero_result
cmp cx, 00h
je int_negative_divide_for_loop_end
dec dx
add ax, bx
jmp int_negative_divide_for_loop
int_negative_divide_for_loop_end:
mov bx, dx
pop cx
pop dx
ret
int_negative_divide endp
int_divide proc
push dx
cmp ax, 00h
je int_divide_zero
cmp bx, 00h
je int_divide_zero
call is_negative
cmp is_negative_result, 01h
je int_divide_negative
cmp ax, bx
jl int_divide_zero_int
mov dx, 00h
int_divide_for_while_loop:
inc dx
sub ax, bx
cmp ax, bx
jl int_divide_for_while_loop_end
jmp int_divide_for_while_loop
int_divide_for_while_loop_end:
mov bx, dx
jmp int_divide_ret
int_divide_zero_int:
mov bx, 00h
jmp int_divide_ret
int_divide_negative:
call int_negative_divide
jmp int_divide_ret
int_divide_zero:
mov ax, 00h
mov bx, 00h
jmp int_divide_ret
int_divide_ret:
pop dx
ret
int_divide endp
frac_negative_divide proc
push cx
push dx
mov dx, 00h
mov cx, 03h
frac_negative_divide_for_loop:
cmp ax, 00h
je frac_negative_divide_for_loop_end
push cx
frac_negative_divide_while:
push ax
add ax, bx
call is_negative
call is_zero
pop ax
mov cx, is_negative_result
xor cx, 01h
or cx, is_zero_result
cmp cx, 01h
jne frac_negative_divide_while_end
push dx
mov cx, 0ah
imul cx
pop dx
push ax
mov cx, 0ah
mov ax, dx
imul cx
mov dx, ax
pop ax
jmp frac_negative_divide_while
frac_negative_divide_while_end:
frac_negative_divide_add_loop:
push ax
add ax, bx
call is_negative
call is_zero
pop ax
mov cx, is_negative_result
or cx, is_zero_result
cmp cx, 01h
jne frac_negative_divide_add_loop_end
add ax, bx
dec dx
jmp frac_negative_divide_add_loop
frac_negative_divide_add_loop_end:
pop cx
loop frac_negative_divide_for_loop
frac_negative_divide_for_loop_end:
mov ax, dx
jmp frac_negative_divide_ret
frac_negative_divide_ret:
pop dx
pop cx
ret
frac_negative_divide endp
frac_divide proc
push cx
push dx
cmp ax, 00h
je frac_divide_ret_zero
cmp bx, 00h
je frac_divide_ret_zero
call is_negative
cmp is_negative_result, 01h
je frac_divide_negative
mov dx, 00h
mov cx, 03h
frac_divide_for_loop:
cmp ax, 00h
je frac_divide_for_loop_end
push cx
frac_divide_while_loop:
cmp ax, bx
jge frac_divide_while_loop_end
push dx
mov cx, 0ah
imul cx
pop dx
push ax
mov ax, dx
mov cx, 0ah
imul cx
mov dx, ax
pop ax
jmp frac_divide_while_loop
frac_divide_while_loop_end:
frac_divide_sub_loop:
cmp ax, bx
jl frac_divide_sub_loop_end
inc dx
sub ax, bx
jmp frac_divide_sub_loop
frac_divide_sub_loop_end:
pop cx
loop frac_divide_for_loop
frac_divide_for_loop_end:
mov ax, dx
jmp frac_divide_ret
frac_divide_negative:
call frac_negative_divide
jmp frac_divide_ret
frac_divide_ret_zero:
mov ax, 00h
jmp frac_divide_ret
frac_divide_ret:
pop dx
pop cx
ret
frac_divide endp
divide proc
push ax
push bx
mov ax, division_value
mov bx, divisor_value
call int_divide
mov division_result_int, bx
mov bx, divisor_value
call frac_divide
mov division_result_frac, ax
pop bx
pop ax
ret
divide endp
sum proc
push ax
push bx
push cx
push dx
push si
mov dx, 00h
mov si, offset numbers_array
mov cx, numbers_count
sum_for_loop:
push cx
mov ax, dx
call is_negative
push is_negative_result
mov ax, [si]
call is_negative
push is_negative_result
add ax, dx
mov dx, ax
call is_negative
pop ax
pop bx
xor ax, bx
pop cx
cmp ax, 00h
jne sum_for_loop_no_overflow
xor bx, is_negative_result
cmp bx, 00h
je sum_for_loop_no_overflow
call overflow ; when sum overflow occurrs just terminate
sum_for_loop_no_overflow:
add si, 2
loop sum_for_loop
mov numbers_sum, dx
pop si
pop dx
pop cx
pop bx
pop ax
ret
sum endp
mean proc
push dx
mov dx, numbers_sum
mov division_value, dx
mov dx, numbers_count
mov divisor_value, dx
call divide
mov dx, division_result_int
mov numbers_mean_int, dx
mov dx, division_result_frac
mov numbers_mean_frac, dx
pop dx
ret
mean endp
is_number proc
cmp dl, '0'
jl is_number_unset
cmp dl, '9'
jg is_number_unset
jmp is_number_set
is_number_unset:
mov is_number_result, 00h
jmp is_number_ret
is_number_set:
mov is_number_result, 01h
jmp is_number_ret
is_number_ret:
ret
endp
parse_negative_int proc
mov ax, 00h
parse_negative_int_for_loop:
mov dx, 00h
mov dl, [si]
call is_number
cmp is_number_result, 01h
jne parse_negative_int_invalid_char
sub dl, '0'
cmp ax, 00h
jne parse_negative_int_for_loop_continue
cmp dl, 00h
jne parse_negative_int_for_loop_continue
jmp parse_negative_for_loop_no_overflow
parse_negative_int_for_loop_continue:
push cx
mov cx, 0ah
push dx
imul cx
jo parse_negative_for_loop_overflow_occurred
pop dx
sub ax, dx
pop cx
call is_negative
cmp is_negative_result, 01h
je parse_negative_for_loop_no_overflow
jne parse_negative_for_loop_overflow_occurred_im
parse_negative_for_loop_overflow_occurred:
pop dx
pop cx
parse_negative_for_loop_overflow_occurred_im:
jmp parse_negative_int_invalid_char
parse_negative_for_loop_no_overflow:
inc si
loop parse_negative_int_for_loop
mov parse_success, 01h
mov parse_result, ax
jmp parse_negative_int_ret
parse_negative_int_invalid_char:
mov parse_success, 00h
mov parse_result, 00h
jmp parse_negative_int_ret
parse_negative_int_ret:
ret
parse_negative_int endp
parse_int proc
pusha
mov si, offset buffer
mov cx, 00h
mov cl, buffer_size
mov dx, 00h
mov dl, [si]
cmp dl, '-'
je parse_int_negative
mov ax, 00h
parse_int_for_loop:
mov dx, 00h
mov dl, [si]
call is_number
cmp is_number_result, 01h
jne parse_int_invalid_char
; multiply ax by 10
; and add dl to it
; why the fuck am i multiplying dx by 10???
push cx
sub dl, '0'
mov cx, 0ah
push dx
imul cx
jo parse_for_loop_overflow_occurred
pop dx
add ax, dx
pop cx
call is_negative
cmp is_negative_result, 00h
je parse_for_loop_no_overflow
jne parse_for_loop_overflow_occurred_im
parse_for_loop_overflow_occurred:
pop dx
pop cx
parse_for_loop_overflow_occurred_im:
jmp parse_int_invalid_char ; was call overflow
parse_for_loop_no_overflow:
inc si
loop parse_int_for_loop
mov parse_success, 01h
mov parse_result, ax
jmp parse_int_ret
parse_int_negative:
inc si
dec cx
call parse_negative_int
jmp parse_int_ret
parse_int_invalid_char:
mov parse_success, 00h
mov parse_result, 00h
jmp parse_int_ret
parse_int_ret:
popa
ret
parse_int endp
read_buffer proc
pusha
mov dx, offset buffer_max
mov ah, 0ch
mov al, 0ah
int 21h
popa
ret
endp
print_char proc
pusha
mov ax, 00h
mov ah, 02h
int 21h
popa
ret
print_char endp
print_int proc
pusha
mov ax, print_int_value
call is_negative
mov cx, is_negative_result
and cx, print_int_sign_flag
cmp cx, 00h
jne print_int_set_minus
jmp print_int_start
print_int_set_minus:
mov dx, 00h
mov dl, '-'
call print_char
jmp print_int_start
print_int_start:
mov dx, 00h
mov cx, 00h
print_int_while_loop:
mov bx, 0ah
call int_divide
push bx
call is_negative
cmp is_negative_result, 01h
je print_int_put_negative_int
jmp print_int_put_int
print_int_put_negative_int:
mov bx, ax
mov ax, 00h
sub ax, bx
jmp print_int_put_int
print_int_put_int:
add ax, '0'
mov dx, ax
pop bx
push dx
inc cx
mov ax, bx
call is_zero
cmp ax, 00h
je print_int_while_loop_end
jmp print_int_while_loop
print_int_while_loop_end:
print_int_put_from_stack:
pop dx
call print_char
loop print_int_put_from_stack
print_int_ret:
popa
ret
print_int endp
print_float proc
pusha
mov ax, print_float_int_value
call is_negative
mov cx, is_negative_result
mov ax, print_float_frac_value
call is_negative
or cx, is_negative_result
cmp cx, 00h
jne print_float_minus
jmp print_float_continue
print_float_minus:
mov dx, '-'
call print_char
jmp print_float_continue
print_float_continue:
mov dx, print_float_int_value
mov print_int_value, dx
mov print_int_sign_flag, 00h
call print_int
mov dx, ','
call print_char
mov dx, print_float_frac_value
mov print_int_value, dx
mov print_int_sign_flag, 00h
call print_int
mov print_int_sign_flag, 01h
popa
ret
print_float endp
handle_input proc
pusha
mov cx, numbers_count
mov si, offset numbers_array
handle_input_for_loop:
call read_buffer
call parse_int
call new_line
cmp parse_success, 01h
je handle_input_success
jne handle_input_failed
handle_input_success:
mov ax, parse_result
mov [si], ax
add si, 2
jmp handle_input_continue
handle_input_failed:
inc cx
call invalid_value
jmp handle_input_continue
handle_input_continue:
loop handle_input_for_loop
popa
ret
handle_input endp
is_count_valid proc
pusha
mov ax, numbers_count
call is_negative
cmp is_negative_result, 00h
jne is_count_valid_invalid
cmp ax, 00h
je is_count_valid_invalid
cmp ax, 30
jg is_count_valid_invalid
jmp is_count_valid_ok
is_count_valid_ok:
mov numbers_count_valid, 01h
jmp is_count_valid_ret
is_count_valid_invalid:
mov numbers_count_valid, 00h
jmp is_count_valid_ret
is_count_valid_ret:
popa
ret
is_count_valid endp
handle_count proc
pusha
handle_count_loop:
call count_prompt
call read_buffer
call parse_int
call new_line
mov dx, parse_result
mov numbers_count, dx
mov cx, parse_success
call is_count_valid
and cx, numbers_count_valid
cmp cx, 00h
jne handle_count_ret
call count_error
jmp handle_count_loop
handle_count_ret:
popa
ret
handle_count endp
print_sum proc
pusha
mov dx, offset sum_message
mov ax, 00h
mov ah, 09h
int 21h
mov dx, numbers_sum
mov print_int_value, dx
mov print_int_sign_flag, 01h
call print_int
call new_line
popa
ret
print_sum endp
print_mean proc
pusha
mov dx, offset mean_message
mov ax, 00h
mov ah, 09h
int 21h
mov dx, numbers_mean_int
mov print_float_int_value, dx
mov dx, numbers_mean_frac
mov print_float_frac_value, dx
call print_float
call new_line
popa
ret
print_mean endp
start:
mov ax, @data
mov ds, ax
mov es, ax
main:
call handle_count
call new_line
call array_prompt
call handle_input
call sum
call mean
call new_line
call print_sum
call print_mean
exit:
mov ax, 4c00h
int 21h
end start