Rename jumps
This commit is contained in:
+6
-6
@@ -166,22 +166,22 @@ has_switch:
|
|||||||
_str_cmp:
|
_str_cmp:
|
||||||
li t2, 0
|
li t2, 0
|
||||||
|
|
||||||
1:
|
.LByteCmp:
|
||||||
add t0, a0, t2
|
add t0, a0, t2
|
||||||
add t1, a1, t2
|
add t1, a1, t2
|
||||||
lb t0, 0(t0)
|
lb t0, 0(t0)
|
||||||
lb t1, 0(t1)
|
lb t1, 0(t1)
|
||||||
bne t0, t1, 3f # first byte that isn't the same, branch to no match
|
bne t0, t1, .LNoMatch # first byte that isn't the same, branch to no match
|
||||||
beqz t0, 2f # jump to ret when null is read
|
beqz t0, .LMatch # jump to ret when null is read
|
||||||
addi t2, t2, 1
|
addi t2, t2, 1
|
||||||
j 1b
|
j .LByteCmp
|
||||||
|
|
||||||
3: # no match
|
.LNoMatch: # no match
|
||||||
li a0, 0
|
li a0, 0
|
||||||
li a1, 0
|
li a1, 0
|
||||||
ret
|
ret
|
||||||
|
|
||||||
2: # match
|
.LMatch: # match
|
||||||
li a0, 0
|
li a0, 0
|
||||||
li a1, 1
|
li a1, 1
|
||||||
ret
|
ret
|
||||||
|
|||||||
+7
-7
@@ -17,32 +17,32 @@ cat_file:
|
|||||||
addi sp, sp, 8
|
addi sp, sp, 8
|
||||||
|
|
||||||
mv t0, a1 # put fd in t0
|
mv t0, a1 # put fd in t0
|
||||||
beqz a0, 1f # if we don't have an error, jump to reading
|
beqz a0, .LReadBlock # if we don't have an error, jump to reading
|
||||||
li a0, 1
|
li a0, 1
|
||||||
j 2f
|
j .LRet
|
||||||
|
|
||||||
|
|
||||||
1:
|
.LReadBlock:
|
||||||
mv a0, t0
|
mv a0, t0
|
||||||
la a1, cat_buf
|
la a1, cat_buf
|
||||||
li a2, 4096
|
li a2, 4096
|
||||||
li a7, 63
|
li a7, 63
|
||||||
ecall
|
ecall
|
||||||
blez a0, 3f
|
blez a0, .LCloseFD # if we haven't read any bytes, EOF, close FD
|
||||||
mv a2, a0 # move bytes read into bytes needed to write
|
mv a2, a0 # move bytes read 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
|
||||||
ecall
|
ecall
|
||||||
j 1b
|
j .LReadBlock
|
||||||
|
|
||||||
3:
|
.LCloseFD:
|
||||||
mv a0, t0
|
mv a0, t0
|
||||||
li a7, 57 # close fd
|
li a7, 57 # close fd
|
||||||
ecall
|
ecall
|
||||||
|
|
||||||
2:
|
.LRet:
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+20
-18
@@ -1,19 +1,21 @@
|
|||||||
.data
|
.data
|
||||||
argc: .space 8
|
argc: .space 8
|
||||||
|
|
||||||
cat_prefix: .asciz "cat: "
|
cat_prefix: .ascii "cat: "
|
||||||
.equ CAT_PREFIX_LEN, . - cat_prefix
|
.equ CAT_PREFIX_LEN, . - cat_prefix
|
||||||
|
|
||||||
file_err: .asciz ": No such file or directory\n"
|
file_err: .asciz ": No such file or directory\n"
|
||||||
.equ FILE_ERR_LEN, . - file_err
|
.equ FILE_ERR_LEN, . - file_err
|
||||||
|
|
||||||
help: .asciz "Usage: cat [OPTION]... [FILE]...\nConcatenate FILE(S) to standard output.\n"
|
help:
|
||||||
|
.ascii "Usage: cat [OPTION]... [FILE]...\n"
|
||||||
|
.ascii "Concatenate FILE(S) to standard output.\n\n"
|
||||||
|
.ascii " -h, --help | Show this help page.\n\n"
|
||||||
|
.asciz "Written by lexzach in RISC-V assembly: <https://git.lexza.ch/Lexzach/riscv-cat>\n"
|
||||||
.equ HELP_LEN, . - help
|
.equ HELP_LEN, . - help
|
||||||
|
|
||||||
flag_h: .asciz "-h"
|
flag_h: .asciz "-h"
|
||||||
# .equ FLAG_H_LEN, . - flag_h
|
|
||||||
flag_help: .asciz "--help"
|
flag_help: .asciz "--help"
|
||||||
# .equ FLAG_HELP_LEN, . - flag_help
|
|
||||||
|
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
@@ -28,17 +30,17 @@ _start:
|
|||||||
call save_sp
|
call save_sp
|
||||||
call fetch_argc
|
call fetch_argc
|
||||||
li t0, 1
|
li t0, 1
|
||||||
ble a1, t0, 1f # return 1 if no args
|
ble a1, t0, .Lreturn_err # return 1 if no args
|
||||||
mv s0, a1 # store argc
|
mv s0, a1 # store argc
|
||||||
li s1, 1
|
li s1, 1
|
||||||
|
|
||||||
la a0, flag_h
|
la a0, flag_h
|
||||||
la a1, flag_help
|
la a1, flag_help
|
||||||
call has_switch
|
call has_switch
|
||||||
bnez a0, 1f
|
bnez a0, .Lreturn_err
|
||||||
beqz a1, .Lskip_disp_help
|
beqz a1, .Lskip_disp_help
|
||||||
call _disp_help
|
call _disp_help
|
||||||
j 2f
|
j .Lreturn_ok
|
||||||
.Lskip_disp_help:
|
.Lskip_disp_help:
|
||||||
|
|
||||||
|
|
||||||
@@ -54,19 +56,19 @@ _start:
|
|||||||
|
|
||||||
# li s1, 1 # reset counter now that we've verified all files exist, skip command
|
# li s1, 1 # reset counter now that we've verified all files exist, skip command
|
||||||
|
|
||||||
3:
|
.Lprocess_file:
|
||||||
mv a0, s1
|
mv a0, s1
|
||||||
call fetch_argv
|
call fetch_argv
|
||||||
bnez a0, 1f # error if failed
|
bnez a0, .Lreturn_err # error if failed
|
||||||
mv a0, a1
|
mv a0, a1
|
||||||
call cat_file
|
call cat_file
|
||||||
bnez a0, 4f # error if failed
|
bnez a0, .Lreturn_file_err # error if failed
|
||||||
addi s1, s1, 1
|
addi s1, s1, 1
|
||||||
blt s1, s0, 3b
|
blt s1, s0, .Lprocess_file
|
||||||
j 2f
|
j .Lreturn_ok
|
||||||
|
|
||||||
4:
|
.Lreturn_file_err:
|
||||||
li a0, 1
|
li a0, 2
|
||||||
la a1, cat_prefix
|
la a1, cat_prefix
|
||||||
li a2, CAT_PREFIX_LEN
|
li a2, CAT_PREFIX_LEN
|
||||||
li a7, 64
|
li a7, 64
|
||||||
@@ -75,23 +77,23 @@ _start:
|
|||||||
mv a0, s1
|
mv a0, s1
|
||||||
call fetch_argv
|
call fetch_argv
|
||||||
|
|
||||||
li a0, 1
|
li a0, 2
|
||||||
li a7, 64
|
li a7, 64
|
||||||
ecall
|
ecall
|
||||||
|
|
||||||
li a0, 1
|
li a0, 2
|
||||||
la a1, file_err
|
la a1, file_err
|
||||||
li a2, FILE_ERR_LEN
|
li a2, FILE_ERR_LEN
|
||||||
li a7, 64
|
li a7, 64
|
||||||
ecall
|
ecall
|
||||||
|
|
||||||
|
|
||||||
1:
|
.Lreturn_err: #1
|
||||||
li a0, 1
|
li a0, 1
|
||||||
li a7, 93
|
li a7, 93
|
||||||
ecall # return 1
|
ecall # return 1
|
||||||
|
|
||||||
2:
|
.Lreturn_ok: #2
|
||||||
li a0, 0
|
li a0, 0
|
||||||
li a7, 93
|
li a7, 93
|
||||||
ecall # return 0
|
ecall # return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user