-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTLL_Libs_x86.txt
More file actions
1106 lines (952 loc) · 26.5 KB
/
HTLL_Libs_x86.txt
File metadata and controls
1106 lines (952 loc) · 26.5 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
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
$$$$is_nint_negative$$$$
; =============================================================================
; UTILITY FUNCTIONS (Correct and Properly Formatted)
; =============================================================================
; is_nint_negative: Reads a signed number, updates its corresponding sign
; flag, AND converts the original number to its positive
; magnitude if it was negative.
;
; [In]
; rdi - Pointer to the nint variable (which holds a signed value).
; rsi - Pointer to the is_negative flag variable for that nint.
;
; [Out]
; The memory at [rsi] will be overwritten with 0 or 1.
; The memory at [rdi] will be overwritten with its positive magnitude.
is_nint_negative:
push rbp
mov rbp, rsp
push rax
; Load the actual signed value from the first pointer
mov rax, [rdi]
; Check its sign using the CPU's Sign Flag
test rax, rax
jns .is_positive_or_zero
.is_negative:
; The number is negative.
; 1. Update the flag variable to 1.
mov qword [rsi], 1
; 2. Negate the value to get its positive magnitude.
neg rax
; 3. Store the positive magnitude BACK into the original variable.
mov [rdi], rax
jmp .done
.is_positive_or_zero:
; The number is positive or zero.
; 1. Update the flag variable to 0.
mov qword [rsi], 0
; 2. The value at [rdi] is already its own positive magnitude, so we do nothing to it.
.done:
pop rax
pop rbp
ret
%%%%is_nint_negative%%%%
$$$$divide_and_store$$$$
; divide_and_store: Performs fixed-point division and stores results in memory.
; [In] rdi: dividend_int, rsi: dividend_dec
; rdx: divisor_int, rcx: divisor_dec
; r8: ptr to result_int, r9: ptr to result_dec
divide_and_store:
; Prologue
push rbx
push r12
push r13
push r14
push r15
; --- THE FIX: Save all volatile input arguments immediately ---
mov r12, rdi
mov r13, rsi
mov r14, rdx
mov r15, rcx
; --- Step 1: Scale the Dividend ---
mov rax, r12
mov rbx, [SCALE_FACTOR]
mul rbx
add rax, r13
mov r12, rax
; --- Step 2: Scale the Divisor ---
mov rax, r14
mov rbx, [SCALE_FACTOR]
mul rbx
add rax, r15
mov r13, rax
; --- Step 3: Perform the main 128-bit division ---
mov rax, r12
mov rbx, [SCALE_FACTOR]
mul rbx
div r13
; --- Step 4: De-scale the final result ---
mov rbx, [SCALE_FACTOR]
xor rdx, rdx
div rbx
; --- Step 5: Store the results using the pointers from r8 and r9 ---
mov [r8], rax
mov [r9], rdx
; Epilogue
pop r15
pop r14
pop r13
pop r12
pop rbx
ret
%%%%divide_and_store%%%%
$$$$print_div$$$$
; print_div: (Unchanged logic, but now properly formatted)
; [In] rdi: integer part, rsi: decimal part
print_div:
; Prologue
push rbp
mov rbp, rsp
push rbx
push r12
push rcx
push rsi
; --- Print integer part ---
mov rax, rdi
lea r12, [print_buffer + 19]
mov byte [r12], 0
dec r12
mov rbx, 10
.int_convert_loop:
xor rdx, rdx
div rbx
add rdx, '0'
mov [r12], dl
dec r12
cmp rax, 0
jne .int_convert_loop
inc r12
mov rsi, r12
lea rdx, [print_buffer + 19]
sub rdx, rsi
mov rax, 1
mov rdi, 1
syscall
; --- Print decimal point ---
mov rax, 1
mov rdi, 1
mov rsi, dot
mov rdx, 1
syscall
; --- Print decimal part ---
pop rax
lea r12, [print_buffer + 5]
mov rbx, 10
mov rcx, 6
.dec_convert_loop:
xor rdx, rdx
div rbx
add rdx, '0'
mov [r12], dl
dec r12
loop .dec_convert_loop
mov rax, 1
mov rdi, 1
lea rsi, [print_buffer]
mov rdx, 6
syscall
; Print a final newline.
mov rax, 1
mov rdi, 1
mov rsi, nl
mov rdx, 1
syscall
; Epilogue
pop rcx
pop r12
pop rbx
pop rbp
ret
%%%%print_div%%%%
$$$$print_number$$$$
; print_number: Prints a number, prepending a '-' sign if requested.
;
; [In]
; rdi - The number to print (as an unsigned, positive magnitude).
; rsi - The "is_negative" flag. 0 for positive, 1 for negative.
print_number:
push rbp
mov rbp, rsp
push rbx
push r12
push rdi
; Check the is_negative flag passed in rsi
cmp rsi, 0
je .skip_sign_print
; Flag is 1, so print a minus sign.
; This is safe because the number is preserved on the stack.
mov rax, 1
mov rdi, 1
mov rsi, minus_sign
mov rdx, 1
syscall
.skip_sign_print:
; Restore the saved number from the stack directly into RAX,
; which is the register we need for the division loop.
pop rax
; The conversion loop, which now operates on the correct number in rax.
lea r12, [print_buffer + 19]
mov byte [r12], 0
dec r12
mov rbx, 10
.conversion_loop:
xor rdx, rdx
div rbx
add rdx, '0'
mov [r12], dl
dec r12
cmp rax, 0
jne .conversion_loop
inc r12
mov rsi, r12
lea rdx, [print_buffer + 20]
sub rdx, rsi
mov rax, 1
mov rdi, 1
syscall
; Print a final newline.
mov rax, 1
mov rdi, 1
mov rsi, nl
mov rdx, 1
syscall
; The stack is balanced because we pushed RDI and popped it into RAX.
pop r12
pop rbx
pop rbp
ret
%%%%print_number%%%%
$$$$print_str$$$$
print_str:
push rbp
mov rbp, rsp
mov rax, 1
mov rdi, 1
; rsi and rdx are expected to be set by the caller
syscall
pop rbp
ret
%%%%print_str%%%%
$$$$input$$$$
; input: Reads a line from stdin, handles empty input, removes the trailing
; newline, and stores the raw string and its length.
;
; [In]
; rdi - Pointer to the buffer where the string should be stored.
; rsi - Pointer to a qword where the length of the clean string will be stored.
input:
push rbp
mov rbp, rsp
push r12
push r13
; Save the pointers passed as arguments immediately
mov r12, rdi
mov r13, rsi
; Correctly set up all registers for the sys_read syscall
mov rax, 0
mov rdi, 0
mov rsi, r12
mov rdx, 100
syscall
; The program waits here. rax will return with bytes read.
; Check if the user entered anything. If they just press Enter, rax will be 1.
; If they press Ctrl+D, rax will be 0. Both cases mean empty input.
cmp rax, 1
jle .read_empty
; This code only runs for valid input (rax > 1).
; The length of the clean string is (bytes_read - 1).
dec rax
jmp .store_length
.read_empty:
; === THE FIX IS HERE ===
; If the input was empty, explicitly set the length to 0.
xor rax, rax
.store_length:
; Store the final calculated length (either from dec rax or xor rax)
; into the memory location pointed to by r13.
mov [r13], rax
pop r13
pop r12
pop rbp
ret
%%%%input%%%%
$$$$array_copy$$$$
; =============================================================================
; DYNAMIC ARRAY LIBRARY FUNCTIONS (Final, Corrected Version)
; =============================================================================
; array_clear: Clears a dynamic array by setting its size to 0.
; =============================================================================
; array_copy: Copies all elements from a source array to a destination array.
; The destination array will be resized if necessary. Any existing data in
; the destination array will be overwritten.
; [In]
; rdi - Pointer to the destination DynamicArray struct.
; rsi - Pointer to the source DynamicArray struct.
; =============================================================================
; =============================================================================
; TRULY BULLETPROOF array_copy
; =============================================================================
array_copy:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
; --- Step 1: Free the destination's old memory block ---
mov rdi, [r12 + DynamicArray.pointer]
cmp rdi, 0
je .copy_skip_munmap_fix
mov rsi, [r12 + DynamicArray.capacity]
imul rsi, 8
mov rax, 11
syscall
.copy_skip_munmap_fix:
; --- Step 2: Set new size and capacity ---
mov rbx, [r13 + DynamicArray.size]
mov [r12 + DynamicArray.size], rbx
mov [r12 + DynamicArray.capacity], rbx
; --- Step 3: Allocate new memory ---
cmp rbx, 0
je .copy_epilogue_fix
mov r14, rbx
imul r14, 8
; ======================= THE CRITICAL FIX IS HERE =========================
; Syscalls clobber registers. We need r12 and r13 after. Save them.
push r12
push r13
mov rax, 9
mov rdi, 0
mov rsi, r14
mov rdx, 3
mov r10, 34
mov r8, -1
mov r9, 0
syscall
; Restore our precious struct pointers immediately after the syscall.
pop r13
pop r12
; ========================================================================
cmp rax, 0
jl .copy_epilogue_fix
mov [r12 + DynamicArray.pointer], rax
; --- Step 4: Copy the data ---
mov rsi, [r13 + DynamicArray.pointer]
mov rdi, [r12 + DynamicArray.pointer]
mov rcx, [r13 + DynamicArray.size]
rep movsq
.copy_epilogue_fix:
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
%%%%array_copy%%%%
$$$$array_clear$$$$
; [In] rdi: Pointer to the DynamicArray struct.
array_clear:
push rbp
mov rbp, rsp
; Set the .size field of the struct pointed to by rdi to 0.
mov qword [rdi + DynamicArray.size], 0
pop rbp
ret
%%%%array_clear%%%%
$$$$array_append$$$$
; =============================================================================
; array_append: (Final, Robust Version with Error Checking)
; Appends a qword value to a dynamic array, resizing if necessary.
; =============================================================================
array_append:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
mov rcx, [r12 + DynamicArray.size]
cmp rcx, [r12 + DynamicArray.capacity]
jl .has_space
; --- Resize Logic ---
.resize:
mov r14, [r12 + DynamicArray.pointer]
mov r15, [r12 + DynamicArray.capacity]
mov rbx, r15
cmp rbx, 0
jne .double_it
mov rbx, INITIAL_CAPACITY
jmp .capacity_calculated
.double_it:
shl rbx, 1
.capacity_calculated:
mov [r12 + DynamicArray.capacity], rbx
; --- Allocate new memory ---
push r12
push r13
push r14
push r15
imul rbx, 8
mov rax, 9
mov rdi, 0
mov rsi, rbx
mov rdx, 3
mov r10, 34
mov r8, -1
mov r9, 0
syscall
pop r15
pop r14
pop r13
pop r12
; ========================= THE CRITICAL FIX =========================
; Syscalls return a negative value on error. We must check for this.
; A valid pointer will be a small positive number. An error is a large
; number close to -1 (0xFFFFFFFFFFFFFFFF).
cmp rax, 0
jl .mmap_failed
; ====================================================================
; --- Copy old data to new block ---
cmp r14, 0
je .skip_copy
mov rdi, rax
mov rsi, r14
mov rcx, [r12 + DynamicArray.size]
rep movsq
; --- Free the OLD memory block ---
push rax
push r12
push r13
imul r15, 8
mov rdi, r14
mov rsi, r15
mov rax, 11
syscall
pop r13
pop r12
pop rax
.skip_copy:
mov [r12 + DynamicArray.pointer], rax
.has_space:
mov rbx, [r12 + DynamicArray.pointer]
mov rcx, [r12 + DynamicArray.size]
mov [rbx + rcx * 8], r13
inc qword [r12 + DynamicArray.size]
jmp .append_epilogue
.mmap_failed:
; If mmap fails, we can't continue. Exit with an error code (e.g., 1).
mov rax, 60
mov rdi, 1
syscall
.append_epilogue:
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
%%%%array_append%%%%
$$$$array_pop$$$$
array_pop:
push rbp
mov rbp, rsp
push rbx
push rcx
push r8
mov r8, rdi
mov rcx, [r8 + DynamicArray.size]
cmp rcx, 0
je .pop_empty
dec rcx
mov [r8 + DynamicArray.size], rcx
mov rbx, [r8 + DynamicArray.pointer]
mov rax, [rbx + rcx * 8]
jmp .pop_done
.pop_empty:
xor rax, rax
.pop_done:
pop r8
pop rcx
pop rbx
pop rbp
ret
%%%%array_pop%%%%
$$$$print_char$$$$
print_char:
push rbp
mov rbp, rsp
; Push the entire RDI register. This subtracts 8 bytes from RSP,
; which keeps the stack correctly aligned. The character is in DIL,
; the lowest byte of RDI, so this works perfectly.
push rdi
mov rax, 1
mov rdi, 1
mov rsi, rsp
mov rdx, 1
syscall
; Clean up the stack by popping the value we pushed.
; This adds 8 back to RSP, restoring it.
pop rdi
pop rbp
ret
%%%%print_char%%%%
$$$$sys_read_line$$$$
; =============================================================================
; sys_read_line: A low-level wrapper around the sys_read syscall for stdin.
; [In]
; rdi - Pointer to the buffer where the string should be stored.
; rsi - Max number of bytes to read.
; [Out]
; rax - The number of bytes actually read (including newline).
; =============================================================================
sys_read_line:
push rbp
mov rbp, rsp
; Prepare arguments for the sys_read syscall
mov rax, 0
mov rdx, rsi
mov rsi, rdi
mov rdi, 0
syscall
pop rbp
ret
%%%%sys_read_line%%%%
$$$$get_user_input$$$$
; =============================================================================
; get_user_input: (Final, Clean, Production Version)
; Correctly prompts, reads, and appends user input.
; =============================================================================
get_user_input:
; --- Prologue: Save all callee-saved registers we will use. ---
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
; Correctly align the entire stack frame for all subsequent calls.
sub rsp, 8
; Save arguments immediately into safe callee-saved registers.
mov r12, rdi
mov r13, rsi
; --- Part 1: Print the prompt string ---
mov rcx, [r13 + DynamicArray.size]
cmp rcx, 0
je .read_from_user
mov r14, rcx
add rcx, 15
and rcx, -16
mov rbx, rcx
sub rsp, rbx
; Pack the prompt into the stack buffer.
mov rdi, rsp
mov rsi, [r13 + DynamicArray.pointer]
push rbx
xor rbx, rbx
.pack_loop:
cmp rbx, r14
jge .do_prompt_print
mov al, [rsi + rbx*8]
mov [rdi + rbx], al
inc rbx
jmp .pack_loop
.do_prompt_print:
pop rbx
mov rax, 1
mov rdi, 1
mov rsi, rsp
mov rdx, r14
syscall
add rsp, rbx
; --- Part 2: Read user's input ---
.read_from_user:
mov rax, 0
mov rdi, 0
mov rsi, input_buffer
mov rdx, 255
syscall
; --- Part 3: Append the input to the dynamic array ---
cmp rax, 1
jle .done
dec rax
mov r14, rax
mov r15, input_buffer
.append_char_loop:
cmp r14, 0
je .done
mov rdi, r12
movzx rsi, byte [r15]
call array_append
inc r15
dec r14
jmp .append_char_loop
.done:
; --- Epilogue: Restore the stack and all saved registers. ---
add rsp, 8
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
%%%%get_user_input%%%%
$$$$fileread_from_ptr$$$$
; =============================================================================
; =============================================================================
; HTLL DYNAMIC FILE I/O LIBRARY - POINTER-BASED VERSIONS
; These functions are the key to building a truly dynamic compiler.
; =============================================================================
; =============================================================================
; =============================================================================
; file_read_from_ptr: Appends a file's contents to a dynamic array.
; [In]
; rdi - Pointer to the destination DynamicArray struct.
; rsi - A POINTER to a null-terminated string containing the file path.
; =============================================================================
fileread_from_ptr:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
; --- Open the file ---
mov rax, 2
mov rdi, r13
xor rsi, rsi
syscall
; ======================= THE ARMOR PLATE =========================
; A valid file descriptor is a small positive number. Errors are negative.
cmp rax, 0
jl .read_error_ptr
; ===================================================================
mov r14, rax
.read_loop_ptr:
mov rax, 0
mov rdi, r14
mov rsi, file_read_buffer
mov rdx, 4096
syscall
cmp rax, 0
jle .close_and_exit_ptr
mov r15, rax
mov rsi, file_read_buffer
.append_byte_loop_ptr:
cmp r15, 0
je .read_loop_ptr
movzx rbx, byte [rsi]
push rsi
mov rdi, r12
mov rsi, rbx
call array_append
pop rsi
inc rsi
dec r15
jmp .append_byte_loop_ptr
.close_and_exit_ptr:
mov rax, 3
mov rdi, r14
syscall
jmp .read_done_ptr
.read_error_ptr:
; The open failed. We do nothing. The destination array remains empty.
; A real-world version would print an error to stderr.
.read_done_ptr:
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
%%%%fileread_from_ptr%%%%
$$$$fileappend_from_ptr$$$$
; =============================================================================
; file_append_from_ptr: Appends an array of character codes to a file.
; [In]
; rdi - A POINTER to a null-terminated string containing the file path.
; rsi - Pointer to the source DynamicArray struct.
; =============================================================================
fileappend_from_ptr:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
mov rcx, [r13 + DynamicArray.size]
cmp rcx, 0
je .append_epilogue_ptr
; --- Pack the array into a byte buffer on the stack ---
mov r14, rcx
mov rbx, rcx
add rbx, 15
and rbx, -16
sub rsp, rbx
mov r15, rsp
mov rsi, [r13 + DynamicArray.pointer]
xor rcx, rcx
.pack_loop_ptr:
cmp rcx, r14
jge .pack_done_ptr
mov al, [rsi + rcx*8]
mov [r15 + rcx], al
inc rcx
jmp .pack_loop_ptr
.pack_done_ptr:
; --- Open the file ---
mov rax, 2
mov rdi, r12
mov rsi, 1089
mov rdx, 420
syscall
; ======================= THE ARMOR PLATE =========================
cmp rax, 0
jl .append_error_ptr
; ===================================================================
mov r14, rax
; --- Write the packed data ---
mov rax, 1
mov rdi, r14
mov rsi, r15
mov rdx, [r13 + DynamicArray.size]
syscall
; --- Close the file ---
mov rax, 3
mov rdi, r14
syscall
.append_error_ptr:
; This label is jumped to if open fails. We just clean up the stack.
.append_cleanup_ptr:
add rsp, rbx
.append_epilogue_ptr:
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
%%%%fileappend_from_ptr%%%%
$$$$filedelete_from_ptr$$$$
; =============================================================================
; file_delete_from_ptr: Deletes a file from the filesystem.
; [In]
; rdi - A POINTER to a null-terminated string containing the file path.
; =============================================================================
filedelete_from_ptr:
push rbp
mov rbp, rsp
mov rax, 87
syscall
; For robustness, you could check if rax is 0 (success) or -1 (failure)
pop rbp
ret
%%%%filedelete_from_ptr%%%%
$$$$file_read$$$$
; =============================================================================
; =============================================================================
; ORIGINAL STATIC (LABEL-BASED) FUNCTIONS
; Kept for compatibility and for handling hardcoded file paths.
; =============================================================================
; =============================================================================
; =============================================================================
; file_read: Appends a file's contents to a dynamic array.
; =============================================================================
file_read:
; This function's logic is already pointer-based, so it works as is.
; We can simply call the new, more clearly named function.
jmp fileread_from_ptr
%%%%file_read%%%%
$$$$file_append$$$$
; =============================================================================
; file_append: Appends an array of character codes to a file.
; =============================================================================
file_append:
; This function's logic is also already pointer-based.
jmp fileappend_from_ptr
%%%%file_append%%%%
$$$$file_delete$$$$
; =============================================================================
; file_delete: Deletes a file from the filesystem.
; =============================================================================
file_delete:
; This function's logic is also already pointer-based.
jmp filedelete_from_ptr
%%%%file_delete%%%%
$$$$array_pack_to_bytes$$$$
; =============================================================================
; ADAPTER FUNCTION 1: HTLL arr -> C-style string (char*)
; =============================================================================
; Takes an HTLL array and creates a C-style string from it.
; [In] rdi - Pointer to the source DynamicArray struct.
; [Out] rax - Pointer to the new C-style string (or 0 on error).
;==============================================================================
; =============================================================================
; ADAPTER FUNCTION 1: HTLL arr -> C-style string (char*) (Robust Version)
; =============================================================================
array_pack_to_bytes:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
; --- ROBUSTNESS CHECK 1 ---
; Check if the struct pointer itself is null
cmp rdi, 0
je .pack_fail
mov r14, [rdi + DynamicArray.pointer]
mov r12, [rdi + DynamicArray.size]
; --- ROBUSTNESS CHECK 2 ---
; Check if the array's internal data pointer is null or if size is zero
cmp r14, 0
je .pack_fail
cmp r12, 0
je .pack_fail
; === Step A: Ask the kernel for memory ===
mov r13, r12
inc r13
mov rax, 9
mov rdi, 0
mov rsi, r13
mov rdx, 3
mov r10, 34
mov r8, -1
mov r9, 0
syscall
cmp rax, 0
jl .pack_fail
; === Step B: Copy the bytes, one by one ===
mov r13, rax
xor rbx, rbx
.pack_loop:
cmp rbx, r12
jge .pack_add_null
mov rcx, [r14 + rbx*8]
mov [r13 + rbx], cl
inc rbx
jmp .pack_loop
.pack_add_null:
mov byte [r13 + rbx], 0
mov rax, r13
mov rdx, r12
inc rdx
jmp .pack_done
.pack_fail:
xor rax, rax
.pack_done:
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret