Files
klibc/src/tests.asm
2025-07-09 11:41:21 +02:00

360 lines
6.4 KiB
NASM

%include "src/constants.asm"
; core.asm
extern exit
; console.asm
extern print
extern puts
extern printf
; string.asm
extern strlen
extern strcpy
extern strcat
extern strclr
; convert.asm
extern dec2str
extern udec2str
extern hex2str
section .rodata
TEST_print equ 1
TEST_puts equ 1
TEST_dec2str equ 1 ;includes udec2str
TEST_hex2str equ 1
TEST_printf equ 1
TEST_strlen equ 1
TEST_strcpy equ 1
TEST_strcat equ 1
TEST_strclr equ 1
str1 db "Hello, world!",EOS
str2 db "Hello, World!",EOS
str3 db "Hello world!",EOS
str4 db "Howdy environment!",EOS
str5 db "The quick brown fox jumps over the lazy dog",EOS
; print()
msgPrint db "TEST print()",NL,EOS
; puts()
msgPuts db "TEST puts()",EOS
; printf()
msgPrintf db "TEST printf()",NL,EOS
printf1 db TAB,"printf(",DQUO,"He%ll%o there%%%%%!%!%\n",DQUO,"): ",NL,TAB,TAB,EOS
printf1Str db "He%ll%o there%%%%%!%!%",NL,EOS
printf2 db TAB,"printf(",DQUO,"Are %s doing %s?\n",DQUO,", printf2Str1, printf2Str2): ",NL,TAB,TAB,EOS
printf2Str db "Are %s doing %s?",NL,EOS
printf2Str1 db "you",EOS
printf2Str2 db "okay",EOS
printf3 db TAB,"printf(",DQUO,"%c%c%c%c%c there %c%c %s%c\n",DQUO,"): ",NL,TAB,TAB,EOS
printf3Str db "%c%c%c%c%c there %c%c %s%c",NL,EOS
printf3C1 equ 'H'
printf3C2 equ 'e'
printf3C3 equ 'l'
printf3C4 equ 'l'
printf3C5 equ 'o'
printf3C6 equ 'm'
printf3C7 equ 'y'
printf3Str1 db "friend",EOS
printf3C8 equ '!'
printf4 db TAB,"printf(",DQUO,"%i|%d|%u , %i|%d|%u\n",DQUO,", -50, -50, -50, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF): ",NL,TAB,TAB,EOS
printf4Str db "%i|%d|%u , %i|%d|%u",NL,EOS
printf5 db TAB,"printf(",DQUO,"%x|%X , %x|%X\n",DQUO,", 0xabcdeffedcba, 0x069bc0e, 666, -1): ",NL,TAB,TAB,EOS
printf5Str db "%x|%X , %x|%X",NL,EOS
; strlen()
strlenStr1 db "Hello",EOS
strlenStr2 db "Hello, world!",NL,EOS
; strcpy()
msgStrcpy db "TEST strcpy()",NL,EOS
strcpy1 db TAB,"strcpy(strBuff1, str1, 12): ",NL,TAB,TAB,EOS
strcpy2 db TAB,"strcpy(strBuff1, str1, 13): ",NL,TAB,TAB,EOS
strcpy3 db TAB,"strcpy(strBuff1, str1, 32): ",NL,TAB,TAB,EOS
strcpy4 db TAB,"strcpy(strBuff1, str5, 32): ",NL,TAB,TAB,EOS
; strcat()
msgStrcat db "TEST strcat()",NL,EOS
strcat1 db TAB,"strcat(strBuff2, str1, 32): ",NL,TAB,TAB,EOS
strcat2 db TAB,"strcat(strBuff2, str5, 32): ",NL,TAB,TAB,EOS
;strclr()
msgStrclr db "TEST strclr()",NL,EOS
strclr1a db TAB,"strcat(strBuff1, str5, 32)",NL,TAB,"strclr(strBuff1, 32)",NL,TAB,"print(strBuff1): ",NL,TAB,TAB,EOS
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
section .data
section .bss
strBuff1 resb 32
strBuff2 resb 32
section .text
global _start
_start:
push rbp
mov rbp, rsp
sub rsp, SIZE_QWORD
;---
;--- print()
;---
%if TEST_print
lea rdi, [rel msgPrint]
call print
%endif
;---
;--- puts()
;---
%if TEST_puts
lea rdi, [rel msgPuts]
call puts
%endif
;---
;--- dec2str() / udec2str()
;---
%if TEST_dec2str
; x/s $rax after calls to confirm proper output
mov rdi, -569384
call dec2str
mov rdi, -569384
call udec2str
mov rdi, 0xFFFFFFFFFFFFFFFF
call dec2str
mov rdi, 0xFFFFFFFFFFFFFFFF
call udec2str
%endif
;---
;--- hex2str()
;---
%if TEST_hex2str
; TEST 1
xor rdi, rdi
mov rsi, 0
call hex2str
mov rdi, -1
mov rsi, 0
call hex2str
; TEST 2
xor rdi, rdi
mov rsi, 1
call hex2str
mov rdi, -1
mov rsi, 1
call hex2str
; TEST 3
mov rdi, 0x123456789ABCDEF
mov rsi, 0
call hex2str
mov rdi, 0x123456789ABCDEF
mov rsi, 1
call hex2str
; TEST 4
mov rdi, 128
mov rsi, 0
call hex2str
%endif
;---
;--- printf()
;---
%if TEST_printf
lea rdi, [rel msgPrintf]
call printf
; TEST 1
lea rdi, [rel printf1]
call print
lea rdi, [rel printf1Str]
call printf
; TEST 2
lea rdi, [rel printf2]
call print
lea rdi, [rel printf2Str]
lea rsi, [rel printf2Str1]
lea rdx, [rel printf2Str2]
call printf
; TEST 3
lea rdi, [rel printf3]
call print
lea rdi, [rel printf3Str]
mov rsi, printf3C1
mov rdx, printf3C2
mov rcx, printf3C3
mov r8, printf3C4
mov r9, printf3C5
push printf3C8
push printf3Str1
push printf3C7
push printf3C6
call printf
add rsp, SIZE_QWORD * 4
; TEST 4
lea rdi, [rel printf4]
call print
lea rdi, [rel printf4Str]
mov rsi, -50
mov rdx, -50
mov rcx, -50
mov r8, 0xFFFFFFFFFFFFFFFF
mov r9, 0xFFFFFFFFFFFFFFFF
push 0xFFFFFFFFFFFFFFFF
sub rsp, SIZE_QWORD
call printf
add rsp, SIZE_QWORD
; TEST 5
lea rdi, [rel printf5]
call print
lea rdi, [rel printf5Str]
mov rsi, 0xabcdeffedcba
mov rdx, 0x069bc0e
mov rcx, 666
mov r8, -1
call printf
%endif
;---
;--- strlen()
;---
%if TEST_strlen
; TEST 1
lea rdi, [rel strlenStr1]
call strlen
; TEST 2
lea rdi, [rel strlenStr2]
call strlen
%endif
;---
;--- strcpy()
;---
%if TEST_strcpy
lea rdi, [rel msgStrcpy]
call print
; TEST 1
lea rdi, [rel strcpy1]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
mov rdx, 12
call strcpy
mov rdi, rax
call puts
; TEST 2
lea rdi, [rel strcpy2]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
mov rdx, 13
call strcpy
mov rdi, rax
call puts
; TEST 3
lea rdi, [rel strcpy3]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
mov rdx, 32
call strcpy
mov rdi, rax
call puts
; TEST 4
lea rdi, [rel strcpy4]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str5]
mov rdx, 32
call strcpy
mov rdi, rax
call puts
%endif
;---
;--- strcat
;---
%if TEST_strcat
lea rdi, [rel msgStrcat]
call print
; TEST 1
lea rdi, [rel strcat1]
call print
lea rdi, [rel strBuff2]
lea rsi, [rel str1]
mov rcx, 32
call strcat
mov rdi, rax
call puts
; TEST 3
lea rdi, [rel strcat2]
call print
lea rdi, [rel strBuff2]
lea rsi, [rel str5]
mov rcx, 32
call strcat
mov rdi, rax
call puts
%endif
;---
;--- strclr
;---
%if TEST_strclr
lea rdi, [rel msgStrclr]
call print
; TEST 1
lea rdi, [rel strclr1a]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str5]
mov rdx, 32
call strcat
lea rdi, [rel strBuff1]
mov rsi, 32
call strclr
lea rdi, [rel strBuff1]
call puts
lea rdi, [rel strclr1b]
call print
lea rdi, [rel strBuff1]
add rdi, 3
call puts
; TEST 2
lea rdi, [rel strclr2]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str5]
mov rdx, 32
call strcat
lea rdi, [rel strBuff1]
mov rsi, 2
call strclr
lea rdi, [rel strBuff1]
add rdi, 3
call puts
%endif
;---
;--- exit()
;---
leave
mov rdi, 0
call exit