92 lines
1.7 KiB
ArmAsm
92 lines
1.7 KiB
ArmAsm
.section .text
|
|
.option norvc
|
|
.global _start
|
|
|
|
# Helper macro to exit with code if condition fails
|
|
.macro ASSERT_EQ reg, val, error_code
|
|
li t4, \val
|
|
beq \reg, t4, .L_ok_\@
|
|
li a0, \error_code
|
|
li a7, 93
|
|
ecall
|
|
.L_ok_\@:
|
|
.endm
|
|
|
|
# Compare string at register \str_reg with data label \label, exit with \error_code if not equal
|
|
.macro ASSERT_STR_EQ str_reg, label, error_code
|
|
la t0, \label
|
|
mv t1, \str_reg
|
|
.L_loop_\@:
|
|
lbu t2, 0(t0)
|
|
lbu t3, 0(t1)
|
|
bne t2, t3, .L_fail_\@
|
|
beqz t2, .L_ok_\@
|
|
addi t0, t0, 1
|
|
addi t1, t1, 1
|
|
j .L_loop_\@
|
|
.L_fail_\@:
|
|
li a0, \error_code
|
|
li a7, 93
|
|
ecall
|
|
.L_ok_\@:
|
|
.endm
|
|
|
|
_start:
|
|
# sp + 0: argc
|
|
# sp + 4: argv[0]
|
|
# sp + 8: argv[1]
|
|
# sp + 12: argv[2]
|
|
|
|
# 1. Print verification start message
|
|
li a7, 64 # sys_write
|
|
li a0, 1 # stdout
|
|
la a1, msg_start
|
|
li a2, 32 # message length
|
|
ecall
|
|
|
|
# 2. Check argc == 3
|
|
lw t0, 0(sp)
|
|
ASSERT_EQ t0, 3, 10
|
|
|
|
# 3. Check argv[0] is not null and not empty
|
|
lw t0, 4(sp)
|
|
beqz t0, .L_argv0_bad
|
|
lbu t1, 0(t0)
|
|
beqz t1, .L_argv0_bad
|
|
j .L_argv0_ok
|
|
.L_argv0_bad:
|
|
li a0, 11
|
|
li a7, 93
|
|
ecall
|
|
.L_argv0_ok:
|
|
|
|
# 4. Check argv[1] == "arg1"
|
|
lw t0, 8(sp)
|
|
ASSERT_STR_EQ t0, str_arg1, 12
|
|
|
|
# 5. Check argv[2] == "arg2"
|
|
lw t0, 12(sp)
|
|
ASSERT_STR_EQ t0, str_arg2, 13
|
|
|
|
# 6. Print success message
|
|
li a7, 64 # sys_write
|
|
li a0, 1 # stdout
|
|
la a1, msg_success
|
|
li a2, 32 # message length
|
|
ecall
|
|
|
|
# 7. Exit with code 0 on success
|
|
li a0, 0
|
|
li a7, 93
|
|
ecall
|
|
|
|
.section .rodata
|
|
msg_start:
|
|
.ascii "Running Command Line Args Tests\n"
|
|
msg_success:
|
|
.ascii "Command Line Args Tests PASSED!\n"
|
|
str_arg1:
|
|
.asciz "arg1"
|
|
str_arg2:
|
|
.asciz "arg2"
|