-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.asm
More file actions
executable file
·1057 lines (886 loc) · 22.4 KB
/
utils.asm
File metadata and controls
executable file
·1057 lines (886 loc) · 22.4 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
; =============================================================================
; Windows x64 Assembly - System Utilities
; =============================================================================
; Author: Dominik Karczewski (ogur.dev)
; Build: nasm -f win64 utils.asm -o utils.obj
; link utils.obj /subsystem:console /entry:main kernel32.lib advapi32.lib ws2_32.lib iphlpapi.lib
; =============================================================================
bits 64
default rel
; Win32 API
extern ExitProcess
extern GetStdHandle
extern WriteConsoleA
extern ReadConsoleA
extern GetComputerNameA
extern GetUserNameA
extern GetCurrentDirectoryA
extern FindFirstFileA
extern FindNextFileA
extern FindClose
extern SetConsoleTitleA
extern GetTickCount64
extern Sleep
extern GetConsoleScreenBufferInfo
extern SetConsoleCursorPosition
extern FillConsoleOutputCharacterA
extern FillConsoleOutputAttribute
; Winsock API
extern WSAStartup
extern WSACleanup
extern socket
extern connect
extern send
extern recv
extern closesocket
extern inet_addr
extern htons
extern gethostbyname
; IPHLPAPI
extern IcmpCreateFile
extern IcmpSendEcho
extern IcmpCloseHandle
; Constants
STD_OUTPUT_HANDLE equ -11
STD_INPUT_HANDLE equ -10
MAX_PATH equ 260
AF_INET equ 2
SOCK_STREAM equ 1
IPPROTO_TCP equ 6
section .data
title db "System Utilities", 0
banner db 0x0A, 0x0D
db "=================================", 0x0A, 0x0D
db " System Utilities - ogur.dev", 0x0A, 0x0D
db "=================================", 0x0A, 0x0D, 0x0A, 0x0D, 0
menu db "Select utility:", 0x0A, 0x0D
db " [1] System Info", 0x0A, 0x0D
db " [2] Directory Tree", 0x0A, 0x0D
db " [3] HTTP Request (curl-like)", 0x0A, 0x0D
db " [4] Ping Host", 0x0A, 0x0D
db " [5] Port Scanner", 0x0A, 0x0D
db " [6] Exit", 0x0A, 0x0D, 0x0A, 0x0D
db "Choice: ", 0
; System info
sys_header db 0x0A, 0x0D, "=== System Info ===", 0x0A, 0x0D, 0
sys_comp db "Computer: ", 0
sys_user db "User: ", 0
sys_dir db "Directory: ", 0
; Directory tree
tree_header db 0x0A, 0x0D, "=== Directory Tree ===", 0x0A, 0x0D, 0
tree_file db " |- ", 0
tree_dir db " [+] ", 0
tree_slash db "/", 0x0A, 0x0D, 0
search_pattern db "*", 0
; HTTP request
http_header db 0x0A, 0x0D, "=== HTTP Request ===", 0x0A, 0x0D, 0
http_prompt db "Enter hostname (e.g., example.com): ", 0
http_connecting db "Connecting to ", 0
http_port db ":80...", 0x0A, 0x0D, 0
http_sent db "Request sent, waiting for response...", 0x0A, 0x0D, 0x0A, 0x0D, 0
http_response_header db "=== Response ===", 0x0A, 0x0D, 0
wsa_error_msg db "WSA initialization failed!", 0x0A, 0x0D, 0
; Ping
ping_header db 0x0A, 0x0D, "=== Ping ===", 0x0A, 0x0D, 0
ping_prompt db "Enter hostname or IP (e.g., 8.8.8.8): ", 0
ping_sending db "Pinging ", 0
ping_dots db "...", 0x0A, 0x0D, 0
ping_reply db "Reply from ", 0
ping_time db ": time=", 0
ping_ms db "ms", 0x0A, 0x0D, 0
ping_timeout db "Request timed out.", 0x0A, 0x0D, 0
ping_resolve_error db "Could not resolve hostname.", 0x0A, 0x0D, 0
; Port scanner
scan_header db 0x0A, 0x0D, "=== Port Scanner ===", 0x0A, 0x0D, 0
scan_ip_prompt db "Enter hostname or IP (e.g., 127.0.0.1): ", 0
scan_start_prompt db "Start port (e.g., 20): ", 0
scan_end_prompt db "End port (e.g., 100): ", 0
scan_scanning db "Scanning ", 0
scan_ports db " ports ", 0
scan_to db "-", 0
scan_ellipsis db "...", 0x0A, 0x0D, 0
scan_open db "Port ", 0
scan_open_suffix db " is OPEN", 0x0A, 0x0D, 0
scan_done db "Scan complete.", 0x0A, 0x0D, 0
; Common
newline db 0x0A, 0x0D, 0
error_socket db "Socket error!", 0x0A, 0x0D, 0
error_connect db "Connection failed!", 0x0A, 0x0D, 0
press_key db 0x0A, 0x0D, "Press Enter to continue...", 0
; Debug strings
debug_resolving db "DEBUG: Resolving: ", 0
debug_got_hostent db "DEBUG: Got hostent", 0x0A, 0x0D, 0
debug_got_addrlist db "DEBUG: Got addr_list", 0x0A, 0x0D, 0
debug_got_firstaddr db "DEBUG: Got first addr", 0x0A, 0x0D, 0
debug_success db "DEBUG: Success, IP = ", 0
debug_err_null db "DEBUG: ERROR - gethostbyname returned NULL", 0x0A, 0x0D, 0
debug_err_addrlist db "DEBUG: ERROR - h_addr_list is NULL", 0x0A, 0x0D, 0
debug_err_firstaddr db "DEBUG: ERROR - first address is NULL", 0x0A, 0x0D, 0
debug_err_ip db "DEBUG: ERROR - IP is zero", 0x0A, 0x0D, 0
section .bss
hStdOut resq 1
hStdIn resq 1
bytesWritten resq 1
bytesRead resq 1
choice resb 10
computerName resb 256
userName resb 256
currentDir resb MAX_PATH
nameSize resd 1
findData resb 592
; Console
consoleInfo resb 22
; Network
wsaData resb 400
sockfd resq 1
sockaddr resb 16
recvBuffer resb 4096
numBuffer resb 20
; User input buffers
input_host resb 256
input_ip resb 64
input_port_start resb 10
input_port_end resb 10
; HTTP request building
http_full_req resb 1024
; Ping
icmpHandle resq 1
pingData resb 32
replyBuffer resb 256
resolved_ip resd 1
section .text
global main
main:
push rbp
mov rbp, rsp
sub rsp, 32
lea rcx, [title]
call SetConsoleTitleA
mov rcx, STD_OUTPUT_HANDLE
call GetStdHandle
mov [hStdOut], rax
mov rcx, STD_INPUT_HANDLE
call GetStdHandle
mov [hStdIn], rax
; Initialize Winsock
mov rcx, 0x0202
lea rdx, [wsaData]
call WSAStartup
; CHECK IF WSA WORKED
test eax, eax
jnz .wsa_failed
.menu_loop:
call clear_screen
lea rdx, [banner]
call print_str
lea rdx, [menu]
call print_str
mov rcx, [hStdIn]
lea rdx, [choice]
mov r8, 10
lea r9, [bytesRead]
call ReadConsoleA
movzx eax, byte [choice]
sub eax, '0'
cmp eax, 1
je .opt_sysinfo
cmp eax, 2
je .opt_tree
cmp eax, 3
je .opt_http
cmp eax, 4
je .opt_ping
cmp eax, 5
je .opt_portscan
cmp eax, 6
je .exit
jmp .menu_loop
.opt_sysinfo:
call show_system_info
call pause_screen
jmp .menu_loop
.opt_tree:
call show_directory_tree
call pause_screen
jmp .menu_loop
.opt_http:
call do_http_request
call pause_screen
jmp .menu_loop
.opt_ping:
call do_ping
call pause_screen
jmp .menu_loop
.opt_portscan:
call do_port_scan
call pause_screen
jmp .menu_loop
.wsa_failed:
lea rdx, [wsa_error_msg]
call print_str
call pause_screen
jmp .exit
.exit:
call WSACleanup
xor rcx, rcx
call ExitProcess
; =============================================================================
; clear_screen - Clear console using Win32 API
; =============================================================================
clear_screen:
push rbp
mov rbp, rsp
sub rsp, 64
; Get console info
mov rcx, [hStdOut]
lea rdx, [consoleInfo]
call GetConsoleScreenBufferInfo
; Calculate console size
movzx eax, word [consoleInfo + 0] ; dwSize.X
movzx ecx, word [consoleInfo + 2] ; dwSize.Y
mul ecx
mov r12d, eax ; Total chars
; Fill with spaces
mov rcx, [hStdOut]
mov edx, 0x20 ; Space character
mov r8d, r12d
xor r9d, r9d ; Top-left (0, 0)
lea rax, [bytesWritten]
mov [rsp+32], rax
call FillConsoleOutputCharacterA
; Reset cursor to 0,0
mov rcx, [hStdOut]
xor edx, edx ; COORD (0, 0)
call SetConsoleCursorPosition
add rsp, 64
pop rbp
ret
; =============================================================================
; show_system_info
; =============================================================================
show_system_info:
push rbp
mov rbp, rsp
sub rsp, 32
lea rdx, [sys_header]
call print_str
; Computer name
mov dword [nameSize], 256
lea rcx, [computerName]
lea rdx, [nameSize]
call GetComputerNameA
lea rdx, [sys_comp]
call print_str
lea rdx, [computerName]
call print_str
lea rdx, [newline]
call print_str
; User name
mov dword [nameSize], 256
lea rcx, [userName]
lea rdx, [nameSize]
call GetUserNameA
lea rdx, [sys_user]
call print_str
lea rdx, [userName]
call print_str
lea rdx, [newline]
call print_str
; Current directory
mov rcx, MAX_PATH
lea rdx, [currentDir]
call GetCurrentDirectoryA
lea rdx, [sys_dir]
call print_str
lea rdx, [currentDir]
call print_str
lea rdx, [newline]
call print_str
add rsp, 32
pop rbp
ret
; =============================================================================
; show_directory_tree
; =============================================================================
show_directory_tree:
push rbp
mov rbp, rsp
sub rsp, 48
lea rdx, [tree_header]
call print_str
lea rcx, [search_pattern]
lea rdx, [findData]
call FindFirstFileA
mov [rsp+40], rax
cmp rax, -1
je .done
.loop:
mov eax, [findData]
test eax, 0x10
jnz .is_dir
lea rdx, [tree_file]
call print_str
lea rdx, [findData + 44]
call print_str
lea rdx, [newline]
call print_str
jmp .next
.is_dir:
lea rsi, [findData + 44]
cmp byte [rsi], '.'
jne .print_dir
cmp byte [rsi + 1], 0
je .next
cmp byte [rsi + 1], '.'
je .next
.print_dir:
lea rdx, [tree_dir]
call print_str
lea rdx, [findData + 44]
call print_str
lea rdx, [tree_slash]
call print_str
.next:
mov rcx, [rsp+40]
lea rdx, [findData]
call FindNextFileA
test eax, eax
jnz .loop
mov rcx, [rsp+40]
call FindClose
.done:
add rsp, 48
pop rbp
ret
; =============================================================================
; do_http_request
; =============================================================================
do_http_request:
push rbp
mov rbp, rsp
sub rsp, 64
lea rdx, [http_header]
call print_str
; Prompt for hostname
lea rdx, [http_prompt]
call print_str
mov rcx, [hStdIn]
lea rdx, [input_host]
mov r8, 256
lea r9, [bytesRead]
call ReadConsoleA
; Strip \r\n
lea rdi, [input_host]
call strip_newline
; Build HTTP request: "GET / HTTP/1.1\r\nHost: HOSTNAME\r\nUser-Agent: ASM-Utils/1.0\r\nConnection: close\r\n\r\n"
lea rdi, [http_full_req]
; GET line
mov rsi, get_line
call copy_string
; Host header
mov rsi, host_header
call copy_string
lea rsi, [input_host]
call copy_string
mov rsi, crlf
call copy_string
; User-Agent
mov rsi, ua_header
call copy_string
; Connection
mov rsi, conn_header
call copy_string
; Final CRLF
mov rsi, crlf
call copy_string
mov byte [rdi], 0
; Calculate length
lea rax, [http_full_req]
sub rdi, rax
mov r15, rdi
; Print connecting
lea rdx, [http_connecting]
call print_str
lea rdx, [input_host]
call print_str
lea rdx, [http_port]
call print_str
; Create socket
mov rcx, AF_INET
mov rdx, SOCK_STREAM
mov r8, IPPROTO_TCP
call socket
mov [sockfd], rax
cmp rax, -1
je .error
; Setup sockaddr
mov word [sockaddr], AF_INET
mov rcx, 80
call htons
mov word [sockaddr + 2], ax
; Resolve hostname
lea rcx, [input_host]
call gethostbyname
test rax, rax
jz .error
mov rax, [rax + 24]
mov rax, [rax]
mov eax, [rax]
mov dword [sockaddr + 4], eax
; Connect
mov rcx, [sockfd]
lea rdx, [sockaddr]
mov r8, 16
call connect
test eax, eax
jnz .error_connect
; Send
lea rdx, [http_sent]
call print_str
mov rcx, [sockfd]
lea rdx, [http_full_req]
mov r8, r15
xor r9, r9
call send
; Receive
lea rdx, [http_response_header]
call print_str
mov rcx, [sockfd]
lea rdx, [recvBuffer]
mov r8, 4096
xor r9, r9
call recv
test eax, eax
jle .close
mov rcx, [hStdOut]
lea rdx, [recvBuffer]
mov r8, 800
lea r9, [bytesWritten]
call WriteConsoleA
lea rdx, [newline]
call print_str
.close:
mov rcx, [sockfd]
call closesocket
jmp .done
.error_connect:
lea rdx, [error_connect]
call print_str
mov rcx, [sockfd]
call closesocket
jmp .done
.error:
lea rdx, [error_socket]
call print_str
.done:
add rsp, 64
pop rbp
ret
; =============================================================================
; do_ping
; =============================================================================
do_ping:
push rbp
mov rbp, rsp
sub rsp, 64
lea rdx, [ping_header]
call print_str
lea rdx, [ping_prompt]
call print_str
mov rcx, [hStdIn]
lea rdx, [input_ip]
mov r8, 64
lea r9, [bytesRead]
call ReadConsoleA
lea rdi, [input_ip]
call strip_newline
; Resolve hostname/IP
lea rcx, [input_ip]
call resolve_host
test eax, eax
jz .resolve_error
mov [resolved_ip], eax
; Print status
lea rdx, [ping_sending]
call print_str
lea rdx, [input_ip]
call print_str
lea rdx, [ping_dots]
call print_str
; Create ICMP handle
call IcmpCreateFile
mov [icmpHandle], rax
test rax, rax
jz .done
; Prepare data
mov rcx, 32
lea rdi, [pingData]
mov al, 0x45
rep stosb
; Get start time
call GetTickCount64
mov r11, rax
; Send ping
mov rcx, [icmpHandle]
mov edx, [resolved_ip]
lea r8, [pingData]
mov r9, 32
push 0
push 0
push 256
lea rax, [replyBuffer]
push rax
push 1000
sub rsp, 32
call IcmpSendEcho
add rsp, 72
test eax, eax
jz .timeout
; Calculate RTT
call GetTickCount64
sub rax, r11
lea rdx, [ping_reply]
call print_str
lea rdx, [input_ip]
call print_str
lea rdx, [ping_time]
call print_str
call print_number
lea rdx, [ping_ms]
call print_str
jmp .close
.timeout:
lea rdx, [ping_timeout]
call print_str
jmp .close
.resolve_error:
lea rdx, [ping_resolve_error]
call print_str
jmp .done
.close:
mov rcx, [icmpHandle]
call IcmpCloseHandle
.done:
add rsp, 64
pop rbp
ret
; =============================================================================
; do_port_scan
; =============================================================================
do_port_scan:
push rbp
mov rbp, rsp
sub rsp, 64
lea rdx, [scan_header]
call print_str
; Get IP
lea rdx, [scan_ip_prompt]
call print_str
mov rcx, [hStdIn]
lea rdx, [input_ip]
mov r8, 64
lea r9, [bytesRead]
call ReadConsoleA
lea rdi, [input_ip]
call strip_newline
; Resolve
lea rcx, [input_ip]
call resolve_host
test eax, eax
jz .error
mov [resolved_ip], eax
; Get start port
lea rdx, [scan_start_prompt]
call print_str
mov rcx, [hStdIn]
lea rdx, [input_port_start]
mov r8, 10
lea r9, [bytesRead]
call ReadConsoleA
lea rdx, [input_port_start]
call parse_number
mov r12, rax
; Get end port
lea rdx, [scan_end_prompt]
call print_str
mov rcx, [hStdIn]
lea rdx, [input_port_end]
mov r8, 10
lea r9, [bytesRead]
call ReadConsoleA
lea rdx, [input_port_end]
call parse_number
mov r13, rax
; Print scanning
lea rdx, [scan_scanning]
call print_str
lea rdx, [input_ip]
call print_str
lea rdx, [scan_ports]
call print_str
mov rax, r12
call print_number
lea rdx, [scan_to]
call print_str
mov rax, r13
call print_number
lea rdx, [scan_ellipsis]
call print_str
.scan_loop:
cmp r12, r13
jg .done
; Create socket
mov rcx, AF_INET
mov rdx, SOCK_STREAM
mov r8, IPPROTO_TCP
call socket
mov [sockfd], rax
cmp rax, -1
je .next_port
; Setup sockaddr
mov word [sockaddr], AF_INET
mov rcx, r12
call htons
mov word [sockaddr + 2], ax
mov eax, [resolved_ip]
mov dword [sockaddr + 4], eax
; Try connect
mov rcx, [sockfd]
lea rdx, [sockaddr]
mov r8, 16
call connect
test eax, eax
jnz .close_sock
; Port open
lea rdx, [scan_open]
call print_str
mov rax, r12
call print_number
lea rdx, [scan_open_suffix]
call print_str
.close_sock:
mov rcx, [sockfd]
call closesocket
.next_port:
inc r12
jmp .scan_loop
.error:
lea rdx, [ping_resolve_error]
call print_str
jmp .done
.done:
lea rdx, [scan_done]
call print_str
add rsp, 64
pop rbp
ret
; =============================================================================
; resolve_host - Using gethostbyname
; Input: rcx = hostname/IP string
; Output: eax = IP address (network byte order), 0 on error
; =============================================================================
resolve_host:
push rbp
mov rbp, rsp
sub rsp, 32
call gethostbyname
test rax, rax
jz .error
mov rdx, [rax + 24]
test rdx, rdx
jz .error
mov rdx, [rdx]
test rdx, rdx
jz .error
mov eax, [rdx]
jmp .done
.error:
xor eax, eax
.done:
add rsp, 32
pop rbp
ret
; =============================================================================
; print_hex - Print 32-bit value in hex
; Input: rdx = value
; =============================================================================
print_hex:
push rbp
mov rbp, rsp
sub rsp, 32
push rbx
push r12
mov r12, rdx
lea rdi, [numBuffer]
mov byte [rdi], '0'
inc rdi
mov byte [rdi], 'x'
inc rdi
mov ecx, 8
.loop:
mov eax, r12d
shr eax, 28
and eax, 0xF
cmp al, 10
jb .digit
add al, 'A' - 10
jmp .store
.digit:
add al, '0'
.store:
mov [rdi], al
inc rdi
shl r12d, 4
dec ecx
jnz .loop
mov byte [rdi], 0
lea rdx, [numBuffer]
call print_str
pop r12
pop rbx
add rsp, 32
pop rbp
ret
; =============================================================================
; copy_string - Copy null-terminated string
; Input: rsi = source, rdi = dest (updated)
; =============================================================================
copy_string:
push rax
.loop:
lodsb
test al, al
jz .done
stosb
jmp .loop
.done:
pop rax
ret
; =============================================================================
; strip_newline
; =============================================================================
strip_newline:
push rax
.loop:
mov al, [rdi]
test al, al
jz .done
cmp al, 0x0D
je .null_it
cmp al, 0x0A
je .null_it
inc rdi
jmp .loop
.null_it:
mov byte [rdi], 0
.done:
pop rax
ret
; =============================================================================
; parse_number
; =============================================================================
parse_number:
push rbx
push rcx
xor rax, rax
xor rbx, rbx
mov rcx, 10
.loop:
movzx ebx, byte [rdx]
cmp bl, '0'
jb .done
cmp bl, '9'
ja .done
sub bl, '0'
mul rcx
add rax, rbx
inc rdx
jmp .loop
.done:
pop rcx
pop rbx
ret
; =============================================================================
; print_str
; =============================================================================
print_str:
push rbp
mov rbp, rsp
sub rsp, 32
push rsi
push rdi
mov rdi, rdx
mov rsi, rdx
xor rcx, rcx
.len_loop:
lodsb
test al, al
jz .print
inc rcx
jmp .len_loop
.print:
push rcx
mov rcx, [hStdOut]
mov rdx, rdi
pop r8
lea r9, [bytesWritten]
call WriteConsoleA
pop rdi
pop rsi
add rsp, 32
pop rbp
ret
; =============================================================================
; print_number
; =============================================================================
print_number:
push rbp
mov rbp, rsp
sub rsp, 32
push rbx