I'm trying to record in a file an user input, using assembly.
I'm working with this code but when the file is created, the input isn't recorded on the file correctly. Someone can help me with this? Here is my code:
.data
file1: .asciiz "file1.txt"
prompt: .asciiz "User entry\n"
buffer: .space 45
.text
la $a0,prompt
li $v0,4
syscall
li $v0, 8
li $a1, 454
syscall
move $s1, $v0
j writeFile1
writeFile1:
li $v0, 13
la $a0, file1
li $a1, 1
li $a2, 0
syscall
move $s6, $v0
#write
li $v0, 15
move $a0, $s6
la $a1, buffer
li $a2, 45
syscall
#close
li $v0, 16
move $a0, $s6
syscall
j exit
exit: li $v0, 10
syscall
Your user input call did not setup the pointer to
buffer
. So, it would read intoprompt
instead. Also, the given length was454
instead of the [intended]45
. Further, this syscall does not return a length, so savingv0
did nothing.After fixing the above the program works. But, it would write a fixed length output so there were binary zeroes at the end.
I've added some code to calculate the string length (e.g. like
strlen
). I've also added sidebar comments to most lines. I highly recommend this for any asm. Anyway, here's the corrected program [please pardon the gratuitous style cleanup]: