-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrncmp.s
38 lines (32 loc) · 1.17 KB
/
strncmp.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
BITS 64
global strncmp
strncmp:
push rbp ; Prologue
mov rbp, rsp ; (==enter)
mov rax, 0 ; result = 0
mov r9d, 0 ; char a = NULL
mov r10d, 0 ; char b = NULL
mov r8d, 0 ; int index = 0
.loop: ; while
cmp r8d, edx ; if (index == n)
je .end ; return
mov r9b, [rdi] ; a = str1
mov r10b, [rsi] ; b = str2
cmp byte r9b, r10b ; if (str1[i] < str2[i])
jl .first ; return -1
jg .second ; return 1
cmp r9b, 0 ; if (str1 == NULL)
je .end ; return
inc rdi ; str1++
inc rsi ; str2++
inc r8d ; ++index
jmp .loop
.first:
mov rax, -1 ; return -1
jmp .end ; end
.second:
mov rax, 1 ; return 1
jmp .end ; end
.end:
leave
ret