Compare commits
22
Commits
77d4a59432
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c48659b96 | ||
|
|
1d5eed6f22
|
||
|
|
ca7daa1734
|
||
|
|
90b6c515ca
|
||
|
|
b5056b0fae
|
||
|
|
0f3beed6ee | ||
|
|
0e2f2ea6df | ||
|
|
f4ccd72b4a
|
||
|
|
ce8aa5cd86
|
||
|
|
201b37d9d1
|
||
|
|
d7beb7fcad
|
||
|
|
a39287e793 | ||
|
|
3eb098e84b
|
||
|
|
685a5ba81d
|
||
|
|
91118b185b
|
||
|
|
446e0b7019 | ||
|
|
43635ab659
|
||
|
|
cf66f92aae
|
||
|
|
71c9a4fb36
|
||
|
|
e4b88f7f9e | ||
|
|
b0a6a15f83 | ||
|
|
cec608f19c
|
@@ -16,4 +16,8 @@ Run the `make` command in the root of the repository.
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
`-h, --help` - Show a help page.
|
`-b, --number-nonblank` - number nonempty output lines, overrides -n
|
||||||
|
`-E, --show-ends` - display $ at end of each line\
|
||||||
|
`-n, --number` - number all output lines
|
||||||
|
`--help` - Show the help page.\
|
||||||
|
`--version` - Show the version page.
|
||||||
|
|||||||
+7
-5
@@ -2,7 +2,7 @@
|
|||||||
sp_start: .space 8
|
sp_start: .space 8
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl save_sp, fetch_argc, fetch_argv, has_switch
|
.globl save_sp, fetch_argc, fetch_argv, has_switch, str_cmp
|
||||||
|
|
||||||
save_sp:
|
save_sp:
|
||||||
sd sp, sp_start, t0 # save the stack pointer
|
sd sp, sp_start, t0 # save the stack pointer
|
||||||
@@ -94,7 +94,7 @@ fetch_argv:
|
|||||||
|
|
||||||
# Check if flag was given
|
# Check if flag was given
|
||||||
# @param[in] a0: ptr to null-terminated switch 1 (ie. -h).
|
# @param[in] a0: ptr to null-terminated switch 1 (ie. -h).
|
||||||
# @param[in] a1: ptr to null-terminated switch 2 (ie. --help) (optional).
|
# @param[in] a1: ptr to null-terminated switch 2 (ie. --help) (optional, pass 0 for none).
|
||||||
# @param[out] a0: result code.
|
# @param[out] a0: result code.
|
||||||
# @param[out] a1: was found (0-not found, 1-found).
|
# @param[out] a1: was found (0-not found, 1-found).
|
||||||
has_switch:
|
has_switch:
|
||||||
@@ -125,14 +125,16 @@ has_switch:
|
|||||||
|
|
||||||
mv a0, s3 # load argv buff addr into a0 (string 0)
|
mv a0, s3 # load argv buff addr into a0 (string 0)
|
||||||
mv a1, s0 # move switch ptr 1 addr into a1 (string 1)
|
mv a1, s0 # move switch ptr 1 addr into a1 (string 1)
|
||||||
call _str_cmp
|
call str_cmp
|
||||||
bnez a1, .LSwitchFound # if the strings match, exit loop
|
bnez a1, .LSwitchFound # if the strings match, exit loop
|
||||||
|
beqz s1, .LSkipSecondSwitch # if we have no second switch, skip the check
|
||||||
|
|
||||||
mv a0, s3 # move argv buff addr into a0 (string 0)
|
mv a0, s3 # move argv buff addr into a0 (string 0)
|
||||||
mv a1, s1 # move switch ptr 2 addr into a1 (string 1)
|
mv a1, s1 # move switch ptr 2 addr into a1 (string 1)
|
||||||
call _str_cmp
|
call str_cmp
|
||||||
bnez a1, .LSwitchFound # if the strings match, exit loop
|
bnez a1, .LSwitchFound # if the strings match, exit loop
|
||||||
|
|
||||||
|
.LSkipSecondSwitch:
|
||||||
addi s2, s2, 1 # if string doesn't match we inc to the next arg
|
addi s2, s2, 1 # if string doesn't match we inc to the next arg
|
||||||
blt s2, s4, .LSwitchProcessNextArg # keep going while we have untested args
|
blt s2, s4, .LSwitchProcessNextArg # keep going while we have untested args
|
||||||
|
|
||||||
@@ -168,7 +170,7 @@ has_switch:
|
|||||||
# @param[in] a1: str buffer 2
|
# @param[in] a1: str buffer 2
|
||||||
# @param[out] a0: result code.
|
# @param[out] a0: result code.
|
||||||
# @param[out] a1: is equal (0-no, 1-yes).
|
# @param[out] a1: is equal (0-no, 1-yes).
|
||||||
_str_cmp:
|
str_cmp:
|
||||||
li t2, 0
|
li t2, 0
|
||||||
|
|
||||||
.LByteCmp:
|
.LByteCmp:
|
||||||
|
|||||||
@@ -0,0 +1,152 @@
|
|||||||
|
.data
|
||||||
|
mod_buf_0: .space 36864
|
||||||
|
mod_buf_1: .space 36864
|
||||||
|
|
||||||
|
.section .text
|
||||||
|
.globl cat_file, open_fd
|
||||||
|
|
||||||
|
# Cat out the given file to stdout
|
||||||
|
# @param[in] a0: ptr to null-terminated file str. 0 = stdin
|
||||||
|
# @param[out] a0: result code.
|
||||||
|
cat_file:
|
||||||
|
addi sp, sp, -32
|
||||||
|
sd s0, 0(sp)
|
||||||
|
sd ra, 8(sp)
|
||||||
|
sd s1, 16(sp) # next modbuf to WRITE TO (inverse is read) 0=modbuf_0, 1=modbuf_1
|
||||||
|
sd s2, 24(sp) # current modbuf byte count
|
||||||
|
|
||||||
|
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
||||||
|
li s1, 1
|
||||||
|
li s2, 0
|
||||||
|
|
||||||
|
beqz a0, 1f
|
||||||
|
call open_fd
|
||||||
|
j 2f
|
||||||
|
1:
|
||||||
|
li s0, 0 # stdin fd
|
||||||
|
2:
|
||||||
|
|
||||||
|
mv s0, a1 # put fd in s0
|
||||||
|
|
||||||
|
beqz a0, .LReadFileBlock # if we don't have an error, jump to reading
|
||||||
|
li a0, 1
|
||||||
|
j .LCatFileRet
|
||||||
|
|
||||||
|
.LReadFileBlock:
|
||||||
|
mv a0, s0
|
||||||
|
la a1, mod_buf_0
|
||||||
|
li a2, 4096
|
||||||
|
li a7, 63
|
||||||
|
ecall
|
||||||
|
blez a0, .LCloseFD # if we haven't read any bytes, EOF, close FD
|
||||||
|
mv s2, a0
|
||||||
|
li s1, 1
|
||||||
|
|
||||||
|
la t1, number_nonblank
|
||||||
|
lb t1, 0(t1)
|
||||||
|
beqz t1, .LNoNonblankNumbers
|
||||||
|
|
||||||
|
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
|
||||||
|
j .LNoNumbers # -n and -b are exclusive
|
||||||
|
.LNoNonblankNumbers:
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
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
|
||||||
|
1:
|
||||||
|
la a1, mod_buf_1
|
||||||
|
2:
|
||||||
|
|
||||||
|
mv a2, s2 # load the amount of bytes read in previous step into bytes needed to write
|
||||||
|
li a0, 1
|
||||||
|
li a7, 64
|
||||||
|
ecall
|
||||||
|
j .LReadFileBlock
|
||||||
|
|
||||||
|
.LCloseFD:
|
||||||
|
mv a0, s0
|
||||||
|
li a7, 57 # close fd
|
||||||
|
ecall
|
||||||
|
|
||||||
|
.LCatFileRet:
|
||||||
|
ld s0, 0(sp)
|
||||||
|
ld ra, 8(sp)
|
||||||
|
ld s1, 16(sp)
|
||||||
|
ld s2, 24(sp)
|
||||||
|
addi sp, sp, 32
|
||||||
|
ret
|
||||||
|
|
||||||
|
# Open fd
|
||||||
|
# @param[in] a0: ptr to null-terminated file str.
|
||||||
|
# @param[out] a0: result code. (0-exists 1-doesn't exist)
|
||||||
|
# @param[out] a1: fd.
|
||||||
|
open_fd:
|
||||||
|
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
||||||
|
|
||||||
|
li a0, -100 # relative/absolute path
|
||||||
|
li a2, 0 # readonly
|
||||||
|
li a7, 56 # open
|
||||||
|
ecall
|
||||||
|
bltz a0, 1f # file doesn't exist
|
||||||
|
mv a1, a0 # move fd to a1
|
||||||
|
li a0, 0
|
||||||
|
j 2f
|
||||||
|
|
||||||
|
1:
|
||||||
|
li a0, 1
|
||||||
|
|
||||||
|
2:
|
||||||
|
ret
|
||||||
-107
@@ -1,107 +0,0 @@
|
|||||||
.data
|
|
||||||
cat_buf: .space 4096
|
|
||||||
|
|
||||||
.section .text
|
|
||||||
.globl cat_file, open_fd, cat_stdin
|
|
||||||
|
|
||||||
# Cat out the given file to stdout
|
|
||||||
# @param[in] a0: ptr to null-terminated file str.
|
|
||||||
# @param[out] a0: result code.
|
|
||||||
cat_file:
|
|
||||||
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
|
||||||
|
|
||||||
addi sp, sp, -8
|
|
||||||
sd ra, 0(sp)
|
|
||||||
call open_fd
|
|
||||||
ld ra, 0(sp)
|
|
||||||
addi sp, sp, 8
|
|
||||||
|
|
||||||
mv t0, a1 # put fd in t0
|
|
||||||
beqz a0, .LReadFileBlock # if we don't have an error, jump to reading
|
|
||||||
li a0, 1
|
|
||||||
j .LCatFileRet
|
|
||||||
|
|
||||||
|
|
||||||
.LReadFileBlock:
|
|
||||||
mv a0, t0
|
|
||||||
la a1, cat_buf
|
|
||||||
li a2, 4096
|
|
||||||
li a7, 63
|
|
||||||
ecall
|
|
||||||
blez a0, .LCloseFD # if we haven't read any bytes, EOF, close FD
|
|
||||||
mv a2, a0 # move bytes read into bytes needed to write
|
|
||||||
|
|
||||||
li a0, 1
|
|
||||||
la a1, cat_buf
|
|
||||||
li a7, 64
|
|
||||||
ecall
|
|
||||||
j .LReadFileBlock
|
|
||||||
|
|
||||||
.LCloseFD:
|
|
||||||
mv a0, t0
|
|
||||||
li a7, 57 # close fd
|
|
||||||
ecall
|
|
||||||
|
|
||||||
.LCatFileRet:
|
|
||||||
ret
|
|
||||||
|
|
||||||
# Cat out stdin to stdout
|
|
||||||
# @param[in] a0: Not used.
|
|
||||||
# @param[out] a0: result code.
|
|
||||||
cat_stdin:
|
|
||||||
.LReadStdinBlock:
|
|
||||||
li a0, 0
|
|
||||||
la a1, cat_buf
|
|
||||||
li a2, 4096
|
|
||||||
li a7, 63
|
|
||||||
ecall
|
|
||||||
blez a0, .LCatStdinRet # if we haven't read any bytes, EOF, close FD
|
|
||||||
mv a2, a0 # move bytes read into bytes needed to write
|
|
||||||
|
|
||||||
li a0, 1
|
|
||||||
la a1, cat_buf
|
|
||||||
li a7, 64
|
|
||||||
ecall
|
|
||||||
j .LReadStdinBlock
|
|
||||||
|
|
||||||
.LCatStdinRet:
|
|
||||||
ret
|
|
||||||
|
|
||||||
# Open fd
|
|
||||||
# @param[in] a0: ptr to null-terminated file str.
|
|
||||||
# @param[out] a0: result code. (0-exists 1-doesn't exist)
|
|
||||||
# @param[out] a1: fd.
|
|
||||||
open_fd:
|
|
||||||
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
|
||||||
|
|
||||||
li a0, -100 # relative/absolute path
|
|
||||||
li a2, 0 # readonly
|
|
||||||
li a7, 56 # open
|
|
||||||
ecall
|
|
||||||
bltz a0, 1f # file doesn't exist
|
|
||||||
mv a1, a0 # move fd to a1
|
|
||||||
li a0, 0
|
|
||||||
j 2f
|
|
||||||
|
|
||||||
1:
|
|
||||||
li a0, 1
|
|
||||||
|
|
||||||
2:
|
|
||||||
ret
|
|
||||||
|
|
||||||
# # Check if the given file exists without opening fd
|
|
||||||
# # @param[in] a0: ptr to null-terminated file str.
|
|
||||||
# # @param[out] a0: result code (0-exists 1-doesn't exist).
|
|
||||||
# file_exists:
|
|
||||||
# mv a1, a0
|
|
||||||
# li a0, -100 # relative/absolute path
|
|
||||||
# li a2, 0
|
|
||||||
# li a7, 48
|
|
||||||
# ecall
|
|
||||||
# beqz a0, 1f
|
|
||||||
# li a0, 1
|
|
||||||
# ret
|
|
||||||
|
|
||||||
# 1:
|
|
||||||
# li a0, 0
|
|
||||||
# ret
|
|
||||||
+154
-19
@@ -1,5 +1,8 @@
|
|||||||
.data
|
.data
|
||||||
argc: .space 8
|
argc: .space 8
|
||||||
|
show_ends: .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
|
||||||
@@ -11,17 +14,36 @@ 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 " -h, --help | Show this help page.\n\n"
|
.ascii " -b, --number-nonblank number nonempty output lines, overrides -n\n"
|
||||||
.asciz "Written by lexzach in RISC-V assembly: <https://git.lexza.ch/Lexzach/riscv-cat>\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
|
.equ HELP_LEN, . - help
|
||||||
|
|
||||||
flag_h: .asciz "-h"
|
version:
|
||||||
flag_help: .asciz "--help"
|
.ascii "cat 1.4.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"
|
||||||
|
.ascii "There is NO WARRANTY, to the extent permitted by law.\n\n"
|
||||||
|
.asciz "Written by lexzach in pure RISC-V assembly\n"
|
||||||
|
.equ VERSION_LEN, . - version
|
||||||
|
|
||||||
|
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"
|
||||||
|
flag_number_nonblank: .asciz "--number-nonblank"
|
||||||
|
flag_b: .asciz "-b"
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl _start
|
.globl _start, show_ends, number, number_nonblank
|
||||||
|
|
||||||
|
# main.
|
||||||
_start:
|
_start:
|
||||||
.option push
|
.option push
|
||||||
.option norelax
|
.option norelax
|
||||||
@@ -32,32 +54,38 @@ _start:
|
|||||||
call fetch_argc
|
call fetch_argc
|
||||||
mv s0, a1 # store argc
|
mv s0, a1 # store argc
|
||||||
li s1, 1
|
li s1, 1
|
||||||
|
li s2, 0 # processed files
|
||||||
la a0, flag_h
|
|
||||||
la a1, flag_help
|
|
||||||
call has_switch
|
|
||||||
bnez a0, .LMainReturnErr
|
|
||||||
beqz a1, .LSkipDispHelp
|
|
||||||
call _disp_help
|
|
||||||
j .LMainReturnOk
|
|
||||||
.LSkipDispHelp:
|
|
||||||
|
|
||||||
|
call zero_line_count
|
||||||
|
bnez a0, .LMainReturnErr
|
||||||
|
call _check_flags
|
||||||
|
|
||||||
li t0, 1
|
li t0, 1
|
||||||
ble s0, t0, .LProcessStdin # return 1 if no args
|
ble s0, t0, .LProcessStdin # use stdin if no commands, although this is checked later, if no flags are used, we can go ahead and jump
|
||||||
|
|
||||||
.LProcessFile:
|
.LProcessFile:
|
||||||
mv a0, s1
|
mv a0, s1
|
||||||
call fetch_argv
|
call fetch_argv
|
||||||
bnez a0, .LMainReturnErr # error if failed
|
bnez a0, .LMainReturnErr # error if failed
|
||||||
mv a0, a1
|
mv a0, a1
|
||||||
|
addi sp, sp, -8
|
||||||
|
sd a0, 0(sp)
|
||||||
|
call _is_known_flag
|
||||||
|
ld a0, 0(sp)
|
||||||
|
addi sp, sp, 8
|
||||||
|
bnez a1, .LSkipArg # check if arg is flag
|
||||||
call cat_file
|
call cat_file
|
||||||
|
addi s2, s2, 1
|
||||||
bnez a0, .LReturnFileErr # error if failed
|
bnez a0, .LReturnFileErr # error if failed
|
||||||
|
.LSkipArg:
|
||||||
addi s1, s1, 1
|
addi s1, s1, 1
|
||||||
blt s1, s0, .LProcessFile
|
blt s1, s0, .LProcessFile
|
||||||
|
beqz s2, .LProcessStdin # if we reach the end of the file queue, and no files have actually been processed, that means they were all flags, switch to stdin
|
||||||
j .LMainReturnOk
|
j .LMainReturnOk
|
||||||
|
|
||||||
.LProcessStdin:
|
.LProcessStdin:
|
||||||
call cat_stdin
|
li a0, 0
|
||||||
|
call cat_file
|
||||||
j .LMainReturnOk
|
j .LMainReturnOk
|
||||||
|
|
||||||
.LReturnFileErr:
|
.LReturnFileErr:
|
||||||
@@ -91,10 +119,117 @@ _start:
|
|||||||
li a7, 93
|
li a7, 93
|
||||||
ecall # return 0
|
ecall # return 0
|
||||||
|
|
||||||
_disp_help:
|
|
||||||
la a0, 1
|
# Check for flags, branch or set buffers as needed.
|
||||||
|
# @param[in] a0: None.
|
||||||
|
# @param[out] a0: None.
|
||||||
|
_check_flags:
|
||||||
|
addi sp, sp, -8
|
||||||
|
sd ra, 0(sp)
|
||||||
|
|
||||||
|
la a0, flag_help # help flag
|
||||||
|
li a1, 0 # no second flag
|
||||||
|
call has_switch
|
||||||
|
bnez a0, .LMainReturnErr
|
||||||
|
beqz a1, .LSkipDispHelp
|
||||||
|
li a0, 1
|
||||||
la a1, help
|
la a1, help
|
||||||
li a2, HELP_LEN
|
li a2, HELP_LEN
|
||||||
li a7, 64
|
li a7, 64
|
||||||
ecall
|
ecall
|
||||||
|
j .LMainReturnOk
|
||||||
|
|
||||||
|
.LSkipDispHelp:
|
||||||
|
|
||||||
|
la a0, flag_version # version flag
|
||||||
|
li a1, 0 # no second flag
|
||||||
|
call has_switch
|
||||||
|
bnez a0, .LMainReturnErr
|
||||||
|
beqz a1, .LSkipDispVersion
|
||||||
|
li a0, 1
|
||||||
|
la a1, version
|
||||||
|
li a2, VERSION_LEN
|
||||||
|
li a7, 64
|
||||||
|
ecall
|
||||||
|
j .LMainReturnOk
|
||||||
|
.LSkipDispVersion:
|
||||||
|
|
||||||
|
la a0, flag_show_ends # show ends flag
|
||||||
|
la a1, flag_E # show ends -E flag
|
||||||
|
call has_switch
|
||||||
|
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
|
||||||
|
|
||||||
|
la a0, flag_number_nonblank
|
||||||
|
la a1, flag_b
|
||||||
|
call has_switch
|
||||||
|
bnez a0, .LMainReturnErr
|
||||||
|
sb a1, number_nonblank, t1
|
||||||
|
|
||||||
|
ld ra, 0(sp)
|
||||||
|
addi sp, sp, 8
|
||||||
|
ret
|
||||||
|
|
||||||
|
# Check if given argv is a flag to determine if cat_file should be used
|
||||||
|
# @param[in] a0: ptr to null-terminated argv string.
|
||||||
|
# @param[out] a0: result code.
|
||||||
|
# @param[out] a1: found flag (0- not a flag, 1- flag).
|
||||||
|
_is_known_flag:
|
||||||
|
addi sp, sp, -16
|
||||||
|
sd ra, 8(sp)
|
||||||
|
sd s0, 0(sp)
|
||||||
|
|
||||||
|
mv s0, a0
|
||||||
|
|
||||||
|
la a0, flag_help
|
||||||
|
mv a1, s0
|
||||||
|
call str_cmp
|
||||||
|
bnez a1, .LFlagFound
|
||||||
|
|
||||||
|
la a0, flag_version
|
||||||
|
mv a1, s0
|
||||||
|
call str_cmp
|
||||||
|
bnez a1, .LFlagFound
|
||||||
|
|
||||||
|
la a0, flag_show_ends
|
||||||
|
mv a1, s0
|
||||||
|
call str_cmp
|
||||||
|
bnez a1, .LFlagFound
|
||||||
|
|
||||||
|
la a0, flag_E
|
||||||
|
mv a1, s0
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
.LFlagFound:
|
||||||
|
ld s0, 0(sp)
|
||||||
|
ld ra, 8(sp)
|
||||||
|
addi sp, sp, 16
|
||||||
ret
|
ret
|
||||||
|
|||||||
@@ -0,0 +1,235 @@
|
|||||||
|
.data
|
||||||
|
line_count: .space 6
|
||||||
|
|
||||||
|
.section .text
|
||||||
|
.globl add_endings, add_line_numbers, zero_line_count
|
||||||
|
|
||||||
|
# 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, -56
|
||||||
|
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)
|
||||||
|
addi sp, sp, 56
|
||||||
|
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[out] a0: result code.
|
||||||
|
# @param[out] a1: bytes written to mod buf.
|
||||||
|
add_line_numbers:
|
||||||
|
addi sp, sp, -72
|
||||||
|
sd s7, 64(sp) # skip empty
|
||||||
|
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) # mod buf offset
|
||||||
|
sd s2, 16(sp) # str buf offset
|
||||||
|
sd s1, 8(sp) # len of str
|
||||||
|
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 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
|
||||||
|
|
||||||
|
# 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 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
|
||||||
|
|
||||||
|
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, 0x9 # tab character
|
||||||
|
sb t5, 0(s6)
|
||||||
|
|
||||||
|
addi s3, s3, 1
|
||||||
|
|
||||||
|
j 2f
|
||||||
|
|
||||||
|
1:
|
||||||
|
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:
|
||||||
|
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 s6, 64(sp)
|
||||||
|
addi sp, sp, 72
|
||||||
|
ret
|
||||||
|
|
||||||
|
|
||||||
|
# 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, 6 # 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
|
||||||
Reference in New Issue
Block a user