From 91118b185b075b8187908fe9d7ab331ee817ab83 Mon Sep 17 00:00:00 2001 From: lexzach Date: Mon, 14 Sep 2026 20:39:38 -0400 Subject: [PATCH] Move file cat over to new modbuf --- src/file.S | 97 ++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/src/file.S b/src/file.S index b8da07d..b6aec9d 100644 --- a/src/file.S +++ b/src/file.S @@ -1,6 +1,8 @@ .data cat_buf: .space 4096 endings_buf: .space 4097 +mod_buf_0: .space 36864 +mod_buf_1: .space 36864 .section .text .globl cat_file, open_fd, cat_stdin @@ -9,45 +11,62 @@ endings_buf: .space 4097 # @param[in] a0: ptr to null-terminated file str. # @param[out] a0: result code. cat_file: - addi sp, sp, -24 - sd s0, 0(sp) + addi sp, sp, -32 + sd s0, 0(sp) sd ra, 8(sp) - sd s1, 16(sp) + sd s1, 16(sp) # next modbuf to WRITE TO (inverse is read) 0=modbuf_0, 1=modbuf_1 + sd s2, 24(sp) # current modbuf byte count mv a1, a0 # since the function takes in a0, move it to a1 for syscall + li s1, 1 + li s2, 0 - addi sp, sp, -8 - sd ra, 0(sp) call open_fd - ld ra, 0(sp) - addi sp, sp, 8 mv s0, a1 # put fd in s0 - la t1, show_ends - lb s1, 0(t1) # store if we show ends into s1 beqz a0, .LReadFileBlock # if we don't have an error, jump to reading li a0, 1 j .LCatFileRet - .LReadFileBlock: mv a0, s0 - la a1, cat_buf + la a1, mod_buf_0 li a2, 4096 li a7, 63 ecall blez a0, .LCloseFD # if we haven't read any bytes, EOF, close FD - beqz s1, .LNoEndingsWrite - mv a1, a0 - la a0, cat_buf - call _stdout_add_endings - j .LReadFileBlock + mv s2, a0 - .LNoEndingsWrite: - mv a2, a0 # load the amount of bytes read in previous step into bytes needed to write + la t1, show_ends + lb t1, 0(t1) + beqz t1, .LNoEndings + + mv a1, s2 # load mod buf size to a1 + + beqz s1, 1f + la a0, mod_buf_0 # load mod_buf_0 as read buf + la a2, mod_buf_1 # load mod_buf_1 as write buf + j 2f + 1: + la a0, mod_buf_1 # load mod_buf_1 as read buf + la a2, mod_buf_0 # load mod_buf_0 as write buf + 2: + + call _add_endings + xori s1, s1, 1 # flip the mod buf to write to + mv s2, a1 # update the current byte count + .LNoEndings: + + beqz s1, 1f + la a1, mod_buf_0 + j 2f + 1: + la a1, mod_buf_1 + 2: + + mv a2, s2 # load the amount of bytes read in previous step into bytes needed to write li a0, 1 - la a1, cat_buf li a7, 64 ecall j .LReadFileBlock @@ -61,7 +80,8 @@ cat_file: ld s0, 0(sp) ld ra, 8(sp) ld s1, 16(sp) - addi sp, sp, 24 + ld s2, 24(sp) + addi sp, sp, 32 ret # Cat out stdin to stdout @@ -86,7 +106,7 @@ cat_stdin: beqz s0, .LNoEndingsStdinWrite mv a1, a0 la a0, cat_buf - call _stdout_add_endings + call _add_endings j .LReadStdinBlock .LNoEndingsStdinWrite: @@ -128,8 +148,10 @@ open_fd: # Print to stdout while adding "$" characters. # @param[in] a0: ptr to null-terminated str. # @param[in] a1: length of str. +# @param[in] a2: mod buf. # @param[out] a0: result code. -_stdout_add_endings: +# @param[out] a1: bytes written to mod buf. +_add_endings: addi sp, sp, -72 sd s7, 64(sp) sd s6, 56(sp) @@ -147,7 +169,7 @@ _stdout_add_endings: li s2, 0 # set the cat_buf char counter to 0 li s3, 0 # set the endings_buf char counter to 0 - la s5, endings_buf + mv s5, a2 .LStdoutEndingsProcessChunk: add s4, s0, s2 # Calculate the offset address lb t6, 0(s4) @@ -161,14 +183,6 @@ _stdout_add_endings: addi s4, s4, 1 sb t5, 0(s4) # endings_buf[s2] = \n addi s3, s3, 2 - - li a0, 1 - la a1, endings_buf - mv a2, s3 - li a7, 64 - ecall - call _zero_endings_buf - li s3, 0 # reset endings_buf char counter j 2f 1: @@ -179,15 +193,9 @@ _stdout_add_endings: 2: addi s2, s2, 1 # inc cat_buf pointer blt s2, s1, .LStdoutEndingsProcessChunk - beqz s3, .LSkipEndingsPrint - li a0, 1 - la a1, endings_buf - mv a2, s3 - li a7, 64 - ecall - .LSkipEndingsPrint: li a0, 0 + mv a1, s3 ld s0, 0(sp) ld s1, 8(sp) @@ -200,16 +208,3 @@ _stdout_add_endings: ld s7, 64(sp) addi sp, sp, 72 ret - -_zero_endings_buf: - li t0, 0 - li t1, 4097 - - .LZeroEndingsBufByte: - la t2, endings_buf - add t2, t2, t0 - sb x0, 0(t2) - addi t0, t0, 1 - blt t0, t1, .LZeroEndingsBufByte - - ret