Add version switch
This commit is contained in:
+3
-1
@@ -94,7 +94,7 @@ fetch_argv:
|
|||||||
|
|
||||||
# Check if flag was given
|
# Check if flag was given
|
||||||
# @param[in] a0: ptr to null-terminated switch 1 (ie. -h).
|
# @param[in] a0: ptr to null-terminated switch 1 (ie. -h).
|
||||||
# @param[in] a1: ptr to null-terminated switch 2 (ie. --help) (optional).
|
# @param[in] a1: ptr to null-terminated switch 2 (ie. --help) (optional, pass 0 for none).
|
||||||
# @param[out] a0: result code.
|
# @param[out] a0: result code.
|
||||||
# @param[out] a1: was found (0-not found, 1-found).
|
# @param[out] a1: was found (0-not found, 1-found).
|
||||||
has_switch:
|
has_switch:
|
||||||
@@ -127,12 +127,14 @@ has_switch:
|
|||||||
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
|
||||||
|
|
||||||
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:
|
||||||
addi s2, s2, 1 # if string doesn't match we inc to the next arg
|
addi s2, s2, 1 # if string doesn't match we inc to the next arg
|
||||||
blt s2, s4, .LSwitchProcessNextArg # keep going while we have untested args
|
blt s2, s4, .LSwitchProcessNextArg # keep going while we have untested args
|
||||||
|
|
||||||
|
|||||||
+47
-16
@@ -11,13 +11,22 @@ help:
|
|||||||
.ascii "Usage: cat [OPTION]... [FILE]...\n"
|
.ascii "Usage: cat [OPTION]... [FILE]...\n"
|
||||||
.ascii "Concatenate FILE(S) to standard output.\n\n"
|
.ascii "Concatenate FILE(S) to standard output.\n\n"
|
||||||
.ascii "With no FILE, or when FILE is -, read standard input.\n\n"
|
.ascii "With no FILE, or when FILE is -, read standard input.\n\n"
|
||||||
.ascii " -h, --help | Show this help page.\n\n"
|
.ascii " --help display this help and exit\n"
|
||||||
.asciz "Written by lexzach in RISC-V assembly: <https://git.lexza.ch/Lexzach/riscv-cat>\n"
|
.ascii " --version output version information and exit\n\n"
|
||||||
|
.asciz "Git repository: <https://git.lexza.ch/Lexzach/riscv-cat>\n"
|
||||||
.equ HELP_LEN, . - help
|
.equ HELP_LEN, . - help
|
||||||
|
|
||||||
flag_h: .asciz "-h"
|
version:
|
||||||
flag_help: .asciz "--help"
|
.ascii "cat 1.0.0\n"
|
||||||
|
.ascii "Copyright (C) 2026 lexzach\n"
|
||||||
|
.ascii "License MIT\n"
|
||||||
|
.ascii "This is free software: you are free to change and redistribute it.\n"
|
||||||
|
.ascii "There is NO WARRANTY, to the extent permitted by law.\n\n"
|
||||||
|
.asciz "Written by lexzach in pure RISC-V assembly\n"
|
||||||
|
.equ VERSION_LEN, . - version
|
||||||
|
|
||||||
|
flag_help: .asciz "--help"
|
||||||
|
flag_version: .asciz "--version"
|
||||||
|
|
||||||
.section .text
|
.section .text
|
||||||
.globl _start
|
.globl _start
|
||||||
@@ -32,18 +41,11 @@ _start:
|
|||||||
call fetch_argc
|
call fetch_argc
|
||||||
mv s0, a1 # store argc
|
mv s0, a1 # store argc
|
||||||
li s1, 1
|
li s1, 1
|
||||||
|
|
||||||
la a0, flag_h
|
|
||||||
la a1, flag_help
|
|
||||||
call has_switch
|
|
||||||
bnez a0, .LMainReturnErr
|
|
||||||
beqz a1, .LSkipDispHelp
|
|
||||||
call _disp_help
|
|
||||||
j .LMainReturnOk
|
|
||||||
.LSkipDispHelp:
|
|
||||||
|
|
||||||
|
call _check_flags
|
||||||
|
|
||||||
li t0, 1
|
li t0, 1
|
||||||
ble s0, t0, .LProcessStdin # return 1 if no args
|
ble s0, t0, .LProcessStdin # use stdin if no commands
|
||||||
|
|
||||||
.LProcessFile:
|
.LProcessFile:
|
||||||
mv a0, s1
|
mv a0, s1
|
||||||
@@ -91,10 +93,39 @@ _start:
|
|||||||
li a7, 93
|
li a7, 93
|
||||||
ecall # return 0
|
ecall # return 0
|
||||||
|
|
||||||
_disp_help:
|
|
||||||
la a0, 1
|
|
||||||
|
_check_flags:
|
||||||
|
addi sp, sp, -8
|
||||||
|
sd ra, 0(sp)
|
||||||
|
|
||||||
|
la a0, flag_help # help flag
|
||||||
|
li a1, 0 # no second flag
|
||||||
|
call has_switch
|
||||||
|
bnez a0, .LMainReturnErr
|
||||||
|
beqz a1, .LSkipDispHelp
|
||||||
|
li a0, 1
|
||||||
la a1, help
|
la a1, help
|
||||||
li a2, HELP_LEN
|
li a2, HELP_LEN
|
||||||
li a7, 64
|
li a7, 64
|
||||||
ecall
|
ecall
|
||||||
|
j .LMainReturnOk
|
||||||
|
|
||||||
|
.LSkipDispHelp:
|
||||||
|
|
||||||
|
la a0, flag_version # help flag
|
||||||
|
li a1, 0 # no second flag
|
||||||
|
call has_switch
|
||||||
|
bnez a0, .LMainReturnErr
|
||||||
|
beqz a1, .LSkipDispVersion
|
||||||
|
li a0, 1
|
||||||
|
la a1, version
|
||||||
|
li a2, VERSION_LEN
|
||||||
|
li a7, 64
|
||||||
|
ecall
|
||||||
|
j .LMainReturnOk
|
||||||
|
.LSkipDispVersion:
|
||||||
|
|
||||||
|
ld ra, 0(sp)
|
||||||
|
addi sp, sp, 8
|
||||||
ret
|
ret
|
||||||
|
|||||||
Reference in New Issue
Block a user