MIPS:将字符串输入存储到内存中

MIPS:将字符串输入存储到内存中,mips,heap-memory,Mips,Heap Memory,以下是MIPS程序请求字符串输入,然后打印输出的最低工作示例: .data enterString: .asciiz "Please enter a string: " theString1: .asciiz "The string is" buffer: .space 100 .text # Allocate memory for an array of strings addi $v0, $zero, 9 # Syscall 9: A

以下是MIPS程序请求字符串输入,然后打印输出的最低工作示例:

.data
    enterString:    .asciiz "Please enter a string: "
    theString1: .asciiz "The string is"
    buffer: .space  100
.text
    # Allocate memory for an array of strings
    addi $v0, $zero, 9      # Syscall 9: Allocate memory
    addi  $a0, $zero, 4     # number of bytes = 4 (one word)
    syscall                   # Allocate memeory
    add  $s1, $zero, $v0         # $s1 is the address of the array of strings
    add  $s3, $zero, $s1         # $s3 is the temporary address of the array of strings
    #Ask user for input
    add  $v0, $zero, 4      # Syscall 4: Print string
    la   $a0, enterString      # Set the string to print to enterString
    syscall                   # Print "Please enter..."
   jal  _readString           # Call _readString function
   #Store it in memory
    sw   $v0, 0($s3)            # Store the address of a string into the array of strings
    add  $s3, $zero, $s1         # $s3 is the temporary address of the array of strings
    addi $v0, $zero, 4      # Syscall 4: Print string
    la   $a0, theString1       # Set the string to print to theString1
    syscall                   # Print "The string..."
    lw   $a0, 0($s3)            # Set the address by loading the address from the array of string
    syscall                   # Print the string
    j done
#Readstring: read the string, store it in memory. NOT ALLOWED TO CHANGE ANY OF THE ABOVE!!!!!!!!!
_readString:
    addi $v0, $zero, 8 #Syscall 8: Read string
    la $a0, buffer #load byte space into address
    addi $a1, $zero, 20 # allot the byte space for string
    syscall
    jr   $ra
done:

我收到一个错误,即第24行中的
错误:0x00400044处的运行时异常:地址超出范围0x00000008
。(第24行,作为参考,是
readString
方法之前的最后一个
syscall
。)我不允许修改上面的代码
\u readString:
;换句话说,我只编写并实现
\u readString
函数的代码。我相信这个错误与内存分配有关,尽管我不确定具体的问题是什么。感谢您的帮助。谢谢。

根据建议,第18行的命令是错误的。它应该改为
sw$a0,0($s3)
,而不是
sw$v0,0($s3)
。提供的代码中有错误。

我不确定您正在查看哪个系统调用文档,但system call 8在
$v0
中没有返回任何内容。它在内存中存储的字符指向
$a0
@Michael你说得对!我把它改成了
$a0
,效果很好。我会回答这个问题,并在回答中包括你的答案。