-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.asm
1233 lines (937 loc) · 15.2 KB
/
lib.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
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
global intCmp
global intClone
global intDelete
global intPrint
global strCmp
global strClone
global strDelete
global strPrint
global strLen
global arrayNew
global arrayGetSize
global arrayAddLast
global arrayGet
global arrayRemove
global arraySwap
global arrayDelete
global listNew
global listGetSize
global listAddFirst
global listGet
global listRemove
global listSwap
global listClone
global listDelete
global cardNew
global cardGetSuit
global cardGetNumber
global cardGetStacked
global cardCmp
global cardClone
global cardAddStacked
global cardDelete
global cardPrint
section .data
formato: db '%d', 0
formato_2: db '%s', 0
formato_3: db '{%s-',0
formato_4: db '%d-',0
formato_5: db '}', 0
formato_6: db "{",0
formato_7: db "-",0
formato_8: db "}",0
formato_9: db "[]", 0
formato_10: db "NULL", 0
section .text
extern malloc
extern free
extern fprintf
extern getDeleteFunction
extern getCloneFunction
extern listAddLast
extern listPrint
; ** Int **
; int32_t intCmp(int32_t* a, int32_t* b)
intCmp:
PUSH RBP
MOV RBP, RSP
SUB RSP, 8
PUSH RBX
MOV EBX, [RDI]
CMP EBX, [RSI]
JZ cero
JG menosUno
MOV EAX, 1
JMP fin
cero:
MOV EAX, 0
JMP fin
menosUno:
MOV EAX, -1
fin:
POP RBX
ADD RSP, 8
POP RBP
ret
; int32_t* intClone(int32_t* a)
intClone:
PUSH RBP
MOV RBP, RSP
SUB RSP, 8
PUSH RBX
mov ebx, [RDI]
mov rdi, 4
call malloc
mov [rax], ebx
POP RBX
ADD RSP, 8
POP RBP
ret
; void intDelete(int32_t* a)
intDelete:
push RBP
MOV RBP, RSP
call free
POP RBP
ret
; void intPrint(int32_t* a, FILE* pFile)
intPrint:
PUSH RBP
MOV RBP, RSP
mov edx, [rdi]
mov rdi, rsi
mov rsi, formato
call fprintf
POP RBP
RET
; ** String **
; int32_t strCmp(char* a, char* b)
strCmp:
push rbp
mov rbp, rsp
sub rsp, 8
push r12
push r13
push r14
push r15
push rbx
call strLen
mov r12, rax
mov r13, rsi
mov rsi, rdi
call strLen
mov r14, rax
mov rsi, r13
inc r14
inc r12
CMP r14, r12
JLE .minA
mov rcx, r12
mov rbx, 0
.ciclo:
mov al, byte [rdi+rbx]
CMP al, byte [rsi+rbx]
JG .aEsMayor
JL .bEsMayor
inc rbx
CMP rcx, rbx
JG .ciclo
cmp r14, r12
JG .aEsMayor
JL .bEsMayor
mov RAX, 0
jmp .fin
.aEsMayor:
MOV rax, -1
jmp .fin
.bEsMayor:
mov rax, 1
jmp .fin
.minA:
mov rcx, r14
mov rbx, 0
JMP .ciclo
.fin:
pop rbx
pop r15
pop r14
pop r13
pop r12
add rsp, 8
pop rbp
ret
; char* strClone(char* a)
strClone:
push rbp
mov rbp, rsp
sub rsp, 8
push r12
mov rsi, rdi
mov r12, rdi
call strLen
mov rdi, rax
inc rdi
call malloc
mov rcx, 0
.ciclo:
mov dl, [r12+rcx]
mov [rax+rcx], dl
inc rcx
cmp dl, 0
jne .ciclo
.fin:
pop r12
add rsp, 8
pop rbp
ret
strDelete:
push rbp
mov rbp, rsp
call free
pop rbp
ret
; void strPrint(char* a, FILE* pFile)
strPrint:
PUSH RBP
MOV RBP, RSP
push r12
push r13
mov r12, rdi
mov r13, rsi
call strLen
cmp rax, 0
je .vacia
mov rax, 0
mov rdx, r12
mov rdi, r13
mov rsi, formato_2
call fprintf
jmp .fin
.vacia:
mov rax, 0
mov rdi, r13
mov rsi, formato_10
call fprintf
.fin:
pop r13
pop r12
POP RBP
ret
; uint32_t strLen(char* a)
strLen:
PUSH RBP
MOV RBP , RSP
MOV RCX, 0
.comp:
CMP byte [RDI+RCX], 0
JE .fin
inc RCX
jmp .comp
.fin:
mov rax, rcx
POP RBP
ret
; ** Array **
; array_t* arrayNew(type_t t, uint8_t capacity)
arrayNew:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r15
push r14
mov rbx, rdi
mov r15, rsi
mov rdi, 16
call malloc
mov dword [rax], ebx
mov byte [rax+4], 0
mov byte [rax+5], r15b
lea r15, [r15*8]
mov r14, rax
mov rdi, r15
call malloc
mov [r14+8], rax
mov rax, r14
pop r14
pop r15
pop rbx
add rsp, 8
pop rbp
ret
; uint8_t arrayGetSize(array_t* a)
arrayGetSize:
push rbp
mov rbp, rsp
mov rax, [rdi+4]
pop rbp
ret
; void arrayAddLast(array_t* a, void* data)
arrayAddLast:
push rbp
mov rbp, rsp
push rbx
push r15
mov r9b, byte [rdi+4]
cmp r9b, [rdi+5]
je .fin
mov rbx, rsi
mov r15 ,rdi
mov rdi, 0
mov edi, dword [r15]
call getCloneFunction
mov rdi, rbx
call rax
mov rbx, [r15+8]
mov rcx, 0
.ciclo:
cmp cl, byte [r15+4]
je .addN
inc cl
jmp .ciclo
.addN:
mov [rbx+rcx*8], rax
inc byte [r15+4]
.fin:
pop r15
pop rbx
pop rbp
ret
; void* arrayGet(array_t* a, uint8_t i)
arrayGet:
push rbp
mov rbp, rsp
mov r8, rsi
cmp byte [rdi+4],r8b
jle .retCero
mov r9, [rdi+8]
mov rax, [r9+rsi*8]
jmp .fin
.retCero:
mov rax,0
.fin:
pop rbp
ret
; void* arrayRemove(array_t* a, uint8_t i)
arrayRemove:
push rbp
mov rbp, rsp
sub rsp, 8
push r12
push r13
push r14
push r15
push rbx
mov r12, rdi
mov r13, rsi
call arrayGet
mov r14, rax
mov r15, 0
mov r15b, byte[r12+4]
dec r15b
cmp sil, r15b
jg .outOfRange
cmp r13b, 0
jl .outOfRange
mov rbx, 0
mov rbx, r13
inc r15b
.ciclo:
cmp bl, r15b
JE .delete
mov rdi, r12
mov rsi, r13
lea rdx, [r13+1]
call arraySwap
inc bl
inc r13
jmp .ciclo
.outOfRange:
mov rax, 0
jmp .fin
.delete:
dec byte [r12+4]
mov rax, r14
.fin:
pop rbx
pop r15
pop r14
pop r13
pop r12
add rsp, 8
pop rbp
ret
; void arraySwap(array_t* a, uint8_t i, uint8_t j)
arraySwap:
push rbp
mov rbp, rsp
push r12
push r13
push r14
push r15
mov r10, rsi
cmp r10b, byte [rdi+4]
jge .fin
cmp dl, byte [rdi+4]
jge .fin
cmp dl, 0
jl .fin
cmp r10b, 0
jl .fin
mov r15, rdi
mov r12, rsi
mov r13, rdx
call arrayGet
mov r14, rax
mov rdi, r15
mov rsi, r13
call arrayGet
mov r9, rax
mov r15, [r15+8]
mov [r15+r12*8], r9
mov [r15+r13*8], r14
.fin:
pop r15
pop r14
pop r13
pop r12
pop rbp
ret
; void arrayDelete(array_t* a)
arrayDelete:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
mov r12, rdi
mov rbx, 0
mov rax, [rdi + 4]
mov bl, al
.ciclo:
mov rdi, r12
cmp bl, 0
je .fin
dec rbx
mov rsi, 0
mov rsi, rbx
call arrayRemove
mov r13, rax
mov rdi, [r12]
call getDeleteFunction
mov rdi, r13
call rax
jmp .ciclo
.fin:
mov rdi, r12
mov rsi, rdi
mov rdi, [rdi+8]
call free
mov rdi, rsi
call free
pop r13
pop r12
pop rbx
add rsp, 8
pop rbp
ret
; ** Lista **
; list_t* listNew(type_t t)
listNew:
push rbp
mov rbp, rsp
;type en rdi
sub rsp, 8
push rbx
mov rbx, 0
mov ebx, edi
mov rdi, 24
call malloc
mov dword [rax], ebx
mov dword [rax+4], 0
mov qword [rax+8], 0
mov qword [rax+16], 0
pop RBX
add rsp, 8
pop rbp
ret
; uint8_t listGetSize(list_t* l)
listGetSize:
push rbp
mov rbp, rsp
cmp rdi, 0
je .listaInvalida
mov rax, 0
mov eax, dword [rdi + 4]
jmp .fin
.listaInvalida:
mov rax, 0
.fin:
pop rbp
ret
; void listAddFirst(list_t* l, void* data)
listAddFirst:
push rbp
mov rbp, rsp
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
mov rdi, [rdi]
call getCloneFunction
mov rdi, r13
call rax
mov r15, rax
.s_listElem_create:
mov rdi, 24
call malloc
mov [rax], r15
lea rsi, [r12+8]
lea rdi, [rax+8]
movsq
mov qword [rax+16], 0
.update_prev:
cmp byte [r12+4], 0
jne .noEstabaVacia
mov [r12+16], rax
jmp .actualizarPrimero
.noEstabaVacia:
mov r14, [r12+8]
mov [r14+16], rax
.actualizarPrimero:
mov [r12+8], rax
inc byte [r12+4]
.fin:
pop r15
pop r14
pop r13
pop r12
pop rbp
ret
; void* listGet(list_t* l, uint8_t i)
listGet:
push rbp
mov rbp, rsp
mov r9,0
mov r9d, dword [rdi+4]
cmp rsi, r9
JGE .finInvalido
mov rcx, 0
mov rax, [rdi+8]
.ciclo:
cmp rcx, rsi
JE .finValido
mov rax, [rax+8]
inc rcx
jmp .ciclo
.finInvalido:
mov rax, 0
jmp .fin
.finValido:
mov rax, [rax]
jmp .fin
.fin:
pop rbp
ret
; void* listRemove(list_t* l, uint8_t i)
listRemove:
push rbp
mov rbp, rsp
sub rsp, 8
push r12
push r13
push r14
push r15
push rbx
mov r12, rdi
mov r13, 0
mov r13, rsi
call listGetAux
mov r14, rax
cmp rax, 0
JE .fin
cmp r13b, 0
JE .iEs0
mov r9b, r13b
inc r9b
cmp r9b, byte [r12+4]
je .iEsElUltimo
cmp byte [r12+4], 1
JNE .iEsIntermedio
.iEs0:
cmp byte [r12+4], 1
JE .iEsElUnico
jne .iEsElPrimero
mov r15, qword 0
mov r15b, byte [r12+4]
dec r15b
cmp r15b, r13b
JE .iEsElUltimo
.iEsElUnico:
mov qword [r12+8], 0
mov qword [r12+16], 0
jmp .final
.iEsIntermedio:
dec r13
mov rdi, r12
mov rsi, r13
call listGetAux
mov r15, rax
mov rdi, r12
ADD r13, 2
mov rsi, r13
call listGetAux
mov rbx, rax
mov [r15+8], rbx
mov [rbx+16], r15
jmp .final
.iEsElPrimero:
inc r13
mov rdi, r12
mov rsi, r13
call listGetAux
mov rbx, rax
mov qword [rbx+16], 0
mov [r12+8], rbx
jmp .final
.iEsElUltimo:
dec r13
mov rdi, r12
mov rsi, r13
call listGetAux
mov rbx, rax
mov qword [rbx+8], 0
mov [r12+16], rbx
jmp .final
.final:
dec byte [r12+4]
mov r15, [r14]
mov rdi, r14
call free
mov rax, r15
.fin:
pop rbx
pop r15
pop r14
pop r13
pop r12
add rsp, 8
pop rbp
ret
; void listSwap(list_t* l, uint8_t i, uint8_t j)
listSwap:
push rbp
mov rbp, rsp
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
mov r14, rdx
mov r15, qword 0
mov r15b, byte [rdi+4]
dec r15b
cmp r15b, r13b
jl .fin
cmp r15b, r14b
jl .fin
call listGetAux
mov r13, rax
mov rdi, r12
mov rsi, rdx
call listGetAux
mov r14, rax
mov r9, [r14]
mov r15, [r13]
mov [r14], r15
mov r15, r9
mov [r13], r15
.fin:
pop r15
pop r14
pop r13
pop r12
pop rbp
ret
; list_t* listClone(list_t* l)
listClone:
push rbp
mov rbp, rsp
push r15
push r14
push r13
push r12
mov r13, rdi
mov rdi, 0
mov rdi, [r13]
call listNew
mov r12, rax
mov r15, 0
mov r14, 0
mov r14b, byte [r13+4]
dec r14b
.ciclo:
cmp r15b, r14b
JG .fin
mov rdi, r13
mov rsi, r15
call listGet
mov rdi, r12
mov rsi, rax
call listAddLast
inc r15b
jmp .ciclo
.fin:
mov rax, r12
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
; void listDelete(list_t* l)
listDelete:
push rbp
mov rbp, rsp
sub rsp, 8
push r12
push r13
push r14
push r15
push rbx
mov r12, rdi
mov rdi, [rdi]
call getDeleteFunction
mov r13, rax
mov rdi, r12
call listGetSize
mov r14, [r12+16]
mov rbx, 0
.borrarNodo:
cmp bl, byte [r12+4]
je .fin
cmp r14, 0
je .fin
mov rdi, [r14]
call r13
mov r15, r14
mov r14, [r14+16]
mov rdi, r15
call free
inc bl
jmp .borrarNodo
.fin:
mov rdi, r12
call free
pop rbx
pop r15
pop r14
pop r13
pop r12
add rsp, 8
pop rbp
ret
; ** Card **
; card_t* cardNew(char* suit, int32_t* number)
cardNew:
PUSH RBP
MOV RBP, RSP
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
call strClone
mov r14, rax
mov rdi, r13
call intClone
mov r15, rax
mov rdi, 24
call malloc
mov [rax], r14
mov [rax+8], r15
mov qword [rax+16], 0
pop r15
pop r14
pop r13
pop r12