diff --git a/src/args.S b/src/args.S index 5c24494..c5009c7 100644 --- a/src/args.S +++ b/src/args.S @@ -166,22 +166,22 @@ has_switch: _str_cmp: li t2, 0 - 1: + .LByteCmp: add t0, a0, t2 add t1, a1, t2 lb t0, 0(t0) lb t1, 0(t1) - bne t0, t1, 3f # first byte that isn't the same, branch to no match - beqz t0, 2f # jump to ret when null is read + bne t0, t1, .LNoMatch # first byte that isn't the same, branch to no match + beqz t0, .LMatch # jump to ret when null is read addi t2, t2, 1 - j 1b + j .LByteCmp - 3: # no match + .LNoMatch: # no match li a0, 0 li a1, 0 ret - 2: # match + .LMatch: # match li a0, 0 li a1, 1 ret diff --git a/src/file.S b/src/file.S index deb9265..cac568f 100644 --- a/src/file.S +++ b/src/file.S @@ -17,32 +17,32 @@ cat_file: addi sp, sp, 8 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 - j 2f + j .LRet - 1: + .LReadBlock: mv a0, t0 la a1, cat_buf li a2, 4096 li a7, 63 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 li a0, 1 la a1, cat_buf li a7, 64 ecall - j 1b + j .LReadBlock - 3: + .LCloseFD: mv a0, t0 li a7, 57 # close fd ecall - 2: + .LRet: ret diff --git a/src/main.S b/src/main.S index 71c07fb..ca91101 100644 --- a/src/main.S +++ b/src/main.S @@ -1,19 +1,21 @@ .data argc: .space 8 -cat_prefix: .asciz "cat: " +cat_prefix: .ascii "cat: " .equ CAT_PREFIX_LEN, . - cat_prefix file_err: .asciz ": No such file or directory\n" .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: \n" .equ HELP_LEN, . - help flag_h: .asciz "-h" -# .equ FLAG_H_LEN, . - flag_h flag_help: .asciz "--help" -# .equ FLAG_HELP_LEN, . - flag_help .section .text @@ -28,17 +30,17 @@ _start: call save_sp call fetch_argc 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 li s1, 1 la a0, flag_h la a1, flag_help call has_switch - bnez a0, 1f + bnez a0, .Lreturn_err beqz a1, .Lskip_disp_help call _disp_help - j 2f + j .Lreturn_ok .Lskip_disp_help: @@ -54,19 +56,19 @@ _start: # li s1, 1 # reset counter now that we've verified all files exist, skip command - 3: + .Lprocess_file: mv a0, s1 call fetch_argv - bnez a0, 1f # error if failed + bnez a0, .Lreturn_err # error if failed mv a0, a1 call cat_file - bnez a0, 4f # error if failed + bnez a0, .Lreturn_file_err # error if failed addi s1, s1, 1 - blt s1, s0, 3b - j 2f + blt s1, s0, .Lprocess_file + j .Lreturn_ok - 4: - li a0, 1 + .Lreturn_file_err: + li a0, 2 la a1, cat_prefix li a2, CAT_PREFIX_LEN li a7, 64 @@ -75,23 +77,23 @@ _start: mv a0, s1 call fetch_argv - li a0, 1 + li a0, 2 li a7, 64 ecall - li a0, 1 + li a0, 2 la a1, file_err li a2, FILE_ERR_LEN li a7, 64 ecall - 1: + .Lreturn_err: #1 li a0, 1 li a7, 93 ecall # return 1 - 2: + .Lreturn_ok: #2 li a0, 0 li a7, 93 ecall # return 0