diff --git a/src/args.S b/src/args.S index cc465d9..9662678 100644 --- a/src/args.S +++ b/src/args.S @@ -1,5 +1,7 @@ .data -sp_start: .space 8 +sp_start: .space 8 +flag_h: .asciz "-h" +flag_helpo: .asciz "--help" .section .text .globl save_sp, fetch_argc, fetch_argv diff --git a/src/file.S b/src/file.S index 8013a6b..1820828 100644 --- a/src/file.S +++ b/src/file.S @@ -2,7 +2,7 @@ cat_buf: .space 4096 .section .text -.globl cat_file +.globl cat_file, open_fd # Fetch argument count # @param[in] a0: ptr to null-terminated file str. @@ -10,12 +10,17 @@ cat_buf: .space 4096 cat_file: 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 + addi sp, sp, -8 + sd ra, 0(sp) + call open_fd + 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: mv a0, t0 @@ -23,7 +28,7 @@ cat_file: li a2, 4096 li a7, 63 ecall - blez a0, 2f + blez a0, 3f mv a2, a0 # move bytes read into bytes needed to write li a0, 1 @@ -32,7 +37,33 @@ cat_file: ecall j 1b + 3: + mv a0, t0 + li a7, 57 # close fd + ecall + 2: 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 diff --git a/src/main.S b/src/main.S index 7357107..7b9fd87 100644 --- a/src/main.S +++ b/src/main.S @@ -1,5 +1,8 @@ .data -argc: .space 8 +argc: .space 8 +file_err: .asciz "\nfile not found: " + +.equ FILE_ERR_LEN, . - file_err .section .text .globl _start @@ -24,11 +27,26 @@ _start: bnez a0, 1f # error if failed mv a0, a1 call cat_file - bnez a0, 1f # error if failed + bnez a0, 4f # error if failed addi s1, s1, 1 blt s1, s0, 3b 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: li a0, 1 li a7, 93