How can I loop until the end of file without hardcoding the the value of $t0
addi $t0, $t0,1000
loop: beq $t0, $0, loopend
#this code reads in a character from the file
jal readchar
#print the character to the console
li $v0,11
move $a0,$t1
syscall
addi $t0,$t0,-1
j loop
loopend:
readchar: li $v0,14
move $a0,$s6
la $a1,inchar
li $a2,1
syscall
lb $t1,inchar
jr $ra
Because when I use $a2 it doesn't work, but it says here http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html
that read from file
li $v0,14
$a0 = file descriptor
$a1 = address of input buffer
$a2 = maximum number of characters to read $v0 contains number of characters
And when I use this as substitute for the hard coded ($ addi $t0, $t0,1000) $a2 is still 0
li $v0,14
move $t0,$a2
sycall
Thank you!
I'm not sure what purpose moving $a2
to $t0
before the system call would serve. $a2
is an argument to the system call where you specify the maximum number of characters to read. The return value - ie the number of characters actually read - is available only after the system call, and will be in register $v0
.
So if you put 1 in $a2
before the system call and $v0
contains zero after the system call, then you've probably reached the end of the file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.