Fix strcmp()

This commit is contained in:
2025-06-25 08:35:25 +02:00
parent 6df67b18f4
commit 9d265e3db6
2 changed files with 47 additions and 25 deletions

View File

@ -96,32 +96,33 @@ strcat:
leave
ret
;----- strcmp(char* str1, char* str2) -----;
; return value: 0 if both strings are the same, otherwise index in array str1 where strings are to become different
; return value: 0 if both strings are the same, otherwise index in array str1 where strings mismatch
; returns -1 if str2 is longer than str1 and no difference was found before that happens
; TODO: FIX length 13 for TEST strcmp(strBuff1, str1) (see tests.asm, strcmp test 1)
strcmp:
push rbp
mov rbp, rsp
call strlen
mov rcx, rax
mov rax, -1
xor r10, r10
.compareLoop:
inc rax
cmp byte [rdi+rax], 0x0
je .eosFound
mov r10b, byte [rsi+rax]
cmp byte [rdi+rax], r10b
je .compareLoop
cmp byte [rdi], 0x0
je .0f
mov r11b, byte [rsi]
cmp byte [rdi], r11b
jne .mismatch
inc rdi
inc rsi
inc r10
jmp .compareLoop
.0f:
mov rax, -1
cmp byte [rsi], 0x0
jne .quit
xor rax, rax
jmp .quit
.eosFound:
mov r10, rax
mov rdi, rsi
call strlen
cmp rax, r10
jle .quit
mov rax, -1
.mismatch:
mov rax, r10
.quit:
leave

View File

@ -133,12 +133,14 @@ section .rodata
msgToupper1 db "TEST toupper(strBuff1[0]): %c",NL,EOS
msgToupper2 db "TEST toupper for whole strBuff1: %s",NL,EOS
; strcmp()
msgStrcmp db NL,"# strcmp()",NL,"strlclr(strBuff1, 32)",NL,"strcpy(strBuff1, str1)",EOS
msgStrcmp db NL,"# strcmp()",NL,"strlclr(strBuff1, 32)",NL,"strcpy(strBuff1, str1)",NL,"toupper(strBuff1[7])",EOS
msgStrcmpStrs db "> str1: %s",NL,"> str2: %s",NL,"> str4: %s",NL,"> strBuff1: %s",NL,EOS
msgStrcmp1 db "TEST strcmp(strBuff1, str1): %d",NL,EOS
msgStrcmp2 db "TEST strcmp(strBuff1, str2): %d",NL,EOS
msgStrcmp3 db "TEST strcmp(str4, str1): %d",NL,EOS
msgStrcmp4 db "TEST strcmp(str1, str4): %d",NL,EOS
msgStrcmp2 db "TEST strcmp(str1, str2): %d",NL,EOS
msgStrcmp3 db "TEST strcmp(strBuff1, str2): %d",NL,EOS
msgStrcmp4 db "TEST strcmp(str4, str1): %d",NL,EOS
msgStrcmp5 db "TEST strcmp(str1, str4): %d",NL,EOS
msgStrcmp6 db "TEST strcmp(str1, str1): %d",NL,EOS
section .bss
strBuff1 resb 32
strBuff2 resb 8
@ -649,6 +651,9 @@ main:
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcpy
lea rdi, [rel strBuff1+7]
call toupper
mov [strBuff1+7], al
xor rax, rax
lea rdi, [rel msgStrcmpStrs]
@ -666,13 +671,21 @@ main:
xor rax, rax
lea rdi, [rel msgStrcmp1]
call printf
; TEST: strcmp(str1, str2)
lea rdi, [rel str1]
lea rsi, [rel str2]
call strcmp
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrcmp2]
call printf
; TEST: strcmp(strBuff1, str2)
lea rdi, [rel strBuff1]
lea rsi, [rel str2]
call strcmp
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrcmp2]
lea rdi, [rel msgStrcmp3]
call printf
; TEST: strcmp(str4, str1)
lea rdi, [rel str4]
@ -680,7 +693,7 @@ main:
call strcmp
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrcmp3]
lea rdi, [rel msgStrcmp4]
call printf
; TEST: strcmp(str1, str4)
lea rdi, [rel str1]
@ -688,7 +701,15 @@ main:
call strcmp
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrcmp4]
lea rdi, [rel msgStrcmp5]
call printf
; TEST: strcmp(str1, str1)
lea rdi, [rel str1]
mov rsi, rdi
call strcmp
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrcmp6]
call printf
%ENDIF