-
Notifications
You must be signed in to change notification settings - Fork 0
/
strpbrk.s
43 lines (38 loc) · 1.03 KB
/
strpbrk.s
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
;; char *strpbrk(const char *s, const char *accept);
;; locates the first occurrence in the string s of any of the bytes in the string accept.
;; returns a pointer to the byte in s that matches one of the bytes in accept, or NULL if no such byte is found.
section .text
global strpbrk:function
strpbrk:
push rbp
mov rbp, rsp
push rdi ; save s because we will modify it
mov rax, 0 ; rax = NULL
cmp byte [rsi], 0 ; if (*accept == 0)
je done ; return rax;
while: ; while (
cmp byte [rdi], 0 ; *s != 0
je done ; )
cwhile: ; {
mov rdx, rsi ; rdx = accept
_while: ; while (
cmp byte [rdx], 0 ; *rdx
je _done ; )
_cwhile: ; {
mov cl, byte [rdx]
cmp byte [rdi], cl ; if (*rdx == *s)
je done ; return rax ;
inc rdx ; rdx++
jmp _while ; }
_done:
inc rdi ; haystack++
jmp while ; }
done:
mov rax, rdi ; rax = haystack
cmp byte [rax], 0 ; if (*rax == 0)
jne return
mov rax, 0x0 ; rax = NULL;
return:
pop rdi ; restore rdi
leave
ret ; return rax