Fix strcmp()
This commit is contained in:
37
string.asm
37
string.asm
@ -96,32 +96,33 @@ strcat:
|
|||||||
leave
|
leave
|
||||||
ret
|
ret
|
||||||
;----- strcmp(char* str1, char* str2) -----;
|
;----- 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
|
; 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:
|
strcmp:
|
||||||
push rbp
|
push rbp
|
||||||
mov rbp, rsp
|
mov rbp, rsp
|
||||||
|
|
||||||
call strlen
|
call strlen
|
||||||
mov rcx, rax
|
xor r10, r10
|
||||||
mov rax, -1
|
|
||||||
.compareLoop:
|
.compareLoop:
|
||||||
inc rax
|
cmp byte [rdi], 0x0
|
||||||
cmp byte [rdi+rax], 0x0
|
je .0f
|
||||||
je .eosFound
|
mov r11b, byte [rsi]
|
||||||
mov r10b, byte [rsi+rax]
|
cmp byte [rdi], r11b
|
||||||
cmp byte [rdi+rax], r10b
|
jne .mismatch
|
||||||
je .compareLoop
|
inc rdi
|
||||||
jmp .quit
|
inc rsi
|
||||||
|
inc r10
|
||||||
|
jmp .compareLoop
|
||||||
|
.0f:
|
||||||
|
mov rax, -1
|
||||||
|
cmp byte [rsi], 0x0
|
||||||
|
jne .quit
|
||||||
|
xor rax, rax
|
||||||
|
jmp .quit
|
||||||
|
|
||||||
.eosFound:
|
.mismatch:
|
||||||
mov r10, rax
|
mov rax, r10
|
||||||
mov rdi, rsi
|
|
||||||
call strlen
|
|
||||||
cmp rax, r10
|
|
||||||
jle .quit
|
|
||||||
mov rax, -1
|
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
leave
|
||||||
|
35
tests.asm
35
tests.asm
@ -133,12 +133,14 @@ section .rodata
|
|||||||
msgToupper1 db "TEST toupper(strBuff1[0]): %c",NL,EOS
|
msgToupper1 db "TEST toupper(strBuff1[0]): %c",NL,EOS
|
||||||
msgToupper2 db "TEST toupper for whole strBuff1: %s",NL,EOS
|
msgToupper2 db "TEST toupper for whole strBuff1: %s",NL,EOS
|
||||||
; strcmp()
|
; 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
|
msgStrcmpStrs db "> str1: %s",NL,"> str2: %s",NL,"> str4: %s",NL,"> strBuff1: %s",NL,EOS
|
||||||
msgStrcmp1 db "TEST strcmp(strBuff1, str1): %d",NL,EOS
|
msgStrcmp1 db "TEST strcmp(strBuff1, str1): %d",NL,EOS
|
||||||
msgStrcmp2 db "TEST strcmp(strBuff1, str2): %d",NL,EOS
|
msgStrcmp2 db "TEST strcmp(str1, str2): %d",NL,EOS
|
||||||
msgStrcmp3 db "TEST strcmp(str4, str1): %d",NL,EOS
|
msgStrcmp3 db "TEST strcmp(strBuff1, str2): %d",NL,EOS
|
||||||
msgStrcmp4 db "TEST strcmp(str1, str4): %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
|
section .bss
|
||||||
strBuff1 resb 32
|
strBuff1 resb 32
|
||||||
strBuff2 resb 8
|
strBuff2 resb 8
|
||||||
@ -649,6 +651,9 @@ main:
|
|||||||
lea rdi, [rel strBuff1]
|
lea rdi, [rel strBuff1]
|
||||||
lea rsi, [rel str1]
|
lea rsi, [rel str1]
|
||||||
call strcpy
|
call strcpy
|
||||||
|
lea rdi, [rel strBuff1+7]
|
||||||
|
call toupper
|
||||||
|
mov [strBuff1+7], al
|
||||||
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
lea rdi, [rel msgStrcmpStrs]
|
lea rdi, [rel msgStrcmpStrs]
|
||||||
@ -666,13 +671,21 @@ main:
|
|||||||
xor rax, rax
|
xor rax, rax
|
||||||
lea rdi, [rel msgStrcmp1]
|
lea rdi, [rel msgStrcmp1]
|
||||||
call printf
|
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)
|
; TEST: strcmp(strBuff1, str2)
|
||||||
lea rdi, [rel strBuff1]
|
lea rdi, [rel strBuff1]
|
||||||
lea rsi, [rel str2]
|
lea rsi, [rel str2]
|
||||||
call strcmp
|
call strcmp
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
lea rdi, [rel msgStrcmp2]
|
lea rdi, [rel msgStrcmp3]
|
||||||
call printf
|
call printf
|
||||||
; TEST: strcmp(str4, str1)
|
; TEST: strcmp(str4, str1)
|
||||||
lea rdi, [rel str4]
|
lea rdi, [rel str4]
|
||||||
@ -680,7 +693,7 @@ main:
|
|||||||
call strcmp
|
call strcmp
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
lea rdi, [rel msgStrcmp3]
|
lea rdi, [rel msgStrcmp4]
|
||||||
call printf
|
call printf
|
||||||
; TEST: strcmp(str1, str4)
|
; TEST: strcmp(str1, str4)
|
||||||
lea rdi, [rel str1]
|
lea rdi, [rel str1]
|
||||||
@ -688,7 +701,15 @@ main:
|
|||||||
call strcmp
|
call strcmp
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
xor rax, 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
|
call printf
|
||||||
%ENDIF
|
%ENDIF
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user