From ce8aa5cd862942fbf7cd4d7aaace28a2f5a537a2 Mon Sep 17 00:00:00 2001 From: lexzach Date: Tue, 15 Sep 2026 11:12:20 -0400 Subject: [PATCH] Progress on lc --- src/cat.S | 20 ++++++++++++++++++ src/mod.S | 62 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/cat.S b/src/cat.S index c2c599a..99a25d3 100644 --- a/src/cat.S +++ b/src/cat.S @@ -62,6 +62,26 @@ cat_file: mv s2, a1 # update the current byte count .LNoEndings: + la t1, number + lb t1, 0(t1) + beqz t1, .LNoNumbers + + 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_line_numbers + xori s1, s1, 1 # flip the mod buf to write to + mv s2, a1 # update the current byte count + .LNoNumbers: + beqz s1, 1f la a1, mod_buf_0 j 2f diff --git a/src/mod.S b/src/mod.S index 7d824b4..19fa675 100644 --- a/src/mod.S +++ b/src/mod.S @@ -77,40 +77,60 @@ add_endings: # @param[out] a1: bytes written to mod buf. add_line_numbers: addi sp, sp, -72 - sd s7, 64(sp) - sd s6, 56(sp) - sd s5, 48(sp) - sd s4, 40(sp) + sd s7, 64(sp) #unused + sd s6, 56(sp) # mod buf ptr + sd s5, 48(sp) # mod buf bp + sd s4, 40(sp) # str buf ptr sd ra, 32(sp) - sd s3, 24(sp) - sd s2, 16(sp) - sd s1, 8(sp) - sd s0, 0(sp) + sd s3, 24(sp) # mod buf offset + sd s2, 16(sp) # str buf offset + sd s1, 8(sp) # len of str + sd s0, 0(sp) # str buf addr mv s0, a0 # put the str ptr in s0 - mv s1, a1 - li s2, 0 # set the str buf char counter to 0 - li s3, 0 # set the mod buf char counter to 0 + mv s1, a1 # move length of str into s1 + li s2, 0 # set the str buf char offset to 0 + li s3, 0 # set the mod buf char offset to 0 + mv s5, a2 # move mod buf addr into s5 - mv s5, a2 .LLineNumbersProcessChunk: add s4, s0, s2 # Calculate the offset address lb t6, 0(s4) li t4, 0xA # newline bne t6, t4, 1f # if the current byte is not a newline, skip to writing - - add s4, s5, s3 # move modbuf pointer - li t5, '\n' - sb t5, 0(s4) # mod buf[s2] = \n - - addi s4, s4, 1 + + call _inc_line_count + + addi s3, s3, 1 # we want the ptr at the spot right after the newline + add s6, s5, s3 # update mod buf + offset addr + + li t2, 0 # lc char buf offset + li t3, 6 # lc char buf max offset + la t1, line_count # load the line count buffer addr + li t5, 0xA # newline character + sb t5, 0(s6) addi s3, s3, 1 + add s6, s5, s3 # update mod buf + offset addr + 3: + add t4, t1, t2 + lb t5, 0(t4) + sb t5, 0(s6) # mod buf[s4] = t4 + addi t2, t2, 1 + addi s3, s3, 1 + add s6, s5, s3 # update mod buf + offset addr + blt t2, t3, 3b + + li t5, 0x7 # tab character + sb t5, 0(s6) + + addi s3, s3, 1 + j 2f 1: - add s4, s5, s3 # s4 = mod buf[s2] - sb t6, 0(s4) # mod buf[s2] = t6 + add s6, s5, s3 # s4 = mod buf[s2] + sb t6, 0(s6) # mod buf[s2] = t6 addi s3, s3, 1 # inc mod buf pointer 2: @@ -175,7 +195,7 @@ _inc_line_count: zero_line_count: la t0, line_count # load the line count buffer address li t1, 0 # char ptr - li t2, 5 # max char + li t2, 6 # max char li t3, 32 # space character 1: