Add _is_known_flag func

This commit is contained in:
2026-09-13 21:49:53 -04:00
parent 71c9a4fb36
commit cf66f92aae
3 changed files with 56 additions and 6 deletions
+3 -1
View File
@@ -16,4 +16,6 @@ Run the `make` command in the root of the repository.
Options:
`-h, --help` - Show a help page.
`-E, --show-ends` - display $ at end of each line\
`--help` - Show the help page.\
`--version` - Show the version page.
+4 -4
View File
@@ -2,7 +2,7 @@
sp_start: .space 8
.section .text
.globl save_sp, fetch_argc, fetch_argv, has_switch
.globl save_sp, fetch_argc, fetch_argv, has_switch, str_cmp
save_sp:
sd sp, sp_start, t0 # save the stack pointer
@@ -125,13 +125,13 @@ has_switch:
mv a0, s3 # load argv buff addr into a0 (string 0)
mv a1, s0 # move switch ptr 1 addr into a1 (string 1)
call _str_cmp
call str_cmp
bnez a1, .LSwitchFound # if the strings match, exit loop
beqz s1, .LSkipSecondSwitch # if we have no second switch, skip the check
mv a0, s3 # move argv buff addr into a0 (string 0)
mv a1, s1 # move switch ptr 2 addr into a1 (string 1)
call _str_cmp
call str_cmp
bnez a1, .LSwitchFound # if the strings match, exit loop
.LSkipSecondSwitch:
@@ -170,7 +170,7 @@ has_switch:
# @param[in] a1: str buffer 2
# @param[out] a0: result code.
# @param[out] a1: is equal (0-no, 1-yes).
_str_cmp:
str_cmp:
li t2, 0
.LByteCmp:
+49 -1
View File
@@ -35,6 +35,7 @@ flag_E: .asciz "-E"
.section .text
.globl _start, show_ends
# main.
_start:
.option push
.option norelax
@@ -56,8 +57,15 @@ _start:
call fetch_argv
bnez a0, .LMainReturnErr # error if failed
mv a0, a1
addi sp, sp, -8
sd a0, 0(sp)
call _is_known_flag
bnez a1, .LSkipArg # check if arg is flag
ld a0, 0(sp)
addi sp, sp, 8
call cat_file
bnez a0, .LReturnFileErr # error if failed
.LSkipArg:
addi s1, s1, 1
blt s1, s0, .LProcessFile
j .LMainReturnOk
@@ -98,7 +106,9 @@ _start:
ecall # return 0
# Check for flags, branch or set buffers as needed.
# @param[in] a0: None.
# @param[out] a0: None.
_check_flags:
addi sp, sp, -8
sd ra, 0(sp)
@@ -139,3 +149,41 @@ _check_flags:
ld ra, 0(sp)
addi sp, sp, 8
ret
# Check if given argv is a flag to determine if cat_file should be used
# @param[in] a0: ptr to null-terminated argv string.
# @param[out] a0: result code.
# @param[out] a1: found flag (0- not a flag, 1- flag).
_is_known_flag:
addi sp, sp, -16
sd ra, 8(sp)
sd s0, 0(sp)
mv s0, a0
la a0, flag_help
mv a1, s0
call str_cmp
bnez a1, .LFlagFound
la a0, flag_version
mv a1, s0
call str_cmp
bnez a1, .LFlagFound
la a0, flag_show_ends
mv a1, s0
call str_cmp
bnez a1, .LFlagFound
la a0, flag_E
mv a1, s0
call str_cmp
bnez a1, .LFlagFound
li a1, 0
.LFlagFound:
ld s0, 0(sp)
ld ra, 8(sp)
addi sp, sp, 16
ret