Show ends #3
@@ -16,4 +16,6 @@ Run the `make` command in the root of the repository.
|
|||||||
|
|
||||||
Options:
|
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
@@ -2,7 +2,7 @@
|
|||||||
sp_start: .space 8
|
sp_start: .space 8
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl save_sp, fetch_argc, fetch_argv, has_switch
|
.globl save_sp, fetch_argc, fetch_argv, has_switch, str_cmp
|
||||||
|
|
||||||
save_sp:
|
save_sp:
|
||||||
sd sp, sp_start, t0 # save the stack pointer
|
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 a0, s3 # load argv buff addr into a0 (string 0)
|
||||||
mv a1, s0 # move switch ptr 1 addr into a1 (string 1)
|
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
|
bnez a1, .LSwitchFound # if the strings match, exit loop
|
||||||
beqz s1, .LSkipSecondSwitch # if we have no second switch, skip the check
|
beqz s1, .LSkipSecondSwitch # if we have no second switch, skip the check
|
||||||
|
|
||||||
mv a0, s3 # move argv buff addr into a0 (string 0)
|
mv a0, s3 # move argv buff addr into a0 (string 0)
|
||||||
mv a1, s1 # move switch ptr 2 addr into a1 (string 1)
|
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
|
bnez a1, .LSwitchFound # if the strings match, exit loop
|
||||||
|
|
||||||
.LSkipSecondSwitch:
|
.LSkipSecondSwitch:
|
||||||
@@ -170,7 +170,7 @@ has_switch:
|
|||||||
# @param[in] a1: str buffer 2
|
# @param[in] a1: str buffer 2
|
||||||
# @param[out] a0: result code.
|
# @param[out] a0: result code.
|
||||||
# @param[out] a1: is equal (0-no, 1-yes).
|
# @param[out] a1: is equal (0-no, 1-yes).
|
||||||
_str_cmp:
|
str_cmp:
|
||||||
li t2, 0
|
li t2, 0
|
||||||
|
|
||||||
.LByteCmp:
|
.LByteCmp:
|
||||||
|
|||||||
+49
-1
@@ -35,6 +35,7 @@ flag_E: .asciz "-E"
|
|||||||
.section .text
|
.section .text
|
||||||
.globl _start, show_ends
|
.globl _start, show_ends
|
||||||
|
|
||||||
|
# main.
|
||||||
_start:
|
_start:
|
||||||
.option push
|
.option push
|
||||||
.option norelax
|
.option norelax
|
||||||
@@ -56,8 +57,15 @@ _start:
|
|||||||
call fetch_argv
|
call fetch_argv
|
||||||
bnez a0, .LMainReturnErr # error if failed
|
bnez a0, .LMainReturnErr # error if failed
|
||||||
mv a0, a1
|
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
|
call cat_file
|
||||||
bnez a0, .LReturnFileErr # error if failed
|
bnez a0, .LReturnFileErr # error if failed
|
||||||
|
.LSkipArg:
|
||||||
addi s1, s1, 1
|
addi s1, s1, 1
|
||||||
blt s1, s0, .LProcessFile
|
blt s1, s0, .LProcessFile
|
||||||
j .LMainReturnOk
|
j .LMainReturnOk
|
||||||
@@ -98,7 +106,9 @@ _start:
|
|||||||
ecall # return 0
|
ecall # return 0
|
||||||
|
|
||||||
|
|
||||||
|
# Check for flags, branch or set buffers as needed.
|
||||||
|
# @param[in] a0: None.
|
||||||
|
# @param[out] a0: None.
|
||||||
_check_flags:
|
_check_flags:
|
||||||
addi sp, sp, -8
|
addi sp, sp, -8
|
||||||
sd ra, 0(sp)
|
sd ra, 0(sp)
|
||||||
@@ -139,3 +149,41 @@ _check_flags:
|
|||||||
ld ra, 0(sp)
|
ld ra, 0(sp)
|
||||||
addi sp, sp, 8
|
addi sp, sp, 8
|
||||||
ret
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user