Add file not found err

This commit is contained in:
2026-09-12 17:52:14 -04:00
parent 8cf0d06780
commit fbd1791c19
3 changed files with 61 additions and 10 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
.data .data
sp_start: .space 8 sp_start: .space 8
flag_h: .asciz "-h"
flag_helpo: .asciz "--help"
.section .text .section .text
.globl save_sp, fetch_argc, fetch_argv .globl save_sp, fetch_argc, fetch_argv
+38 -7
View File
@@ -2,7 +2,7 @@
cat_buf: .space 4096 cat_buf: .space 4096
.section .text .section .text
.globl cat_file .globl cat_file, open_fd
# Fetch argument count # Fetch argument count
# @param[in] a0: ptr to null-terminated file str. # @param[in] a0: ptr to null-terminated file str.
@@ -10,12 +10,17 @@ cat_buf: .space 4096
cat_file: cat_file:
mv a1, a0 # since the function takes in a0, move it to a1 for syscall mv a1, a0 # since the function takes in a0, move it to a1 for syscall
li a0, -100 # relative/absolute path addi sp, sp, -8
li a2, 0 # readonly sd ra, 0(sp)
li a7, 56 # open call open_fd
ecall ld ra, 0(sp)
addi sp, sp, 8
mv t0, a1 # put fd in t0
beqz a0, 1f # if we don't have an error, jump to reading
li a0, 1
j 2f
mv t0, a0 # put fd in t0
1: 1:
mv a0, t0 mv a0, t0
@@ -23,7 +28,7 @@ cat_file:
li a2, 4096 li a2, 4096
li a7, 63 li a7, 63
ecall ecall
blez a0, 2f blez a0, 3f
mv a2, a0 # move bytes read into bytes needed to write mv a2, a0 # move bytes read into bytes needed to write
li a0, 1 li a0, 1
@@ -32,7 +37,33 @@ cat_file:
ecall ecall
j 1b j 1b
3:
mv a0, t0
li a7, 57 # close fd
ecall
2: 2:
ret ret
# Fetch argument count
# @param[in] a0: ptr to null-terminated file str.
# @param[out] a0: result code. (0-exists 1-doesn't exist)
# @param[out] a1: fd.
open_fd:
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
li a0, -100 # relative/absolute path
li a2, 0 # readonly
li a7, 56 # open
ecall
bltz a0, 1f # file doesn't exist
mv a1, a0 # move fd to a1
li a0, 0
j 2f
1:
li a0, 1
2:
ret
+20 -2
View File
@@ -1,5 +1,8 @@
.data .data
argc: .space 8 argc: .space 8
file_err: .asciz "\nfile not found: "
.equ FILE_ERR_LEN, . - file_err
.section .text .section .text
.globl _start .globl _start
@@ -24,11 +27,26 @@ _start:
bnez a0, 1f # error if failed bnez a0, 1f # error if failed
mv a0, a1 mv a0, a1
call cat_file call cat_file
bnez a0, 1f # error if failed bnez a0, 4f # error if failed
addi s1, s1, 1 addi s1, s1, 1
blt s1, s0, 3b blt s1, s0, 3b
j 2f j 2f
4:
li a0, 1
la a1, file_err
li a2, FILE_ERR_LEN
li a7, 64
ecall
li t0, 1
mv a0, s1
call fetch_argv
li a0, 1
li a7, 64
ecall
1: 1:
li a0, 1 li a0, 1
li a7, 93 li a7, 93