-
Notifications
You must be signed in to change notification settings - Fork 0
/
det.asm
728 lines (572 loc) · 10.6 KB
/
det.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
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
section .data
SYS_EXIT equ 1
SYS_WRITE equ 4
SYS_READ equ 3
KEYBOARD_SCREEN equ 1 ; the type of input/output devices (keybord, console)
dimen db 0
miss db 0
print_invert db 0 ; for print_num procedure: if you want inverted output set it to 1
det dw 0
singmap dw 0 ; singmap for the matrix; Note: with dw matrix cant store more than 16 values
negnum db 0
; Messages
msg_intro db 'The program for calculating the determinant of the matrix', 0xa, 0xa
msg_intro_len equ $ - msg_intro
msg_dimen db 'Dimension of the matrix = '
msg_dimen_len equ $ - msg_dimen
msg_GenInvit db 0xa, 'Input values for elements of the matrix:', 0xa
msg_GenInvit_len equ $ - msg_GenInvit
msg_invit1 db 'matrix['
msg_invit1_len equ $ - msg_invit1
msg_invit2 db ']['
msg_invit2_len equ $ - msg_invit2
msg_invit3 db '] = '
msg_invit3_len equ $ - msg_invit3
msg_IncorInput db 'The value is not correct. Try again: '
msg_IncorInput_len equ $ - msg_IncorInput
msg_IncorDimen db 'The value of dimension is not correct. It can not be more than 4. Try again: ', 0xa
msg_IncorDimen_len equ $ - msg_IncorDimen
msg_space db 0x09
msg_space_len equ $ - msg_space
msg_NewLine db 0xa
msg_NewLine_len equ $ - msg_NewLine
msg_ForMatrix db 0xa, 'For the matrix:', 0xa
msg_ForMatrix_len equ $ - msg_ForMatrix
msg_DetIs db 'the determinant = '
msg_DetIs_len equ $ - msg_DetIs
msg_minus db '-'
msg_minus_len equ $ - msg_minus
matrix times 16 dw '0'
section .bss
i resb 1
j resb 1
num resw 1
temp resb 1
rank resb 1
cur_rank resb 1
nod resb 1
tempd resd 1
tempw resd 1
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_intro
mov edx, msg_intro_len
int 0x80
input_dimen:
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_dimen
mov edx, msg_dimen_len
int 0x80
call read_num
mov [dimen], al
cmp al, 5
JL correct_dimen
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_IncorDimen
mov edx, msg_IncorDimen_len
int 0x80
JMP input_dimen
correct_dimen:
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_GenInvit
mov edx, msg_GenInvit_len
int 0x80
lea esi, [matrix] ; an index of the first element of the matrix
mov dl, [dimen] ; a dimension of the matrix
call read_matrix
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_ForMatrix
mov edx, msg_ForMatrix_len
int 0x80
lea esi, [matrix] ; an index of the first element of the matrix
mov dl, [dimen] ; a dimension of the matrix
call print_matrix
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_DetIs
mov edx, msg_DetIs_len
int 0x80
lea esi, [matrix] ; an index of the first element of the matrix
mov dl, [dimen] ; a dimension of the matrix
cmp dl, 2
JE det_for_2
cmp dl, 3
JE det_for_3
cmp dl, 4
JE det_for_4
det_for_2:
mov al, [esi]
mov dl, [esi+3]
imul dl
mov bx, ax
mov al, [esi+2]
mov dl, [esi+1]
imul dl
sub bx, ax
mov [det], bx
JMP out_of_det
det_for_3:
mov al, [esi]
mov dl, [esi+4]
imul dl
mov dl, [esi+8]
imul dl
mov [det], ax
mov al, [esi+6]
mov dl, [esi+1]
imul dl
mov dl, [esi+5]
imul dl
add [det], ax
mov al, [esi+3]
mov dl, [esi+7]
imul dl
mov dl, [esi+2]
imul dl
add [det], ax
mov al, [esi+6]
mov dl, [esi+4]
imul dl
mov dl, [esi+2]
imul dl
sub [det], ax
mov al, [esi]
mov dl, [esi+7]
imul dl
mov dl, [esi+5]
imul dl
sub [det], ax
mov al, [esi+3]
mov dl, [esi+1]
imul dl
mov dl, [esi+8]
imul dl
sub [det], ax
JMP out_of_det
det_for_4:
mov word [tempw], 0
mov al, [esi+5]
mov dl, [esi+10]
imul dl
mov dl, [esi+15]
imul dl
mov [tempw], ax
mov al, [esi+6]
mov dl, [esi+11]
imul dl
mov dl, [esi+13]
imul dl
add [tempw], ax
mov al, [esi+9]
mov dl, [esi+7]
imul dl
mov dl, [esi+14]
imul dl
add [tempw], ax
mov al, [esi+13]
mov dl, [esi+10]
imul dl
mov dl, [esi+7]
imul dl
sub [tempw], ax
mov al, [esi+5]
mov dl, [esi+11]
imul dl
mov dl, [esi+14]
imul dl
sub [tempw], ax
mov al, [esi+6]
mov dl, [esi+15]
imul dl
mov dl, [esi+9]
imul dl
sub [tempw], ax
mov ax, [tempw]
mov dl, [esi]
imul dl
mov [det], ax
;
mov word [tempw], 0
mov al, [esi+4]
mov dl, [esi+10]
imul dl
mov dl, [esi+15]
imul dl
mov [tempw], ax
mov al, [esi+6]
mov dl, [esi+11]
imul dl
mov dl, [esi+12]
imul dl
add [tempw], ax
mov al, [esi+8]
mov dl, [esi+7]
imul dl
mov dl, [esi+14]
imul dl
add [tempw], ax
mov al, [esi+12]
mov dl, [esi+10]
imul dl
mov dl, [esi+7]
imul dl
sub [tempw], ax
mov al, [esi+4]
mov dl, [esi+11]
imul dl
mov dl, [esi+14]
imul dl
sub [tempw], ax
mov al, [esi+6]
mov dl, [esi+15]
imul dl
mov dl, [esi+8]
imul dl
sub [tempw], ax
mov ax, [tempw]
mov dl, [esi+1]
imul dl
sub [det], ax
;
mov word [tempw], 0
mov al, [esi+4]
mov dl, [esi+9]
imul dl
mov dl, [esi+15]
imul dl
mov [tempw], ax
mov al, [esi+5]
mov dl, [esi+11]
imul dl
mov dl, [esi+12]
imul dl
add [tempw], ax
mov al, [esi+8]
mov dl, [esi+7]
imul dl
mov dl, [esi+13]
imul dl
add [tempw], ax
mov al, [esi+12]
mov dl, [esi+9]
imul dl
mov dl, [esi+7]
imul dl
sub [tempw], ax
mov al, [esi+4]
mov dl, [esi+11]
imul dl
mov dl, [esi+13]
imul dl
sub [tempw], ax
mov al, [esi+5]
mov dl, [esi+15]
imul dl
mov dl, [esi+8]
imul dl
sub [tempw], ax
mov ax, [tempw]
mov dl, [esi+2]
imul dl
add [det], ax
;
mov word [tempw], 0
mov al, [esi+4]
mov dl, [esi+9]
imul dl
mov dl, [esi+14]
imul dl
mov [tempw], ax
mov al, [esi+5]
mov dl, [esi+10]
imul dl
mov dl, [esi+12]
imul dl
add [tempw], ax
mov al, [esi+8]
mov dl, [esi+6]
imul dl
mov dl, [esi+13]
imul dl
add [tempw], ax
mov al, [esi+12]
mov dl, [esi+9]
imul dl
mov dl, [esi+6]
imul dl
sub [tempw], ax
mov al, [esi+4]
mov dl, [esi+10]
imul dl
mov dl, [esi+13]
imul dl
sub [tempw], ax
mov al, [esi+5]
mov dl, [esi+14]
imul dl
mov dl, [esi+8]
imul dl
sub [tempw], ax
mov ax, [tempw]
mov dl, [esi+3]
imul dl
sub [det], ax
;
out_of_det:
mov ax, [det]
call print_num
mov eax, 1 ;system call number (sys_exit)
mov ebx, 0 ;success exit status
int 0x80 ;call kernel
; HowToUse (Example)
;
; lea esi, [matrix] ; an index of the first element of the matrix
; mov dl, [dimen] ; a dimension of the matrix
; call print_matrix
print_matrix:
mov byte [j], 1
mov al, dl
mul dl
mov cl, al
L3:
pusha
mov al, [esi]
cbw
call print_num
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_space
mov edx, msg_space_len
int 0x80
popa
inc esi
cmp [j], dl ; where dl --dimension of matrix
JE p_new_row
inc byte [j]
JMP p_the_same_row
p_new_row:
mov byte [j], 1
pusha
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_NewLine
mov edx, msg_NewLine_len
int 0x80
popa
p_the_same_row:
dec cl
JNZ L3
ret
; HowToUse (Example)
;
; lea esi, [matrix] ; an index of the first element of the matrix
; mov dl, [dimen] ; a dimension of the matrix
; call read_matrix
read_matrix:
mov byte [i], 1
mov byte [j], 1
mov al, dl
mul dl
mov cl, al
L2:
pusha
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_invit1
mov edx, msg_invit1_len
int 0x80
mov al, [i]
cbw
call print_num
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_invit2
mov edx, msg_invit2_len
int 0x80
mov al, [j]
cbw
call print_num
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_invit3
mov edx, msg_invit3_len
int 0x80
call read_num
mov [esi], al
popa
inc esi
cmp [j], dl ; where dl --dimension of matrix
JE new_row
inc byte [j]
JMP the_same_row
new_row:
mov byte [j], 1
inc byte [i]
the_same_row:
dec cl
JNZ L2
ret
; HowToUse (Example)
; Example_1:
; mov al, [j]
; cbw ; extension from 8 bites to 16 bites
; call print_num
;
; Example_2:
; mov ax, [j]
; call print_num
print_num:
pusha
cmp ax, 0
JE print_0
mov [num], ax
shr ax, 15
cmp ax, 0
JE positive_num_p
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_minus
mov edx, msg_minus_len
int 0x80
neg word [num]
positive_num_p:
mov byte [nod], 0
mov [tempd], esp
extract_no:
cmp word [num], 0
JE print_no
inc byte [nod]
mov dx, 0
mov ax, word [num]
mov bx, 10
div bx
push dx
mov word [num], ax
JMP extract_no
print_no:
cmp byte [nod], 0
JE end_print
dec byte [nod]
cmp byte [print_invert], 1
JE invert
pop dx
JMP not_invert
invert:
mov ebp, esp
mov al, [nod]
mov dx, [ebp + eax*2]
not_invert:
mov byte [temp], dl
add byte [temp], '0'
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, temp
mov edx, 1
int 0x80
JMP print_no
end_print:
mov esp, [tempd]
popa
ret
print_0:
mov byte [temp], '0'
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, temp
mov edx, 1
int 0x80
popa
ret
; HowToUse (Example)
;
; call read_num
; mov [num], al
read_num:
mov byte [num], 0
mov byte [miss], 0
mov byte [rank], 0
mov byte [cur_rank], 0
mov byte [negnum], 0
mov ebp, esp ;save state of stack
read_NewChar:
mov eax, SYS_READ
mov ebx, KEYBOARD_SCREEN
mov ecx, temp
mov edx, 1
int 0x80
cmp byte [miss], '1'
JE not_num
cmp byte [temp], '-'
JNE check_num
cmp byte [rank], 0
JNE set_miss
mov byte [negnum], 1
JMP read_NewChar
check_num:
cmp byte [temp], '0'
JL not_num
cmp byte [temp], '9'
JG not_num
inc byte [rank]
mov al, [rank]
cmp al, 2
JG not_num
mov al, byte [temp]
push ax
JMP read_NewChar
not_num:
cmp byte [temp], 0xa
JE end_read
set_miss:
mov byte [miss], 1
JMP read_NewChar
end_read:
cmp byte [miss], 0
JNE input_error
cmp byte [rank], 0
JE input_error
get_new_rank:
pop dx
sub dl, '0' ;convert from ascii to decimal
cmp byte [cur_rank], 0
JG cur_rank_more_0
mov bl, dl
mov [num], bl
JMP cur_rank_0
cur_rank_more_0:
mov al, 1
mov cl, [cur_rank]
L1:
mov bl, 10
mul bl
dec cl
JNZ L1
mul dl
add [num], ax
cur_rank_0:
inc byte [cur_rank]
mov al, [rank]
cmp byte [cur_rank], al
JNE get_new_rank
mov al, 1
cmp byte [negnum], al
JNE positive_num
neg byte [num]
positive_num:
mov al, [num]
ret
input_error:
mov eax, SYS_WRITE
mov ebx, KEYBOARD_SCREEN
mov ecx, msg_IncorInput
mov edx, msg_IncorInput_len
int 0x80
mov esp, ebp
JMP read_num