From 201b37d9d125fff084339803d6d97ae284c5f11d Mon Sep 17 00:00:00 2001 From: lexzach Date: Tue, 15 Sep 2026 10:26:30 -0400 Subject: [PATCH] Work on inc line count function --- src/main.S | 2 ++ src/mod.S | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/main.S b/src/main.S index 5a8acac..13323f0 100644 --- a/src/main.S +++ b/src/main.S @@ -52,6 +52,8 @@ _start: li s1, 1 li s2, 0 # processed files + call zero_line_count + bnez a0, .LMainReturnErr call _check_flags li t0, 1 diff --git a/src/mod.S b/src/mod.S index 6ef9078..7d824b4 100644 --- a/src/mod.S +++ b/src/mod.S @@ -1,8 +1,8 @@ .data -line_count: .space 8 +line_count: .space 6 .section .text -.globl add_endings, add_line_numbers +.globl add_endings, add_line_numbers, zero_line_count # Print to stdout while adding "$" characters. # @param[in] a0: ptr to null-terminated str. @@ -131,4 +131,58 @@ add_line_numbers: ld s7, 64(sp) addi sp, sp, 72 ret - \ No newline at end of file + + +# Increment the line count string +# @param[in] a0: not used +# @param[out] a0: result code +_inc_line_count: + la t0, line_count # load the line count buffer address + li t1, 5 # current offset, we move from right to left first + + .LIncLine: + bltz t1, .LLineCountDoneInc + add t2, t0, t1 # load the correct offset address into t2 + lb t3, 0(t2) # load the current ascii character into t3 + + li t4, 32 # load space character + bne t3, t4, .LLineCountNotSpaceChar # if the character is not a space, skip setting to zero + li t4, 48 # ascii '0' + sb t4, 0(t2) # store this at the current pointer + + .LLineCountNotSpaceChar: + add t2, t0, t1 # load the correct offset address into t2 + lb t3, 0(t2) # load the current ascii character into t3 + li t4, 58 # ascii ':', right after ascii '9', overflow + + addi t3, t3, 1 + beq t3, t4, .LLineCountOverflow + sb t3, 0(t2) + j .LLineCountDoneInc + .LLineCountOverflow: + li t4, 48 # ascii '0' + sb t4, 0(t2) # store that ascii '0' + li t4, 1 + sub t1, t1, t4 # move to the next digit to the left + j .LIncLine + + .LLineCountDoneInc: + ret + +# Set the line count to all space characters +# @param[in] a0: not used +# @param[out] a0: result code +zero_line_count: + la t0, line_count # load the line count buffer address + li t1, 0 # char ptr + li t2, 5 # max char + li t3, 32 # space character + + 1: + add t4, t0, t1 # load offset addr + sb t3, 0(t4) # store the space char in offset addr + addi t1, t1, 1 + blt t1, t2, 1b + + li a0, 0 + ret