Adds strcmp() (note: much different than strcmp in C)

This commit is contained in:
2025-07-09 23:44:40 +02:00
parent 791cccc262
commit 08389a5bb2
2 changed files with 129 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ section .text
global strcpy
global strcat
global strclr
global strcmp
;----- strlen(*str[]) -----;
; Gets the length of given string
@@ -102,3 +103,43 @@ strclr:
cld
rep stosb
ret
;----- strcmp(*str1[], *str2[]) -----;
; Compares str2 against str1
; Return values:
; 0 Two strings are the same, OR str1 or str2 is empty
; Length of str1 str2 is longer than str1, but no difference was found in between str1 and str2 -up to the point str1 ended-
; Anything else Position (index+1) where str1 and str2 differ
; Used registers:
; rax* (ret)
; rdi Pointer to str1
; rsi Pointer to str2
; rcx* Counter for scasb
; rdx* Length of str1
; r8* Backup for rdi
strcmp:
mov rax, -1
cmp byte [rdi], EOS
je .equal
cmp byte [rsi], EOS
je .equal
mov r8, rdi
call strlen
mov rcx, rax
mov rdx, rax
mov rdi, r8
cld
repe cmpsb
je .equal
mov rax, rdx
sub rax, rcx
jmp .quit
.equal:
cmp byte [rsi], EOS
jne .quit
mov rax, -1
.quit:
ret

View File

@@ -13,6 +13,7 @@ extern strlen
extern strcpy
extern strcat
extern strclr
extern strcmp
; convert.asm
extern dec2str
extern udec2str
@@ -30,6 +31,7 @@ section .rodata
TEST_strclr equ 1
TEST_islower equ 1
TEST_isupper equ 1
TEST_strcmp equ 1
str1 db "Hello, world!",EOS
str2 db "Hello, World!",EOS
@@ -96,7 +98,7 @@ section .rodata
strclr1b db TAB,"print(strBuff1[3]): ",NL,TAB,TAB,EOS
strclr2 db TAB,"strcat(strBuff1, str5, 32)",NL,TAB,"strclr(strbuff1, 2)",NL,TAB,"print(strbuff1[3]): ",NL,TAB,TAB,EOS
;islower()
; islower()
msgIslower db NL,"TEST islower()",NL,EOS
islower1 db TAB,"islower(str1[0]): %d",NL,EOS
islower2 db TAB,"islower(str1[1]): %d",NL,EOS
@@ -104,7 +106,7 @@ section .rodata
islower4 db TAB,"islower('D'): %d",NL,EOS
islower5 db TAB,"islower('!'): %d",NL,EOS
;isupper()
; isupper()
msgIsupper db NL,"TEST isupper()",NL,EOS
isupper1 db TAB,"isupper(str1[0]): %d",NL,EOS
isupper2 db TAB,"isupper(str1[1]): %d",NL,EOS
@@ -112,6 +114,15 @@ section .rodata
isupper4 db TAB,"isupper('D'): %d",NL,EOS
isupper5 db TAB,"isupper('!'): %d",NL,EOS
; strcmp()
msgStrcmp db NL,"TEST strcmp()",NL,EOS
strcmp1 db TAB,"strcmp(str1, str1): %d",NL,EOS
strcmp2 db TAB,"strcmp(str1, str2): %d",NL,EOS
strcmp3 db TAB,"- strcpy(strBuff1, str1)",NL,TAB,"- strcat(strBuff1, str2)",NL,TAB,"> strBuff1: %s",NL,TAB,"> str1: %s",NL,TAB,"strcmp(str1, strBuff1): %d",NL,EOS
strcmp4 db TAB,"strcmp(strBuff1, str1): %d",NL,EOS
strcmp5 db TAB,"- strclr(strBuff1, 32)",NL,TAB,"strcmp(strBuff1, str1): %d",NL,TAB,"strcmp(str1, strBuff1): %d",NL,EOS
strcmp6 db TAB,"strcmp(str1, str5): %d",NL,EOS
section .data
section .bss
@@ -408,9 +419,11 @@ _start:
lea rdi, [rel strBuff1]
mov rsi, 2
call strclr
;lea rdi, [rel strBuff1] ;will add output for this later < works as expected tho
;call puts
lea rdi, [rel strBuff1]
add rdi, 3
call puts
call puts
%endif
;---
@@ -499,6 +512,78 @@ _start:
call printf
%endif
;---
;--- strcmp()
;---
%if TEST_strcmp
lea rdi, [rel msgStrcmp]
call printf
; TEST 1
lea rdi, [rel str1]
lea rsi, [rel str1]
call strcmp
lea rdi, [rel strcmp1]
mov rsi, rax
call printf
; TEST 2
lea rdi, [rel str1]
lea rsi, [rel str2]
call strcmp
lea rdi, [rel strcmp2]
mov rsi, rax
call printf
; TEST 3
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcpy
lea rdi, [rel strBuff1]
lea rsi, [rel str2]
call strcat
lea rdi, [rel str1]
lea rsi, [rel strBuff1]
call strcmp
lea rdi, [rel strcmp3]
lea rsi, [rel strBuff1]
lea rdx, [rel str1]
mov rcx, rax
call printf
; TEST 4
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcmp
lea rdi, [rel strcmp4]
mov rsi, rax
call printf
; TEST 5
lea rdi, [rel strBuff1]
mov rsi, 32
call strclr
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcmp
mov rbx, rax
lea rdi, [rel str1]
lea rsi, [rel strBuff1]
call strcmp
lea rdi, [rel strcmp5]
mov rsi, rbx
mov rdx, rax
call printf
; TEST 6
lea rdi, [rel str1]
lea rsi, [rel str5]
call strcmp
lea rdi, [rel strcmp6]
mov rsi, rax
call printf
%endif
;---
;--- exit()
;---