aboutsummaryrefslogtreecommitdiff
path: root/src/lib/strcmp.s
blob: c24b5b5a06433ca59d0610e15b3aa8dbbfda6dc4 (plain)
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
# --------------------------------------------
# FUNCTION: strcmp
# PURPOSE : compares twos strings, and returns
#           if they are equal
# INPUTS  : rdi = address of first string
#           rsi = address of second string
# OUTPUTS : rax = 1 if equals else 0
# CLOBBERS: 
# --------------------------------------------
.section .text
.globl strcmp
.type strcmp, @function
strcmp:
    xorq %rax, %rax                     # i = 0

loop:
    movb (%rdi, %rax, 1), %r8b
    movb (%rsi, %rax, 1), %r9b

    cmpb %r8b, %r9b                     # compare each byte
    jne .strcmp_fail

    testb %r8b, %r8b                    # if any is null,
    jz .strcmp_success                  # both are null here

    incq %rax

    jmp loop

.strcmp_fail:
    xorq %rax, %rax
    ret

.strcmp_success:
    movq $1, %rax
    ret