From f4ccd72b4a5d025ddc88cb349e5942d4f7ee3635 Mon Sep 17 00:00:00 2001 From: lexzach Date: Tue, 15 Sep 2026 11:28:07 -0400 Subject: [PATCH] Complete line numbers --- src/cat.S | 40 ++++++++++++++++++++-------------------- src/mod.S | 24 +++++++++++++++++++++++- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/cat.S b/src/cat.S index 99a25d3..1de6eb3 100644 --- a/src/cat.S +++ b/src/cat.S @@ -42,26 +42,6 @@ cat_file: mv s2, a0 li s1, 1 - 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: - la t1, number lb t1, 0(t1) beqz t1, .LNoNumbers @@ -82,6 +62,26 @@ cat_file: mv s2, a1 # update the current byte count .LNoNumbers: + 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 diff --git a/src/mod.S b/src/mod.S index 19fa675..cfb129c 100644 --- a/src/mod.S +++ b/src/mod.S @@ -93,12 +93,34 @@ add_line_numbers: 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 + + # there is always one line number at the very start + call _inc_line_count + 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 + add s6, s5, s3 # update mod buf + offset addr + 4: + 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, 4b + li t5, 0x9 # tab character + sb t5, 0(s6) + addi s3, s3, 1 .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 + mv t0, s2 + addi t0, t0, 1 # move ahead one char and see if EOF + bge t0, s1, 1f # if the newline is at the veyr end, we don't indicate a new line number + call _inc_line_count @@ -121,7 +143,7 @@ add_line_numbers: add s6, s5, s3 # update mod buf + offset addr blt t2, t3, 3b - li t5, 0x7 # tab character + li t5, 0x9 # tab character sb t5, 0(s6) addi s3, s3, 1