New modbuf for multiple flags #4
+46
-51
@@ -1,6 +1,8 @@
|
|||||||
.data
|
.data
|
||||||
cat_buf: .space 4096
|
cat_buf: .space 4096
|
||||||
endings_buf: .space 4097
|
endings_buf: .space 4097
|
||||||
|
mod_buf_0: .space 36864
|
||||||
|
mod_buf_1: .space 36864
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl cat_file, open_fd, cat_stdin
|
.globl cat_file, open_fd, cat_stdin
|
||||||
@@ -9,45 +11,62 @@ endings_buf: .space 4097
|
|||||||
# @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
|
addi sp, sp, -32
|
||||||
sd s0, 0(sp)
|
sd s0, 0(sp)
|
||||||
sd ra, 8(sp)
|
sd ra, 8(sp)
|
||||||
sd s1, 16(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
|
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
||||||
|
li s1, 1
|
||||||
|
li s2, 0
|
||||||
|
|
||||||
addi sp, sp, -8
|
|
||||||
sd ra, 0(sp)
|
|
||||||
call open_fd
|
call open_fd
|
||||||
ld ra, 0(sp)
|
|
||||||
addi sp, sp, 8
|
|
||||||
|
|
||||||
mv s0, a1 # put fd in s0
|
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, s0
|
mv a0, s0
|
||||||
la a1, cat_buf
|
la a1, mod_buf_0
|
||||||
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
|
||||||
beqz s1, .LNoEndingsWrite
|
mv s2, a0
|
||||||
mv a1, a0
|
|
||||||
la a0, cat_buf
|
|
||||||
call _stdout_add_endings
|
|
||||||
j .LReadFileBlock
|
|
||||||
|
|
||||||
.LNoEndingsWrite:
|
la t1, show_ends
|
||||||
mv a2, a0 # load the amount of bytes read in previous step into bytes needed to write
|
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 a0, 1
|
||||||
la a1, cat_buf
|
|
||||||
li a7, 64
|
li a7, 64
|
||||||
ecall
|
ecall
|
||||||
j .LReadFileBlock
|
j .LReadFileBlock
|
||||||
@@ -61,7 +80,8 @@ cat_file:
|
|||||||
ld s0, 0(sp)
|
ld s0, 0(sp)
|
||||||
ld ra, 8(sp)
|
ld ra, 8(sp)
|
||||||
ld s1, 16(sp)
|
ld s1, 16(sp)
|
||||||
addi sp, sp, 24
|
ld s2, 24(sp)
|
||||||
|
addi sp, sp, 32
|
||||||
ret
|
ret
|
||||||
|
|
||||||
# Cat out stdin to stdout
|
# Cat out stdin to stdout
|
||||||
@@ -86,7 +106,7 @@ cat_stdin:
|
|||||||
beqz s0, .LNoEndingsStdinWrite
|
beqz s0, .LNoEndingsStdinWrite
|
||||||
mv a1, a0
|
mv a1, a0
|
||||||
la a0, cat_buf
|
la a0, cat_buf
|
||||||
call _stdout_add_endings
|
call _add_endings
|
||||||
j .LReadStdinBlock
|
j .LReadStdinBlock
|
||||||
|
|
||||||
.LNoEndingsStdinWrite:
|
.LNoEndingsStdinWrite:
|
||||||
@@ -128,8 +148,10 @@ open_fd:
|
|||||||
# Print to stdout while adding "$" characters.
|
# Print to stdout while adding "$" characters.
|
||||||
# @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.
|
||||||
# @param[out] a0: result code.
|
# @param[out] a0: result code.
|
||||||
_stdout_add_endings:
|
# @param[out] a1: bytes written to mod buf.
|
||||||
|
_add_endings:
|
||||||
addi sp, sp, -72
|
addi sp, sp, -72
|
||||||
sd s7, 64(sp)
|
sd s7, 64(sp)
|
||||||
sd s6, 56(sp)
|
sd s6, 56(sp)
|
||||||
@@ -147,7 +169,7 @@ _stdout_add_endings:
|
|||||||
li s2, 0 # set the cat_buf char counter to 0
|
li s2, 0 # set the cat_buf char counter to 0
|
||||||
li s3, 0 # set the endings_buf char counter to 0
|
li s3, 0 # set the endings_buf char counter to 0
|
||||||
|
|
||||||
la s5, endings_buf
|
mv s5, a2
|
||||||
.LStdoutEndingsProcessChunk:
|
.LStdoutEndingsProcessChunk:
|
||||||
add s4, s0, s2 # Calculate the offset address
|
add s4, s0, s2 # Calculate the offset address
|
||||||
lb t6, 0(s4)
|
lb t6, 0(s4)
|
||||||
@@ -161,14 +183,6 @@ _stdout_add_endings:
|
|||||||
addi s4, s4, 1
|
addi s4, s4, 1
|
||||||
sb t5, 0(s4) # endings_buf[s2] = \n
|
sb t5, 0(s4) # endings_buf[s2] = \n
|
||||||
addi s3, s3, 2
|
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
|
j 2f
|
||||||
|
|
||||||
1:
|
1:
|
||||||
@@ -179,15 +193,9 @@ _stdout_add_endings:
|
|||||||
2:
|
2:
|
||||||
addi s2, s2, 1 # inc cat_buf pointer
|
addi s2, s2, 1 # inc cat_buf pointer
|
||||||
blt s2, s1, .LStdoutEndingsProcessChunk
|
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
|
li a0, 0
|
||||||
|
mv a1, s3
|
||||||
|
|
||||||
ld s0, 0(sp)
|
ld s0, 0(sp)
|
||||||
ld s1, 8(sp)
|
ld s1, 8(sp)
|
||||||
@@ -200,16 +208,3 @@ _stdout_add_endings:
|
|||||||
ld s7, 64(sp)
|
ld s7, 64(sp)
|
||||||
addi sp, sp, 72
|
addi sp, sp, 72
|
||||||
ret
|
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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user