我正在尝试使用程序集在文件中记录用户输入。
我正在使用此代码但是在创建文件时,输入未正确记录在文件中。有人可以帮我这个吗?这是我的代码:
.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
答案 0 :(得分:3)
您的用户输入调用未设置指向buffer
的指针。因此,它会读入prompt
。此外,给定长度为454
,而不是[预期] 45
。此外,这个系统调用不返回一个长度,因此保存v0
什么也没做。
修好上述程序后即可运行。但是,它会写一个固定长度的输出,所以最后有二进制零。
我添加了一些代码来计算字符串长度(例如像strlen
)。我还在大多数行添加了侧栏评论。我强烈建议任何asm。无论如何,这里有更正的程序[请原谅无偿的风格清理]:
.data
file1: .asciiz "file1.txt"
prompt: .asciiz "User entry\n"
buffer: .space 46
.eqv BUFMAX 45 # usable buffer length (one less)
.text
# prompt user
la $a0,prompt # prompt string
li $v0,4 # puts syscall number
syscall
# read user string
li $v0,8
la $a0,buffer # FIXME -- this was missing
li $a1,BUFMAX # FIXME -- this was 454
syscall
###move $s1,$v0 # FIXME -- does nothing v0 is not length
###li $s1,BUFMAX # use fixed length
###j writeFile1 # would do write with zeroes in file
# calculate string length (e.g. like strlen)
move $s1,$a0 # point to buffer start
getlen:
lb $t0,0($s1) # get byte -- is it zero?
addi $s1,$s1,1 # advance pointer
bne $t0,$zero,getlen # no, loop
sub $s1,$s1,$a0 # get length from end pointer
subi $s1,$s1,1 # compensate for preincrement
writeFile1:
# open the output file
li $v0,13 # open syscall number
la $a0,file1 # filename
li $a1,1 # open for writing
li $a2,0 # open mode
syscall
move $s6,$v0 # save fildes number
# write
li $v0,15 # write syscall number
move $a0,$s6 # get fildes number
la $a1,buffer # get buffer pointer
move $a2,$s1 # get buffer length
syscall
# close
li $v0,16
move $a0,$s6 # get fildes number
syscall
j exit
exit:
li $v0,10 # exit syscall number
syscall