LibC: Fix memchr and memcmp assembly implementations

These functions did not work with size 0 :D
This commit is contained in:
Bananymous 2025-01-23 21:55:26 +02:00
parent ee078fc638
commit 15021b442c
2 changed files with 10 additions and 6 deletions

View File

@ -3,10 +3,11 @@ memchr:
xchgl 4(%esp), %edi xchgl 4(%esp), %edi
movl 8(%esp), %eax movl 8(%esp), %eax
movl 12(%esp), %ecx movl 12(%esp), %ecx
movl $1, %edx
cmpl $1, %ecx # clear ZF if count is zero
repne scasb repne scasb
xorl %eax, %eax cmovel %edi, %edx
testl %ecx, %ecx leal -1(%edx), %eax
cmovnzl %edi, %eax
movl 4(%esp), %edi movl 4(%esp), %edi
ret ret
@ -15,6 +16,7 @@ memcmp:
xchgl 4(%esp), %edi xchgl 4(%esp), %edi
xchgl 8(%esp), %esi xchgl 8(%esp), %esi
movl 12(%esp), %ecx movl 12(%esp), %ecx
testl %ecx, %ecx # set ZF if count is zero
repe cmpsb repe cmpsb
jne .memcmp_not_equal jne .memcmp_not_equal
xorl %eax, %eax xorl %eax, %eax

View File

@ -2,15 +2,17 @@
memchr: memchr:
movb %sil, %al movb %sil, %al
movq %rdx, %rcx movq %rdx, %rcx
movq $1, %rdx
cmpq $1, %rcx # clear ZF if count is zero
repne scasb repne scasb
xorq %rax, %rax cmoveq %rdi, %rdx
testq %rcx, %rcx leaq -1(%rdx), %rax
cmovnzq %rdi, %rax
ret ret
.global memcmp .global memcmp
memcmp: memcmp:
movq %rdx, %rcx movq %rdx, %rcx
testq %rcx, %rcx # set ZF if count is zero
repe cmpsb repe cmpsb
jne .memcmp_not_equal jne .memcmp_not_equal
xorq %rax, %rax xorq %rax, %rax