-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.inc
More file actions
126 lines (99 loc) · 4.06 KB
/
utils.inc
File metadata and controls
126 lines (99 loc) · 4.06 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
; =====================================================================
; wsa_inet_addr
; Minimal IPv4 ASCII-to-binary converter.
; Input : RSI = pointer to zero-terminated ASCII IPv4 string ("127.0.0.1")
; Output: RAX = IPv4 address in network byte order (DWORD)
; =====================================================================
proc wsa_inet_addr
push rbp
mov rbp, rsp
xor rax, rax ; working accumulator / return value
xor rdi, rdi ; final assembled IPv4 result = 0
mov rcx, 4 ; 4 octets to process
; ---------------------------------------------------------------------
; Process each octet
; ---------------------------------------------------------------------
convert_octet:
xor rdx, rdx ; clear numeric accumulator for this octet
mov rbx, 10 ; base = decimal
; ---------------------------------------------------------------------
; Convert decimal digits of current octet
; ---------------------------------------------------------------------
convert_digit:
movzx eax, byte [rsi] ; read next character
cmp al, '0'
jl end_octet ; not a digit ? end of octet
cmp al, '9'
jg end_octet ; not a digit ? end of octet
imul rdx, rbx ; value = value * 10
sub al, '0' ; convert ASCII ? integer
add rdx, rax ; value += digit
mov rax, rdx ; update working register
mov dil, al ; save last digit (unused but preserved)
inc rsi ; advance cursor
jmp convert_digit
; ---------------------------------------------------------------------
; End of current octet
; ---------------------------------------------------------------------
end_octet:
cmp byte [rsi], '.' ; check dot separator
jne end_function ; invalid format ? terminate early
shl rdi, 8 ; shift previous octets left
inc rsi ; move past '.'
loop convert_octet ; process next octet
; ---------------------------------------------------------------------
; Final output assembly
; ---------------------------------------------------------------------
end_function:
mov rax, rdi ; return assembled IPv4
bswap eax ; convert to network byte order (big endian)
leave
ret
endp
; =====================================================================
; strcmp (ANSI)
; Basic byte-wise string comparison.
; Input : RSI = string1, RDI = string2
; Output: RAX = 0 if equal, non-zero if different
; =====================================================================
strcmp:
push rsi
push rdi
.str_loop:
movzx rax, byte [rsi]
movzx rcx, byte [rdi]
cmp al, cl
jne .different
test al, al ; reached string terminator?
jz .equal
inc rsi
inc rdi
jmp .str_loop
.equal:
xor rax, rax ; equal ? return 0
jmp .done
.different:
sub rax, rcx ; difference in byte values
.done:
pop rdi
pop rsi
ret
; =====================================================================
; strlen_unicode
; Computes the length of a UTF-16 string in bytes.
; Input : RCX = pointer to UTF-16 sequence
; Output: RAX = length in bytes
; =====================================================================
strlen_unicode:
xor rax, rax ; character count = 0
xor rdx, rdx ; offset = 0
.strlen_loop:
movzx r8, word [rcx + rdx] ; load UTF-16 element
test r8, r8
jz .strlen_done ; found null terminator ? stop
add rdx, 2 ; move to next wchar
inc rax ; count one wchar
jmp .strlen_loop
.strlen_done:
shl rax, 1 ; convert wchar count ? byte count
ret