Add number feature #5
+1
-65
@@ -57,7 +57,7 @@ cat_file:
|
||||
la a2, mod_buf_0 # load mod_buf_0 as write buf
|
||||
2:
|
||||
|
||||
call _add_endings
|
||||
call add_endings
|
||||
xori s1, s1, 1 # flip the mod buf to write to
|
||||
mv s2, a1 # update the current byte count
|
||||
.LNoEndings:
|
||||
@@ -109,67 +109,3 @@ open_fd:
|
||||
|
||||
2:
|
||||
ret
|
||||
|
||||
# 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.
|
||||
# @param[out] a1: bytes written to mod buf.
|
||||
_add_endings:
|
||||
addi sp, sp, -72
|
||||
sd s7, 64(sp)
|
||||
sd s6, 56(sp)
|
||||
sd s5, 48(sp)
|
||||
sd s4, 40(sp)
|
||||
sd ra, 32(sp)
|
||||
sd s3, 24(sp)
|
||||
sd s2, 16(sp)
|
||||
sd s1, 8(sp)
|
||||
sd s0, 0(sp)
|
||||
|
||||
|
||||
mv s0, a0 # put the cat_buf ptr in s0
|
||||
mv s1, a1
|
||||
li s2, 0 # set the cat_buf char counter to 0
|
||||
li s3, 0 # set the endings_buf char counter to 0
|
||||
|
||||
mv s5, a2
|
||||
.LStdoutEndingsProcessChunk:
|
||||
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 # s4 = endings_buf[s2]
|
||||
li t5, '$'
|
||||
sb t5, 0(s4) # endings_buf[s2] = $
|
||||
li t5, '\n'
|
||||
addi s4, s4, 1
|
||||
sb t5, 0(s4) # endings_buf[s2] = \n
|
||||
addi s3, s3, 2
|
||||
j 2f
|
||||
|
||||
1:
|
||||
add s4, s5, s3 # s4 = endings_buf[s2]
|
||||
sb t6, 0(s4) # endings_buf[s2] = t6
|
||||
|
||||
addi s3, s3, 1 # inc endings_buf pointer
|
||||
2:
|
||||
addi s2, s2, 1 # inc cat_buf pointer
|
||||
blt s2, s1, .LStdoutEndingsProcessChunk
|
||||
|
||||
li a0, 0
|
||||
mv a1, s3
|
||||
|
||||
ld s0, 0(sp)
|
||||
ld s1, 8(sp)
|
||||
ld s2, 16(sp)
|
||||
ld s3, 24(sp)
|
||||
ld ra, 32(sp)
|
||||
ld s4, 40(sp)
|
||||
ld s5, 48(sp)
|
||||
ld s6, 56(sp)
|
||||
ld s7, 64(sp)
|
||||
addi sp, sp, 72
|
||||
ret
|
||||
+22
-2
@@ -1,6 +1,7 @@
|
||||
.data
|
||||
argc: .space 8
|
||||
show_ends: .space 1
|
||||
number: .space 1
|
||||
|
||||
cat_prefix: .ascii "cat: "
|
||||
.equ CAT_PREFIX_LEN, . - cat_prefix
|
||||
@@ -13,13 +14,14 @@ help:
|
||||
.ascii "Concatenate FILE(S) to standard output.\n\n"
|
||||
.ascii "With no FILE, or when FILE is -, read standard input.\n\n"
|
||||
.ascii " -E, --show-ends display $ at end of each line\n"
|
||||
.ascii " -n, --number number all output lines\n"
|
||||
.ascii " --help display this help and exit\n"
|
||||
.ascii " --version output version information and exit\n\n"
|
||||
.asciz "Git repository: <https://git.lexza.ch/Lexzach/riscv-cat>\n"
|
||||
.equ HELP_LEN, . - help
|
||||
|
||||
version:
|
||||
.ascii "cat 1.2.0\n"
|
||||
.ascii "cat 1.3.0\n"
|
||||
.ascii "Copyright (C) 2026 lexzach\n"
|
||||
.ascii "License MIT\n"
|
||||
.ascii "This is free software: you are free to change and redistribute it.\n"
|
||||
@@ -31,9 +33,11 @@ flag_help: .asciz "--help"
|
||||
flag_version: .asciz "--version"
|
||||
flag_show_ends: .asciz "--show-ends"
|
||||
flag_E: .asciz "-E"
|
||||
flag_number: .asciz "--number"
|
||||
flag_n: .asciz "-n"
|
||||
|
||||
.section .text
|
||||
.globl _start, show_ends
|
||||
.globl _start, show_ends, number
|
||||
|
||||
# main.
|
||||
_start:
|
||||
@@ -150,6 +154,12 @@ _check_flags:
|
||||
bnez a0, .LMainReturnErr
|
||||
sb a1, show_ends, t1 # since the return for has switch is either 0 for not found or 1 for found, we can just write it directly into the buf
|
||||
|
||||
la a0, flag_number
|
||||
la a1, flag_n
|
||||
call has_switch
|
||||
bnez a0, .LMainReturnErr
|
||||
sb a1, number, t1
|
||||
|
||||
ld ra, 0(sp)
|
||||
addi sp, sp, 8
|
||||
ret
|
||||
@@ -185,6 +195,16 @@ _is_known_flag:
|
||||
call str_cmp
|
||||
bnez a1, .LFlagFound
|
||||
|
||||
la a0, flag_number
|
||||
mv a1, s0
|
||||
call str_cmp
|
||||
bnez a1, .LFlagFound
|
||||
|
||||
la a0, flag_n
|
||||
mv a1, s0
|
||||
call str_cmp
|
||||
bnez a1, .LFlagFound
|
||||
|
||||
li a1, 0
|
||||
.LFlagFound:
|
||||
ld s0, 0(sp)
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
.data
|
||||
line_count: .space 8
|
||||
|
||||
.section .text
|
||||
.globl add_endings, add_line_numbers
|
||||
|
||||
# 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 addr.
|
||||
# @param[out] a0: result code.
|
||||
# @param[out] a1: bytes written to mod buf.
|
||||
add_endings:
|
||||
addi sp, sp, -72
|
||||
sd s7, 64(sp)
|
||||
sd s6, 56(sp)
|
||||
sd s5, 48(sp)
|
||||
sd s4, 40(sp)
|
||||
sd ra, 32(sp)
|
||||
sd s3, 24(sp)
|
||||
sd s2, 16(sp)
|
||||
sd s1, 8(sp)
|
||||
sd s0, 0(sp)
|
||||
|
||||
|
||||
mv s0, a0 # put the str buf 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 s5, a2
|
||||
.LEndingsProcessChunk:
|
||||
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 # s4 = mod buf[s2]
|
||||
li t5, '$'
|
||||
sb t5, 0(s4) # mod buf[s2] = $
|
||||
li t5, '\n'
|
||||
addi s4, s4, 1
|
||||
sb t5, 0(s4) # mod buf[s2] = \n
|
||||
addi s3, s3, 2
|
||||
j 2f
|
||||
|
||||
1:
|
||||
add s4, s5, s3 # s4 = mod buf[s2]
|
||||
sb t6, 0(s4) # mod buf[s2] = t6
|
||||
|
||||
addi s3, s3, 1 # inc mod buf pointer
|
||||
2:
|
||||
addi s2, s2, 1 # inc str buf pointer
|
||||
blt s2, s1, .LEndingsProcessChunk
|
||||
|
||||
li a0, 0
|
||||
mv a1, s3
|
||||
|
||||
ld s0, 0(sp)
|
||||
ld s1, 8(sp)
|
||||
ld s2, 16(sp)
|
||||
ld s3, 24(sp)
|
||||
ld ra, 32(sp)
|
||||
ld s4, 40(sp)
|
||||
ld s5, 48(sp)
|
||||
ld s6, 56(sp)
|
||||
ld s7, 64(sp)
|
||||
addi sp, sp, 72
|
||||
ret
|
||||
|
||||
# Prefix newlines with ######+TAB line count
|
||||
# @param[in] a0: ptr to null-terminated str.
|
||||
# @param[in] a1: length of str.
|
||||
# @param[in] a2: mod buf addr.
|
||||
# @param[in] a3: skip empty.
|
||||
# @param[out] a0: result code.
|
||||
# @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 ra, 32(sp)
|
||||
sd s3, 24(sp)
|
||||
sd s2, 16(sp)
|
||||
sd s1, 8(sp)
|
||||
sd s0, 0(sp)
|
||||
|
||||
|
||||
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 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
|
||||
addi s3, s3, 1
|
||||
j 2f
|
||||
|
||||
1:
|
||||
add s4, s5, s3 # s4 = mod buf[s2]
|
||||
sb t6, 0(s4) # mod buf[s2] = t6
|
||||
|
||||
addi s3, s3, 1 # inc mod buf pointer
|
||||
2:
|
||||
addi s2, s2, 1 # inc str buf pointer
|
||||
blt s2, s1, .LLineNumbersProcessChunk
|
||||
|
||||
li a0, 0
|
||||
mv a1, s3
|
||||
|
||||
ld s0, 0(sp)
|
||||
ld s1, 8(sp)
|
||||
ld s2, 16(sp)
|
||||
ld s3, 24(sp)
|
||||
ld ra, 32(sp)
|
||||
ld s4, 40(sp)
|
||||
ld s5, 48(sp)
|
||||
ld s6, 56(sp)
|
||||
ld s7, 64(sp)
|
||||
addi sp, sp, 72
|
||||
ret
|
||||
|
||||
Reference in New Issue
Block a user