diff --git a/src/args.S b/src/args.S index c44e602..29d9527 100644 --- a/src/args.S +++ b/src/args.S @@ -94,7 +94,7 @@ fetch_argv: # Check if flag was given # @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] a1: was found (0-not found, 1-found). has_switch: @@ -127,12 +127,14 @@ has_switch: mv a1, s0 # move switch ptr 1 addr into a1 (string 1) 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 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 blt s2, s4, .LSwitchProcessNextArg # keep going while we have untested args diff --git a/src/main.S b/src/main.S index badcf02..3bb30b4 100644 --- a/src/main.S +++ b/src/main.S @@ -11,13 +11,22 @@ help: .ascii "Usage: cat [OPTION]... [FILE]...\n" .ascii "Concatenate FILE(S) to standard output.\n\n" .ascii "With no FILE, or when FILE is -, read standard input.\n\n" - .ascii " -h, --help | Show this help page.\n\n" - .asciz "Written by lexzach in RISC-V assembly: \n" + .ascii " --help display this help and exit\n" + .ascii " --version output version information and exit\n\n" + .asciz "Git repository: \n" .equ HELP_LEN, . - help -flag_h: .asciz "-h" -flag_help: .asciz "--help" +version: + .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 .globl _start @@ -32,18 +41,11 @@ _start: call fetch_argc mv s0, a1 # store argc 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 - ble s0, t0, .LProcessStdin # return 1 if no args + ble s0, t0, .LProcessStdin # use stdin if no commands .LProcessFile: mv a0, s1 @@ -91,10 +93,39 @@ _start: li a7, 93 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 li a2, HELP_LEN li a7, 64 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