Compare commits

..
7 Commits
4 changed files with 95 additions and 25 deletions
+2
View File
@@ -16,6 +16,8 @@ Run the `make` command in the root of the repository.
Options: Options:
`-b, --number-nonblank` - number nonempty output lines, overrides -n
`-E, --show-ends` - display $ at end of each line\ `-E, --show-ends` - display $ at end of each line\
`-n, --number` - number all output lines
`--help` - Show the help page.\ `--help` - Show the help page.\
`--version` - Show the version page. `--version` - Show the version page.
+25 -4
View File
@@ -42,9 +42,9 @@ cat_file:
mv s2, a0 mv s2, a0
li s1, 1 li s1, 1
la t1, show_ends la t1, number_nonblank
lb t1, 0(t1) lb t1, 0(t1)
beqz t1, .LNoEndings beqz t1, .LNoNonblankNumbers
mv a1, s2 # load mod buf size to a1 mv a1, s2 # load mod buf size to a1
@@ -57,10 +57,11 @@ cat_file:
la a2, mod_buf_0 # load mod_buf_0 as write buf la a2, mod_buf_0 # load mod_buf_0 as write buf
2: 2:
call add_endings call add_line_numbers
xori s1, s1, 1 # flip the mod buf to write to xori s1, s1, 1 # flip the mod buf to write to
mv s2, a1 # update the current byte count mv s2, a1 # update the current byte count
.LNoEndings: j .LNoNumbers # -n and -b are exclusive
.LNoNonblankNumbers:
la t1, number la t1, number
lb t1, 0(t1) lb t1, 0(t1)
@@ -82,6 +83,26 @@ cat_file:
mv s2, a1 # update the current byte count mv s2, a1 # update the current byte count
.LNoNumbers: .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 beqz s1, 1f
la a1, mod_buf_0 la a1, mod_buf_0
j 2f j 2f
+29 -9
View File
@@ -1,7 +1,8 @@
.data .data
argc: .space 8 argc: .space 8
show_ends: .space 1 show_ends: .space 1
number: .space 1 number: .space 1
number_nonblank: .space 1
cat_prefix: .ascii "cat: " cat_prefix: .ascii "cat: "
.equ CAT_PREFIX_LEN, . - cat_prefix .equ CAT_PREFIX_LEN, . - cat_prefix
@@ -13,15 +14,16 @@ help:
.ascii "Usage: cat [OPTION]... [FILE]...\n" .ascii "Usage: cat [OPTION]... [FILE]...\n"
.ascii "Concatenate FILE(S) to standard output.\n\n" .ascii "Concatenate FILE(S) to standard output.\n\n"
.ascii "With no FILE, or when FILE is -, read standard input.\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 " -b, --number-nonblank number nonempty output lines, overrides -n\n"
.ascii " -n, --number number all output lines\n" .ascii " -E, --show-ends display $ at end of each line\n"
.ascii " --help display this help and exit\n" .ascii " -n, --number number all output lines\n"
.ascii " --version output version information and exit\n\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" .asciz "Git repository: <https://git.lexza.ch/Lexzach/riscv-cat>\n"
.equ HELP_LEN, . - help .equ HELP_LEN, . - help
version: version:
.ascii "cat 1.3.0\n" .ascii "cat 1.4.0\n"
.ascii "Copyright (C) 2026 lexzach\n" .ascii "Copyright (C) 2026 lexzach\n"
.ascii "License MIT\n" .ascii "License MIT\n"
.ascii "This is free software: you are free to change and redistribute it.\n" .ascii "This is free software: you are free to change and redistribute it.\n"
@@ -35,9 +37,11 @@ flag_show_ends: .asciz "--show-ends"
flag_E: .asciz "-E" flag_E: .asciz "-E"
flag_number: .asciz "--number" flag_number: .asciz "--number"
flag_n: .asciz "-n" flag_n: .asciz "-n"
flag_number_nonblank: .asciz "--number-nonblank"
flag_b: .asciz "-b"
.section .text .section .text
.globl _start, show_ends, number .globl _start, show_ends, number, number_nonblank
# main. # main.
_start: _start:
@@ -162,6 +166,12 @@ _check_flags:
bnez a0, .LMainReturnErr bnez a0, .LMainReturnErr
sb a1, number, t1 sb a1, number, t1
la a0, flag_number_nonblank
la a1, flag_b
call has_switch
bnez a0, .LMainReturnErr
sb a1, number_nonblank, t1
ld ra, 0(sp) ld ra, 0(sp)
addi sp, sp, 8 addi sp, sp, 8
ret ret
@@ -207,6 +217,16 @@ _is_known_flag:
call str_cmp call str_cmp
bnez a1, .LFlagFound bnez a1, .LFlagFound
la a0, flag_number_nonblank
mv a1, s0
call str_cmp
bnez a1, .LFlagFound
la a0, flag_b
mv a1, s0
call str_cmp
bnez a1, .LFlagFound
li a1, 0 li a1, 0
.LFlagFound: .LFlagFound:
ld s0, 0(sp) ld s0, 0(sp)
+38 -11
View File
@@ -11,9 +11,7 @@ line_count: .space 6
# @param[out] a0: result code. # @param[out] a0: result code.
# @param[out] a1: bytes written to mod buf. # @param[out] a1: bytes written to mod buf.
add_endings: add_endings:
addi sp, sp, -72 addi sp, sp, -56
sd s7, 64(sp)
sd s6, 56(sp)
sd s5, 48(sp) sd s5, 48(sp)
sd s4, 40(sp) sd s4, 40(sp)
sd ra, 32(sp) sd ra, 32(sp)
@@ -63,21 +61,18 @@ add_endings:
ld ra, 32(sp) ld ra, 32(sp)
ld s4, 40(sp) ld s4, 40(sp)
ld s5, 48(sp) ld s5, 48(sp)
ld s6, 56(sp) addi sp, sp, 56
ld s7, 64(sp)
addi sp, sp, 72
ret ret
# Prefix newlines with ######+TAB line count # Prefix newlines with ######+TAB line count
# @param[in] a0: ptr to null-terminated str. # @param[in] a0: ptr to null-terminated str.
# @param[in] a1: length of str. # @param[in] a1: length of str.
# @param[in] a2: mod buf addr. # @param[in] a2: mod buf addr.
# @param[in] a3: skip empty.
# @param[out] a0: result code. # @param[out] a0: result code.
# @param[out] a1: bytes written to mod buf. # @param[out] a1: bytes written to mod buf.
add_line_numbers: add_line_numbers:
addi sp, sp, -72 addi sp, sp, -72
sd s7, 64(sp) #unused sd s7, 64(sp) # skip empty
sd s6, 56(sp) # mod buf ptr sd s6, 56(sp) # mod buf ptr
sd s5, 48(sp) # mod buf bp sd s5, 48(sp) # mod buf bp
sd s4, 40(sp) # str buf ptr sd s4, 40(sp) # str buf ptr
@@ -87,6 +82,8 @@ add_line_numbers:
sd s1, 8(sp) # len of str sd s1, 8(sp) # len of str
sd s0, 0(sp) # str buf addr sd s0, 0(sp) # str buf addr
la t0, number_nonblank
lb s7, 0(t0)
mv s0, a0 # put the str ptr in s0 mv s0, a0 # put the str ptr in s0
mv s1, a1 # move length of str into s1 mv s1, a1 # move length of str into s1
@@ -94,11 +91,41 @@ add_line_numbers:
li s3, 0 # set the mod 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 # 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: .LLineNumbersProcessChunk:
add s4, s0, s2 # Calculate the offset address add s4, s0, s2 # Calculate the offset address
lb t6, 0(s4) lb t6, 0(s4)
li t4, 0xA # newline li t4, 0xA # newline
bne t6, t4, 1f # if the current byte is not a newline, skip to writing 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 very end, we don't indicate a new line number
beqz s7, .LLineNumbersSkipEmptyCheck
mv t0, s4 # move the current string buf pointer into t0
addi t0, t0, 1 # move the str pointer forward one
lb t0, 0(t0)
# t4 still contains a newline char
beq t0, t4, 1f # if the next byte IS a newline, skip to writing
.LLineNumbersSkipEmptyCheck:
call _inc_line_count call _inc_line_count
@@ -121,7 +148,7 @@ add_line_numbers:
add s6, s5, s3 # update mod buf + offset addr add s6, s5, s3 # update mod buf + offset addr
blt t2, t3, 3b blt t2, t3, 3b
li t5, 0x7 # tab character li t5, 0x9 # tab character
sb t5, 0(s6) sb t5, 0(s6)
addi s3, s3, 1 addi s3, s3, 1
@@ -148,7 +175,7 @@ add_line_numbers:
ld s4, 40(sp) ld s4, 40(sp)
ld s5, 48(sp) ld s5, 48(sp)
ld s6, 56(sp) ld s6, 56(sp)
ld s7, 64(sp) ld s6, 64(sp)
addi sp, sp, 72 addi sp, sp, 72
ret ret