Adds strlcpy()
This commit is contained in:
31
string.asm
31
string.asm
@ -1,6 +1,7 @@
|
|||||||
section .text
|
section .text
|
||||||
global strlen
|
global strlen
|
||||||
global strcpy
|
global strcpy
|
||||||
|
global strlcpy
|
||||||
global strcat
|
global strcat
|
||||||
global strcmp
|
global strcmp
|
||||||
|
|
||||||
@ -48,6 +49,36 @@ strcpy:
|
|||||||
.quit:
|
.quit:
|
||||||
leave
|
leave
|
||||||
ret
|
ret
|
||||||
|
;----- strlcpy(char* dest, char* src, int size) -----;
|
||||||
|
; return value: Length of src
|
||||||
|
strlcpy:
|
||||||
|
push rbp
|
||||||
|
mov rbp, rsp
|
||||||
|
|
||||||
|
mov r9, rsi
|
||||||
|
test rdx, rdx
|
||||||
|
jz .quit
|
||||||
|
js .quit
|
||||||
|
dec rdx
|
||||||
|
.loop:
|
||||||
|
cmp byte [rsi], 0x0
|
||||||
|
je .0f
|
||||||
|
mov r10b, byte [rsi]
|
||||||
|
mov byte [rdi], r10b
|
||||||
|
inc rsi
|
||||||
|
inc rdi
|
||||||
|
dec rdx
|
||||||
|
cmp rdx, 0x0
|
||||||
|
ja .loop
|
||||||
|
|
||||||
|
.0f:
|
||||||
|
mov byte [rdi], 0x0
|
||||||
|
|
||||||
|
.quit:
|
||||||
|
mov rdi, r9
|
||||||
|
call strlen
|
||||||
|
leave
|
||||||
|
ret
|
||||||
;----- strcat(char* dest, char* src) -----;
|
;----- strcat(char* dest, char* src) -----;
|
||||||
; return value: pointer to dest
|
; return value: pointer to dest
|
||||||
strcat:
|
strcat:
|
||||||
|
40
tests.asm
40
tests.asm
@ -15,6 +15,7 @@ extern puts
|
|||||||
;string.asm
|
;string.asm
|
||||||
extern strlen
|
extern strlen
|
||||||
extern strcpy
|
extern strcpy
|
||||||
|
extern strlcpy
|
||||||
extern strcat
|
extern strcat
|
||||||
extern strcmp
|
extern strcmp
|
||||||
|
|
||||||
@ -29,6 +30,7 @@ section .rodata
|
|||||||
TEST_islower equ 1
|
TEST_islower equ 1
|
||||||
TEST_isupper equ 1
|
TEST_isupper equ 1
|
||||||
TEST_strcpy equ 1
|
TEST_strcpy equ 1
|
||||||
|
TEST_strlcpy equ 1
|
||||||
TEST_strcat equ 1
|
TEST_strcat equ 1
|
||||||
TEST_tolower equ 1
|
TEST_tolower equ 1
|
||||||
TEST_toupper equ 1
|
TEST_toupper equ 1
|
||||||
@ -57,6 +59,9 @@ section .rodata
|
|||||||
msgIsupper db "isupper(str1[0]): %d",10,0
|
msgIsupper db "isupper(str1[0]): %d",10,0
|
||||||
msgIsupper2 db "isupper(str1[1]): %d",10,0
|
msgIsupper2 db "isupper(str1[1]): %d",10,0
|
||||||
msgStrcpy db "strcpy(strBuff, str1): %s",10,0
|
msgStrcpy db "strcpy(strBuff, str1): %s",10,0
|
||||||
|
msgStrlcpy db "strlcpy(strBuff2, str1, -1): (%d): %s",10,0
|
||||||
|
msgStrlcpy2 db "strlcpy(strBuff2, str1, 0): (%d): %s",10,0
|
||||||
|
msgStrlcpy3 db "strlcpy(strBuff2, str1, 8): (%d): %s",10,0
|
||||||
msgStrcat db "strcat(strBuff, str3): %s",10,0
|
msgStrcat db "strcat(strBuff, str3): %s",10,0
|
||||||
msgStrlen3 db "strlen(strBuff): %d",10,0
|
msgStrlen3 db "strlen(strBuff): %d",10,0
|
||||||
msgTolower db "tolower(str1[0]): %c",10,0
|
msgTolower db "tolower(str1[0]): %c",10,0
|
||||||
@ -70,7 +75,8 @@ section .rodata
|
|||||||
msgStrcmp2 db "strcmp(str1, strBuff): %d",10,0
|
msgStrcmp2 db "strcmp(str1, strBuff): %d",10,0
|
||||||
section .data
|
section .data
|
||||||
section .bss
|
section .bss
|
||||||
strBuff resb 32
|
strBuff resb 32
|
||||||
|
strBuff2 resb 5
|
||||||
section .text
|
section .text
|
||||||
global main
|
global main
|
||||||
main:
|
main:
|
||||||
@ -215,6 +221,38 @@ main:
|
|||||||
lea rsi, [rel strBuff]
|
lea rsi, [rel strBuff]
|
||||||
call printf
|
call printf
|
||||||
%ENDIF
|
%ENDIF
|
||||||
|
%IF TEST_strlcpy
|
||||||
|
; TEST: strlcpy(strBuff2, str1, -1)
|
||||||
|
lea rdi, [rel strBuff2]
|
||||||
|
lea rsi, [rel str1]
|
||||||
|
mov rdx, -1
|
||||||
|
call strlcpy
|
||||||
|
mov rsi, rax
|
||||||
|
xor rax, rax
|
||||||
|
lea rdi, [rel msgStrlcpy]
|
||||||
|
lea rdx, [rel strBuff2]
|
||||||
|
call printf
|
||||||
|
; TEST: strlcpy(strBuff2, str1, 0)
|
||||||
|
lea rdi, [rel strBuff2]
|
||||||
|
lea rsi, [rel str1]
|
||||||
|
mov rdx, 0
|
||||||
|
call strlcpy
|
||||||
|
mov rsi, rax
|
||||||
|
xor rax, rax
|
||||||
|
lea rdi, [rel msgStrlcpy2]
|
||||||
|
lea rdx, [rel strBuff2]
|
||||||
|
call printf
|
||||||
|
; TEST: strlcpy(strBuff2, str1, 8)
|
||||||
|
lea rdi, [rel strBuff2]
|
||||||
|
lea rsi, [rel str1]
|
||||||
|
mov rdx, 5
|
||||||
|
call strlcpy
|
||||||
|
mov rsi, rax
|
||||||
|
xor rax, rax
|
||||||
|
lea rdi, [rel msgStrlcpy3]
|
||||||
|
lea rdx, [rel strBuff2]
|
||||||
|
call printf
|
||||||
|
%ENDIF
|
||||||
%IF TEST_strcat
|
%IF TEST_strcat
|
||||||
; TEST: strcat()
|
; TEST: strcat()
|
||||||
lea rdi, [rel strBuff]
|
lea rdi, [rel strBuff]
|
||||||
|
Reference in New Issue
Block a user