From fe856ff3d9acc638c5ecee30d64b70afe0d8f721 Mon Sep 17 00:00:00 2001 From: lexzach Date: Fri, 11 Sep 2026 22:27:24 -0400 Subject: [PATCH] Complete single file cat --- src/args.S | 8 ++++---- src/file.S | 38 ++++++++++++++++++++++++++++++++++++++ src/main.S | 19 +++++++++++-------- 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 src/file.S diff --git a/src/args.S b/src/args.S index a45f4a4..8264a4b 100644 --- a/src/args.S +++ b/src/args.S @@ -29,7 +29,7 @@ fetch_argv: addi sp, sp, -16 sd ra, 8(sp) sd s0, 0(sp) - call fetch_argc + call fetch_argc # get argument count ld ra, 8(sp) ld s0, 0(sp) addi sp, sp, 16 @@ -47,14 +47,14 @@ fetch_argv: sd ra, 16(sp) sd s1, 8(sp) sd s0, 0(sp) - call _next_argv + call _next_argv # grab the next argument ld ra, 16(sp) ld s1, 8(sp) ld s0, 0(sp) addi sp, sp, 24 mv a0, a1 addi s1, s1, 1 - bne s1, s0, 1b + bne s1, s0, 1b # if we haven't retrieved the requested argument, go again j 3f @@ -68,7 +68,7 @@ fetch_argv: sd s0, 0(sp) mv a0, a1 - call _argv_size + call _argv_size # grab the size of the retrieved argument ld ra, 8(sp) ld s0, 0(sp) diff --git a/src/file.S b/src/file.S new file mode 100644 index 0000000..8013a6b --- /dev/null +++ b/src/file.S @@ -0,0 +1,38 @@ +.data +cat_buf: .space 4096 + +.section .text +.globl cat_file + +# Fetch argument count +# @param[in] a0: ptr to null-terminated file str. +# @param[out] a0: result code. +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 + + mv t0, a0 # put fd in t0 + + 1: + mv a0, t0 + la a1, cat_buf + li a2, 4096 + li a7, 63 + ecall + blez a0, 2f + mv a2, a0 # move bytes read into bytes needed to write + + li a0, 1 + la a1, cat_buf + li a7, 64 + ecall + j 1b + + 2: + ret + + diff --git a/src/main.S b/src/main.S index 4a4718a..e1e7700 100644 --- a/src/main.S +++ b/src/main.S @@ -12,20 +12,23 @@ _start: call save_sp call fetch_argc - mv a0, a1 li t0, 1 - sub a0, a0, t0 + ble a1, t0, 1f # return 1 on no file given + + li a0, 1 call fetch_argv - beqz a0, 1f - li a0, 1 - li a7, 93 - ecall + beqz a0, 2f 1: li a0, 1 - li a7, 64 - ecall + li a7, 93 + ecall # return 1 + + 2: + mv a0, a1 + call cat_file + bnez a0, 1b # error if failed li a0, 0 li a7, 93