Complete single file cat
This commit is contained in:
+4
-4
@@ -29,7 +29,7 @@ fetch_argv:
|
||||
addi sp, sp, -16
|
||||
sd ra, 8(sp)
|
||||
sd s0, 0(sp)
|
||||
call fetch_argc
|
||||
call fetch_argc # get argument count
|
||||
ld ra, 8(sp)
|
||||
ld s0, 0(sp)
|
||||
addi sp, sp, 16
|
||||
@@ -47,14 +47,14 @@ fetch_argv:
|
||||
sd ra, 16(sp)
|
||||
sd s1, 8(sp)
|
||||
sd s0, 0(sp)
|
||||
call _next_argv
|
||||
call _next_argv # grab the next argument
|
||||
ld ra, 16(sp)
|
||||
ld s1, 8(sp)
|
||||
ld s0, 0(sp)
|
||||
addi sp, sp, 24
|
||||
mv a0, a1
|
||||
addi s1, s1, 1
|
||||
bne s1, s0, 1b
|
||||
bne s1, s0, 1b # if we haven't retrieved the requested argument, go again
|
||||
|
||||
j 3f
|
||||
|
||||
@@ -68,7 +68,7 @@ fetch_argv:
|
||||
sd s0, 0(sp)
|
||||
|
||||
mv a0, a1
|
||||
call _argv_size
|
||||
call _argv_size # grab the size of the retrieved argument
|
||||
|
||||
ld ra, 8(sp)
|
||||
ld s0, 0(sp)
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
.data
|
||||
cat_buf: .space 4096
|
||||
|
||||
.section .text
|
||||
.globl cat_file
|
||||
|
||||
# Fetch argument count
|
||||
# @param[in] a0: ptr to null-terminated file str.
|
||||
# @param[out] a0: result code.
|
||||
cat_file:
|
||||
mv a1, a0 # since the function takes in a0, move it to a1 for syscall
|
||||
|
||||
li a0, -100 # relative/absolute path
|
||||
li a2, 0 # readonly
|
||||
li a7, 56 # open
|
||||
ecall
|
||||
|
||||
mv t0, a0 # put fd in t0
|
||||
|
||||
1:
|
||||
mv a0, t0
|
||||
la a1, cat_buf
|
||||
li a2, 4096
|
||||
li a7, 63
|
||||
ecall
|
||||
blez a0, 2f
|
||||
mv a2, a0 # move bytes read into bytes needed to write
|
||||
|
||||
li a0, 1
|
||||
la a1, cat_buf
|
||||
li a7, 64
|
||||
ecall
|
||||
j 1b
|
||||
|
||||
2:
|
||||
ret
|
||||
|
||||
|
||||
+11
-8
@@ -12,20 +12,23 @@ _start:
|
||||
|
||||
call save_sp
|
||||
call fetch_argc
|
||||
mv a0, a1
|
||||
li t0, 1
|
||||
sub a0, a0, t0
|
||||
ble a1, t0, 1f # return 1 on no file given
|
||||
|
||||
li a0, 1
|
||||
|
||||
call fetch_argv
|
||||
beqz a0, 1f
|
||||
li a0, 1
|
||||
li a7, 93
|
||||
ecall
|
||||
beqz a0, 2f
|
||||
|
||||
1:
|
||||
li a0, 1
|
||||
li a7, 64
|
||||
ecall
|
||||
li a7, 93
|
||||
ecall # return 1
|
||||
|
||||
2:
|
||||
mv a0, a1
|
||||
call cat_file
|
||||
bnez a0, 1b # error if failed
|
||||
|
||||
li a0, 0
|
||||
li a7, 93
|
||||
|
||||
Reference in New Issue
Block a user