Add perror() (currently only errors 1-34 from asm-generic/errno-base.h)

This commit is contained in:
2025-06-26 09:29:54 +02:00
parent fc28643c1a
commit 232d50576f
2 changed files with 109 additions and 2 deletions

View File

@ -1,5 +1,3 @@
extern printf
;constants.asm
extern EOS
extern NL
@ -17,6 +15,8 @@ extern exit
;console.asm
extern print
extern puts
extern printf
extern perror
;string.asm
extern strlen
extern strcpy
@ -36,6 +36,7 @@ section .rodata
TEST_print equ 1
TEST_puts equ 1
TEST_printf equ 1
TEST_perror equ 1
TEST_min equ 1 ;includes minu
TEST_max equ 1 ;includes maxu
TEST_strlen equ 1
@ -85,6 +86,12 @@ section .rodata
msgPrintf1 db "TEST printf()",NL,EOS
msgPrintf2 db "TEST printf(testStr, tS1, tS2, tS3, invalid1, tS4, num4, tS5, tS6, invalid2, tS7, tS8, num1, tS9)",NL,EOS
msgPrintf2b db "Return value: %d",NL,EOS
; perror()
msgPerror db NL,"# perror()",EOS
msgPerror1 db "TEST perror(",34,"ErrTest",34,") (rax=0):",NL,EOS
msgPerror2 db "TEST perror(",34,"ErrTest",34,") (rax=-22):",NL,EOS
msgPerror3 db "TEST perror(",34,"ErrTest",34,") (rax=7):",NL,EOS
perrorStr db "ErrTest",EOS
; min() / minu()
msgMin db NL,"# min() / minu()",EOS
msgMin1 db "TEST min(num1, num2): %d",NL,EOS
@ -172,6 +179,9 @@ section .rodata
msgFileB1 db "TEST fopen('testFile1.txt', 'w'): %d",NL,EOS
msgFileB2 db "TEST fwrite(filePointer1, fileStr1): %d",NL,EOS
msgFileB3 db "TEST fclose(filePointer1): %d",NL,EOS
perror_fopen db " Error@fopen()",EOS
perror_fwrite db " Error@fwrite()",EOS
perror_fclose db " Error@fclose()",EOS
section .data
filePointer1 dq 0
section .bss
@ -248,6 +258,31 @@ _start:
lea rdi, [msgPrintf2b]
call printf
%ENDIF
%IF TEST_perror
lea rdi, [rel msgPerror]
call puts
; TEST: perror (no error)
lea rdi, [rel msgPerror1]
call print
xor rax, rax
lea rdi, [rel perrorStr]
call perror
; TEST: perror (err=-22)
lea rdi, [rel msgPerror2]
call print
mov rax, -22
lea rdi, [rel perrorStr]
call perror
; TEST: perror (err=7)
lea rdi, [rel msgPerror3]
call print
mov rax, 7
lea rdi, [rel perrorStr]
call perror
%ENDIF
%IF TEST_min
lea rdi, [rel msgMin]
call puts
@ -785,6 +820,10 @@ _start:
lea rdi, [rel testFile1]
mov rsi, 'w'
call fopen
%IF TEST_perror
lea rdi, [rel perror_fopen]
call perror
%ENDIf
mov r15, rax ;store filepointer to R15
mov rsi, rax
xor rax, rax
@ -795,6 +834,10 @@ _start:
mov rdi, r15
lea rsi, [fileStr1]
call fwrite
%IF TEST_perror
lea rdi, [rel perror_fwrite]
call perror
%ENDIF
mov rsi, rax
xor rax, rax
lea rdi, [rel msgFileB2]
@ -803,6 +846,10 @@ _start:
; TEST: fclose()
mov rdi, r15
call fclose
%IF TEST_perror
lea rdi, [rel perror_fclose]
call perror
%ENDIF
mov rsi, rax
xor rax, rax
lea rdi, [rel msgFileB3]