(alternative) strcmp()
This commit is contained in:
31
string.asm
31
string.asm
@ -2,6 +2,7 @@ section .text
|
||||
global strlen
|
||||
global strcpy
|
||||
global strcat
|
||||
global strcmp
|
||||
|
||||
;----- strlen(char* str) -----;
|
||||
; return value: amount of characters before EOS was found
|
||||
@ -63,3 +64,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
|
||||
; returns -1 if str2 is longer than str1 and no difference was found before that happens
|
||||
strcmp:
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
|
||||
call strlen
|
||||
mov rcx, rax
|
||||
mov rax, -1
|
||||
.compareLoop:
|
||||
inc rax
|
||||
cmp byte [rdi+rax], 0x0
|
||||
je .eosFound
|
||||
mov r10b, byte [rsi+rax]
|
||||
cmp byte [rdi+rax], r10b
|
||||
je .compareLoop
|
||||
jmp .quit
|
||||
|
||||
.eosFound:
|
||||
mov r10, rax
|
||||
mov rdi, rsi
|
||||
call strlen
|
||||
cmp rax, r10
|
||||
jle .quit
|
||||
mov rax, -1
|
||||
|
||||
.quit:
|
||||
leave
|
||||
ret
|
||||
|
73
tests.asm
73
tests.asm
@ -16,6 +16,7 @@ extern puts
|
||||
extern strlen
|
||||
extern strcpy
|
||||
extern strcat
|
||||
extern strcmp
|
||||
|
||||
section .rodata
|
||||
PRINT_nums equ 1
|
||||
@ -31,6 +32,7 @@ section .rodata
|
||||
TEST_strcat equ 1
|
||||
TEST_tolower equ 1
|
||||
TEST_toupper equ 1
|
||||
TEST_strcmp equ 1
|
||||
|
||||
num1 dq 13
|
||||
num2 dq -25
|
||||
@ -54,18 +56,21 @@ section .rodata
|
||||
msgIslower2 db "islower(str1[1]): %d",10,0
|
||||
msgIsupper db "isupper(str1[0]): %d",10,0
|
||||
msgIsupper2 db "isupper(str1[1]): %d",10,0
|
||||
msgStrcpy db "strcpy(str1Copy, str1): %s",10,0
|
||||
msgStrcat db "strcat(str1Copy, str3): %s",10,0
|
||||
msgStrlen3 db "strlen(str1Copy): %d",10,0
|
||||
msgStrcpy db "strcpy(strBuff, str1): %s",10,0
|
||||
msgStrcat db "strcat(strBuff, str3): %s",10,0
|
||||
msgStrlen3 db "strlen(strBuff): %d",10,0
|
||||
msgTolower db "tolower(str1[0]): %c",10,0
|
||||
msgTolower2 db "tolower(str1[1]): %c",10,0
|
||||
msgTolower3 db "tolower() for whole str1Copy: %s",10,0
|
||||
msgTolower3 db "tolower() for whole strBuff: %s",10,0
|
||||
msgToupper db "toupper(str1[0]): %c",10,0
|
||||
msgToupper2 db "toupper(str1[1]): %c",10,0
|
||||
msgToupper3 db "toupper() for whole str1Copy: %s",10,0
|
||||
msgToupper3 db "toupper() for whole strBuff: %s",10,0
|
||||
msgStrcmp db "strcmp(str1, str2): %d",10,0
|
||||
msgStrings2 db "Comparing...",10,"> str1: %s",10,"> strBuff: %s",10,0
|
||||
msgStrcmp2 db "strcmp(str1, strBuff): %d",10,0
|
||||
section .data
|
||||
section .bss
|
||||
str1Copy resb 32
|
||||
strBuff resb 32
|
||||
section .text
|
||||
global main
|
||||
main:
|
||||
@ -202,28 +207,28 @@ main:
|
||||
%ENDIF
|
||||
%IF TEST_strcpy
|
||||
; TEST: strcpy()
|
||||
lea rdi, [rel str1Copy]
|
||||
lea rdi, [rel strBuff]
|
||||
lea rsi, [rel str1]
|
||||
call strcpy
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgStrcpy]
|
||||
lea rsi, [rel str1Copy]
|
||||
lea rsi, [rel strBuff]
|
||||
call printf
|
||||
%ENDIF
|
||||
%IF TEST_strcat
|
||||
; TEST: strcat()
|
||||
lea rdi, [rel str1Copy]
|
||||
lea rdi, [rel strBuff]
|
||||
lea rsi, [rel str3]
|
||||
call strcat
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgStrcat]
|
||||
lea rsi, [rel str1Copy]
|
||||
lea rsi, [rel strBuff]
|
||||
call printf
|
||||
%ENDIF
|
||||
%IF TEST_strcat
|
||||
%IF TEST_strlen
|
||||
; TEST: strlen() for str1Copy
|
||||
lea rdi, [rel str1Copy]
|
||||
; TEST: strlen() for strBuff
|
||||
lea rdi, [rel strBuff]
|
||||
call strlen
|
||||
mov rsi, rax
|
||||
xor rax, rax
|
||||
@ -264,8 +269,8 @@ main:
|
||||
call printf
|
||||
%ENDIF
|
||||
%IF TEST_tolower
|
||||
; TEST: tolower: entire str1Copy
|
||||
lea rdi, [rel str1Copy]
|
||||
; TEST: tolower: entire strBuff
|
||||
lea rdi, [rel strBuff]
|
||||
call strlen
|
||||
mov rcx, rax
|
||||
.tolowerLoop:
|
||||
@ -282,12 +287,12 @@ main:
|
||||
loop .tolowerLoop
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgTolower3]
|
||||
lea rsi, [rel str1Copy]
|
||||
lea rsi, [rel strBuff]
|
||||
call printf
|
||||
%ENDIF
|
||||
%IF TEST_toupper
|
||||
; TEST: toupper: entire str1Copy
|
||||
lea rdi, [rel str1Copy]
|
||||
; TEST: toupper: entire strBuff
|
||||
lea rdi, [rel strBuff]
|
||||
call strlen
|
||||
mov rcx, rax
|
||||
.toupperLoop:
|
||||
@ -304,9 +309,39 @@ main:
|
||||
loop .toupperLoop
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgToupper3]
|
||||
lea rsi, [rel str1Copy]
|
||||
lea rsi, [rel strBuff]
|
||||
call printf
|
||||
%ENDIF
|
||||
%IF TEST_strcmp
|
||||
; TEST: strcmp(str1, str2)
|
||||
lea rdi, [rel str1]
|
||||
lea rsi, [rel str2]
|
||||
call strcmp
|
||||
mov rsi, rax
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgStrcmp]
|
||||
call printf
|
||||
;
|
||||
lea rdi, [rel strBuff]
|
||||
lea rsi, [rel str1]
|
||||
call strcpy
|
||||
lea rdi, [rel strBuff]
|
||||
lea rsi, [rel str3]
|
||||
call strcat
|
||||
%IF PRINT_strs
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgStrings2]
|
||||
lea rsi, [rel str1]
|
||||
lea rdx, [rel strBuff]
|
||||
call printf
|
||||
%ENDIF
|
||||
lea rdi, [rel str1]
|
||||
lea rsi, [rel strBuff]
|
||||
call strcmp
|
||||
mov rsi, rax
|
||||
xor rax, rax
|
||||
lea rdi, [rel msgStrcmp2]
|
||||
call printf
|
||||
%ENDIF
|
||||
|
||||
leave
|
||||
ret
|
||||
|
Reference in New Issue
Block a user