Add number feature #5
@@ -62,6 +62,26 @@ cat_file:
|
|||||||
mv s2, a1 # update the current byte count
|
mv s2, a1 # update the current byte count
|
||||||
.LNoEndings:
|
.LNoEndings:
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
beqz s1, 1f
|
beqz s1, 1f
|
||||||
la a1, mod_buf_0
|
la a1, mod_buf_0
|
||||||
j 2f
|
j 2f
|
||||||
|
|||||||
@@ -77,40 +77,60 @@ add_endings:
|
|||||||
# @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)
|
sd s7, 64(sp) #unused
|
||||||
sd s6, 56(sp)
|
sd s6, 56(sp) # mod buf ptr
|
||||||
sd s5, 48(sp)
|
sd s5, 48(sp) # mod buf bp
|
||||||
sd s4, 40(sp)
|
sd s4, 40(sp) # str buf ptr
|
||||||
sd ra, 32(sp)
|
sd ra, 32(sp)
|
||||||
sd s3, 24(sp)
|
sd s3, 24(sp) # mod buf offset
|
||||||
sd s2, 16(sp)
|
sd s2, 16(sp) # str buf offset
|
||||||
sd s1, 8(sp)
|
sd s1, 8(sp) # len of str
|
||||||
sd s0, 0(sp)
|
sd s0, 0(sp) # str buf addr
|
||||||
|
|
||||||
|
|
||||||
mv s0, a0 # put the str ptr in s0
|
mv s0, a0 # put the str ptr in s0
|
||||||
mv s1, a1
|
mv s1, a1 # move length of str into s1
|
||||||
li s2, 0 # set the str buf char counter to 0
|
li s2, 0 # set the str buf char offset to 0
|
||||||
li s3, 0 # set the mod buf char counter 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
|
|
||||||
.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
|
||||||
|
|
||||||
add s4, s5, s3 # move modbuf pointer
|
call _inc_line_count
|
||||||
li t5, '\n'
|
|
||||||
sb t5, 0(s4) # mod buf[s2] = \n
|
|
||||||
|
|
||||||
addi s4, s4, 1
|
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
|
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, 0x7 # tab character
|
||||||
|
sb t5, 0(s6)
|
||||||
|
|
||||||
|
addi s3, s3, 1
|
||||||
|
|
||||||
j 2f
|
j 2f
|
||||||
|
|
||||||
1:
|
1:
|
||||||
add s4, s5, s3 # s4 = mod buf[s2]
|
add s6, s5, s3 # s4 = mod buf[s2]
|
||||||
sb t6, 0(s4) # mod buf[s2] = t6
|
sb t6, 0(s6) # mod buf[s2] = t6
|
||||||
|
|
||||||
addi s3, s3, 1 # inc mod buf pointer
|
addi s3, s3, 1 # inc mod buf pointer
|
||||||
2:
|
2:
|
||||||
@@ -175,7 +195,7 @@ _inc_line_count:
|
|||||||
zero_line_count:
|
zero_line_count:
|
||||||
la t0, line_count # load the line count buffer address
|
la t0, line_count # load the line count buffer address
|
||||||
li t1, 0 # char ptr
|
li t1, 0 # char ptr
|
||||||
li t2, 5 # max char
|
li t2, 6 # max char
|
||||||
li t3, 32 # space character
|
li t3, 32 # space character
|
||||||
|
|
||||||
1:
|
1:
|
||||||
|
|||||||
Reference in New Issue
Block a user