Show ends #3
+110
-19
@@ -1,5 +1,6 @@
|
|||||||
.data
|
.data
|
||||||
cat_buf: .space 4096
|
cat_buf: .space 4096
|
||||||
|
endings_buf: .space 4097
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl cat_file, open_fd, cat_stdin
|
.globl cat_file, open_fd, cat_stdin
|
||||||
@@ -8,6 +9,11 @@ cat_buf: .space 4096
|
|||||||
# @param[in] a0: ptr to null-terminated file str.
|
# @param[in] a0: ptr to null-terminated file str.
|
||||||
# @param[out] a0: result code.
|
# @param[out] a0: result code.
|
||||||
cat_file:
|
cat_file:
|
||||||
|
addi sp, sp, -24
|
||||||
|
sd s0, 0(sp)
|
||||||
|
sd ra, 8(sp)
|
||||||
|
sd s1, 16(sp)
|
||||||
|
|
||||||
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
||||||
|
|
||||||
addi sp, sp, -8
|
addi sp, sp, -8
|
||||||
@@ -16,21 +22,30 @@ cat_file:
|
|||||||
ld ra, 0(sp)
|
ld ra, 0(sp)
|
||||||
addi sp, sp, 8
|
addi sp, sp, 8
|
||||||
|
|
||||||
mv t0, a1 # put fd in t0
|
mv s0, a1 # put fd in s0
|
||||||
|
la t1, show_ends
|
||||||
|
lb s1, 0(t1) # store if we show ends into s1
|
||||||
|
|
||||||
beqz a0, .LReadFileBlock # if we don't have an error, jump to reading
|
beqz a0, .LReadFileBlock # if we don't have an error, jump to reading
|
||||||
li a0, 1
|
li a0, 1
|
||||||
j .LCatFileRet
|
j .LCatFileRet
|
||||||
|
|
||||||
|
|
||||||
.LReadFileBlock:
|
.LReadFileBlock:
|
||||||
mv a0, t0
|
mv a0, s0
|
||||||
la a1, cat_buf
|
la a1, cat_buf
|
||||||
li a2, 4096
|
li a2, 4096
|
||||||
li a7, 63
|
li a7, 63
|
||||||
ecall
|
ecall
|
||||||
blez a0, .LCloseFD # if we haven't read any bytes, EOF, close FD
|
blez a0, .LCloseFD # if we haven't read any bytes, EOF, close FD
|
||||||
mv a2, a0 # move bytes read into bytes needed to write
|
beqz s1, .LNoEndingsWrite
|
||||||
|
mv a1, a0
|
||||||
|
la a0, cat_buf
|
||||||
|
call _stdout_add_endings
|
||||||
|
j .LReadFileBlock
|
||||||
|
|
||||||
|
.LNoEndingsWrite:
|
||||||
|
mv a2, a0 # load the amount of bytes read in previous step into bytes needed to write
|
||||||
li a0, 1
|
li a0, 1
|
||||||
la a1, cat_buf
|
la a1, cat_buf
|
||||||
li a7, 64
|
li a7, 64
|
||||||
@@ -38,11 +53,15 @@ cat_file:
|
|||||||
j .LReadFileBlock
|
j .LReadFileBlock
|
||||||
|
|
||||||
.LCloseFD:
|
.LCloseFD:
|
||||||
mv a0, t0
|
mv a0, s0
|
||||||
li a7, 57 # close fd
|
li a7, 57 # close fd
|
||||||
ecall
|
ecall
|
||||||
|
|
||||||
.LCatFileRet:
|
.LCatFileRet:
|
||||||
|
ld s0, 0(sp)
|
||||||
|
ld ra, 8(sp)
|
||||||
|
ld s1, 16(sp)
|
||||||
|
addi sp, sp, 24
|
||||||
ret
|
ret
|
||||||
|
|
||||||
# Cat out stdin to stdout
|
# Cat out stdin to stdout
|
||||||
@@ -89,19 +108,91 @@ open_fd:
|
|||||||
2:
|
2:
|
||||||
ret
|
ret
|
||||||
|
|
||||||
# # Check if the given file exists without opening fd
|
# Print to stdout while adding "$" characters.
|
||||||
# # @param[in] a0: ptr to null-terminated file str.
|
# @param[in] a0: ptr to null-terminated str.
|
||||||
# # @param[out] a0: result code (0-exists 1-doesn't exist).
|
# @param[in] a1: length of str.
|
||||||
# file_exists:
|
# @param[out] a0: result code.
|
||||||
# mv a1, a0
|
_stdout_add_endings:
|
||||||
# li a0, -100 # relative/absolute path
|
addi sp, sp, -72
|
||||||
# li a2, 0
|
sd s7, 64(sp)
|
||||||
# li a7, 48
|
sd s6, 56(sp)
|
||||||
# ecall
|
sd s5, 48(sp)
|
||||||
# beqz a0, 1f
|
sd s4, 40(sp)
|
||||||
# li a0, 1
|
sd ra, 32(sp)
|
||||||
# ret
|
sd s3, 24(sp)
|
||||||
|
sd s2, 16(sp)
|
||||||
|
sd s1, 8(sp)
|
||||||
|
sd s0, 0(sp)
|
||||||
|
|
||||||
# 1:
|
|
||||||
# li a0, 0
|
mv s0, a0 # put the cat_buf ptr in s0
|
||||||
# ret
|
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
|
||||||
|
|
||||||
|
la s5, endings_buf
|
||||||
|
.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
|
||||||
|
|
||||||
|
li a0, 1
|
||||||
|
la a1, endings_buf
|
||||||
|
mv a2, s3
|
||||||
|
li a7, 64
|
||||||
|
ecall
|
||||||
|
call _zero_endings_buf
|
||||||
|
li s3, 0 # reset endings_buf char counter
|
||||||
|
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
|
||||||
|
beqz s3, .LSkipEndingsPrint
|
||||||
|
li a0, 1
|
||||||
|
la a1, endings_buf
|
||||||
|
mv a2, s3
|
||||||
|
li a7, 64
|
||||||
|
ecall
|
||||||
|
.LSkipEndingsPrint:
|
||||||
|
|
||||||
|
li a0, 0
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
_zero_endings_buf:
|
||||||
|
li t0, 0
|
||||||
|
li t1, 4097
|
||||||
|
|
||||||
|
.LZeroEndingsBufByte:
|
||||||
|
la t2, endings_buf
|
||||||
|
add t2, t2, t0
|
||||||
|
sb x0, 0(t2)
|
||||||
|
addi t0, t0, 1
|
||||||
|
blt t0, t1, .LZeroEndingsBufByte
|
||||||
|
|
||||||
|
ret
|
||||||
|
|||||||
+13
-3
@@ -1,5 +1,6 @@
|
|||||||
.data
|
.data
|
||||||
argc: .space 8
|
argc: .space 8
|
||||||
|
show_ends: .space 1
|
||||||
|
|
||||||
cat_prefix: .ascii "cat: "
|
cat_prefix: .ascii "cat: "
|
||||||
.equ CAT_PREFIX_LEN, . - cat_prefix
|
.equ CAT_PREFIX_LEN, . - cat_prefix
|
||||||
@@ -11,13 +12,14 @@ 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 " --help display this help and exit\n"
|
.ascii " --help display this help and exit\n"
|
||||||
.ascii " --version output version information and exit\n\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.0.0\n"
|
.ascii "cat 1.1.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"
|
||||||
@@ -27,9 +29,11 @@ version:
|
|||||||
|
|
||||||
flag_help: .asciz "--help"
|
flag_help: .asciz "--help"
|
||||||
flag_version: .asciz "--version"
|
flag_version: .asciz "--version"
|
||||||
|
flag_show_ends: .asciz "--show-ends"
|
||||||
|
flag_E: .asciz "-E"
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl _start
|
.globl _start, show_ends
|
||||||
|
|
||||||
_start:
|
_start:
|
||||||
.option push
|
.option push
|
||||||
@@ -113,7 +117,7 @@ _check_flags:
|
|||||||
|
|
||||||
.LSkipDispHelp:
|
.LSkipDispHelp:
|
||||||
|
|
||||||
la a0, flag_version # help flag
|
la a0, flag_version # version flag
|
||||||
li a1, 0 # no second flag
|
li a1, 0 # no second flag
|
||||||
call has_switch
|
call has_switch
|
||||||
bnez a0, .LMainReturnErr
|
bnez a0, .LMainReturnErr
|
||||||
@@ -126,6 +130,12 @@ _check_flags:
|
|||||||
j .LMainReturnOk
|
j .LMainReturnOk
|
||||||
.LSkipDispVersion:
|
.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
|
||||||
|
|
||||||
ld ra, 0(sp)
|
ld ra, 0(sp)
|
||||||
addi sp, sp, 8
|
addi sp, sp, 8
|
||||||
ret
|
ret
|
||||||
|
|||||||
Reference in New Issue
Block a user