来自键盘的 MIPS 汇编输入失败
MIPS Assembly input from keyboard fail
我正在尝试编写一个 MIPS 代码来打开一个文件,读取它并在用户从键盘输入输入(换行)时打印出一行,直到文本文档结束。但是程序以某种方式跳过了用户输入并且不等待输入。
代码:
.data
buffer: .space 4
fin: .asciiz "input.txt" # filename for input
myName: .asciiz "\nTheUserName"
.text
openfile:
li $v0, 13 # system call for open file
la $a0, fin # board file name
li $a1, 0 # Flag for reading
li $a2, 0
syscall
add $s7, $v0, [=10=] # save the file descriptor
read:
#read from file code goes here
li $v0, 14 # system call for read from file
move $a0, $s7 # file descriptor
la $a1, buffer # address of buffer to which to read
li $a2, 1 # hardcoded buffer length
syscall
beq $v0,[=10=],exit #exit if EOF is seen
print:
#print file code goes here
la $a0, buffer # address of string to be printed
li $v0, 4 # print string
syscall
beq $a0,0x0A, input #check whether the character read from the file is ‘\n’ (new line) or not
b read
input:
#get input code goes here
li $v0, 12
syscall
beq $v0, 0x0A, read #check whether the character read from the keyboard is ‘\n’ (new line) or not
b input
exit:
# Close the file code goes here
li $v0, 16 # system call for close file
move $a0, $s7 # file descriptor to close
syscall # close file
name:
# Print your name and surname on the screen as a string as the last line
li $v0, 4
la $a0, myName
syscall
输入文件:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris tristique dui at ornare pretium.
Phasellus ac vehicula libero. Fusce scelerisque dolor ipsum, vitae suscipit sapien dictum quis.
Praesent sed libero tellus. In vitae laoreet massa.
Duis vehicula fringilla orci, et iaculis nulla malesuada in.
Nam at cursus nisi. Duis convallis magna quis dolor aliquam ullamcorper.
Nunc sollicitudin a leo in placerat. Cras id pretium ligula, sed facilisis massa.
Curabitur semper ultricies nulla non lacinia. Nunc a fermentum ex, nec egestas ligula.
Quisque varius libero sed rhoncus venenatis. Cras pulvinar ultrices dignissim.
好的,我修正了你的程序。
主要问题是文件输入的换行符测试不正确。您正在比较 $a0
[这是缓冲区 地址 ] 与换行符 而不是 缓冲区的第一个字符。
还有另外两个错误。
我已经对错误和修复进行了注释[请原谅不必要的样式清理]:
.data
buffer: .space 4
fin: .asciiz "input.txt" # filename for input
myName: .asciiz "\nTheUserName"
.text
openfile:
li $v0,13 # system call for open file
la $a0,fin # board file name
li $a1,0 # Flag for reading
li $a2,0
syscall
add $s7,$v0,[=10=] # save the file descriptor
# NOTE/BUG: this line was missing -- actually it may be optional -- try with
# and without it to see the effect:
b input # pause for user input _before_ 1st line
read:
# read from file code goes here
li $v0,14 # system call for read from file
move $a0,$s7 # file descriptor
la $a1,buffer # address of buffer to which to read
li $a2,1 # hardcoded buffer length
syscall
beq $v0,[=10=],exit # exit if EOF is seen
print:
# print file code goes here
la $a0,buffer # address of string to be printed
# NOTE/BUG: add the following line:
sb $zero,1($a0) # ensure we have EOS at end of string
li $v0,4 # print string
syscall
# NOTE/BUG: add the following line -- it was the _real_ bug:
lb $a0,0($a0) # get buffer char value
beq $a0,0x0A,input # is file char newline?
b read
input:
# get input code goes here
li $v0,12
syscall
beq $v0,0x0A,read # is keyboard char newline?
b input
exit:
# Close the file code goes here
li $v0,16 # system call for close file
move $a0,$s7 # file descriptor to close
syscall # close file
name:
# Print your name and surname on the screen as a string as the last line
li $v0,4
la $a0,myName
syscall
li $v0,10
syscall
我正在尝试编写一个 MIPS 代码来打开一个文件,读取它并在用户从键盘输入输入(换行)时打印出一行,直到文本文档结束。但是程序以某种方式跳过了用户输入并且不等待输入。
代码:
.data
buffer: .space 4
fin: .asciiz "input.txt" # filename for input
myName: .asciiz "\nTheUserName"
.text
openfile:
li $v0, 13 # system call for open file
la $a0, fin # board file name
li $a1, 0 # Flag for reading
li $a2, 0
syscall
add $s7, $v0, [=10=] # save the file descriptor
read:
#read from file code goes here
li $v0, 14 # system call for read from file
move $a0, $s7 # file descriptor
la $a1, buffer # address of buffer to which to read
li $a2, 1 # hardcoded buffer length
syscall
beq $v0,[=10=],exit #exit if EOF is seen
print:
#print file code goes here
la $a0, buffer # address of string to be printed
li $v0, 4 # print string
syscall
beq $a0,0x0A, input #check whether the character read from the file is ‘\n’ (new line) or not
b read
input:
#get input code goes here
li $v0, 12
syscall
beq $v0, 0x0A, read #check whether the character read from the keyboard is ‘\n’ (new line) or not
b input
exit:
# Close the file code goes here
li $v0, 16 # system call for close file
move $a0, $s7 # file descriptor to close
syscall # close file
name:
# Print your name and surname on the screen as a string as the last line
li $v0, 4
la $a0, myName
syscall
输入文件:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris tristique dui at ornare pretium.
Phasellus ac vehicula libero. Fusce scelerisque dolor ipsum, vitae suscipit sapien dictum quis.
Praesent sed libero tellus. In vitae laoreet massa.
Duis vehicula fringilla orci, et iaculis nulla malesuada in.
Nam at cursus nisi. Duis convallis magna quis dolor aliquam ullamcorper.
Nunc sollicitudin a leo in placerat. Cras id pretium ligula, sed facilisis massa.
Curabitur semper ultricies nulla non lacinia. Nunc a fermentum ex, nec egestas ligula.
Quisque varius libero sed rhoncus venenatis. Cras pulvinar ultrices dignissim.
好的,我修正了你的程序。
主要问题是文件输入的换行符测试不正确。您正在比较 $a0
[这是缓冲区 地址 ] 与换行符 而不是 缓冲区的第一个字符。
还有另外两个错误。
我已经对错误和修复进行了注释[请原谅不必要的样式清理]:
.data
buffer: .space 4
fin: .asciiz "input.txt" # filename for input
myName: .asciiz "\nTheUserName"
.text
openfile:
li $v0,13 # system call for open file
la $a0,fin # board file name
li $a1,0 # Flag for reading
li $a2,0
syscall
add $s7,$v0,[=10=] # save the file descriptor
# NOTE/BUG: this line was missing -- actually it may be optional -- try with
# and without it to see the effect:
b input # pause for user input _before_ 1st line
read:
# read from file code goes here
li $v0,14 # system call for read from file
move $a0,$s7 # file descriptor
la $a1,buffer # address of buffer to which to read
li $a2,1 # hardcoded buffer length
syscall
beq $v0,[=10=],exit # exit if EOF is seen
print:
# print file code goes here
la $a0,buffer # address of string to be printed
# NOTE/BUG: add the following line:
sb $zero,1($a0) # ensure we have EOS at end of string
li $v0,4 # print string
syscall
# NOTE/BUG: add the following line -- it was the _real_ bug:
lb $a0,0($a0) # get buffer char value
beq $a0,0x0A,input # is file char newline?
b read
input:
# get input code goes here
li $v0,12
syscall
beq $v0,0x0A,read # is keyboard char newline?
b input
exit:
# Close the file code goes here
li $v0,16 # system call for close file
move $a0,$s7 # file descriptor to close
syscall # close file
name:
# Print your name and surname on the screen as a string as the last line
li $v0,4
la $a0,myName
syscall
li $v0,10
syscall