Memory 内存被mips覆盖
我在一些MIPS代码中遇到问题,其中数组的前2个元素被覆盖。我接受来自用户的4个不同输入,每个输入一个字节,然后将它们存储在大小为4的“.space”中。当我把它们全部打印出来时,前两个元素是空白的。我想这和马车返回有关,但我不能完全确定。以下是我的工作内容:Memory 内存被mips覆盖,memory,mips,Memory,Mips,我在一些MIPS代码中遇到问题,其中数组的前2个元素被覆盖。我接受来自用户的4个不同输入,每个输入一个字节,然后将它们存储在大小为4的“.space”中。当我把它们全部打印出来时,前两个元素是空白的。我想这和马车返回有关,但我不能完全确定。以下是我的工作内容: .data msg: .asciiz "Enter the band colors\n" band12: .asciiz "Value bands (first 2 band colors)\n" bandM
.data
msg: .asciiz "Enter the band colors\n"
band12: .asciiz "Value bands (first 2 band colors)\n"
bandM: .asciiz "Multiplier band\n"
bandT: .asciiz "Tolerance band\n"
buffer: .byte '0'
userInput: .space 4
normalized: .word 0
tolerance: .ascii ""
.text
main: li $v0, 4
la $a0, msg
syscall
la $a0, band12
syscall
la $t0, userInput #store the input array in a register
li $v0, 8 #read the first input into buffer
la $a0, buffer
la $a1, 8
syscall
#store the input into the first element of the input array
lb $t1, buffer
sb $t1, ($t0)
#read the second input into buffer
la $a0, buffer
la $a1, 8
syscall
lb $t2, buffer #store the input into the second element of the input array
sb $t2, 1($t0)
#3rd band message
li $v0, 4
la $a0, bandM
syscall
#read in 3rd band
li $v0, 8
la $a0, buffer
la $a1, 8
syscall
#move to 3rd array index
lb $t3, buffer
sb $t3, 2($t0)
#last prompt
li $v0, 4
la $a0, bandT
syscall
#read tolerance band
li $v0, 8
la $a0, buffer
la $a1, 8
syscall
#move to 4th array index
lb $t4, buffer
sb $t4, 3($t0)
li $v0, 11
lb $a0, ($t0)
syscall
lb $a0, 1($t0)
syscall
lb $a0, 2($t0)
syscall
lb $a0, 3($t0)
syscall
jr $ra
li $v0, 10
syscall
这是QtSpim的输出。
假设您正在使用spim:
syscall 8
的$a1
参数是要读取的字符数。您的代码将$a1
设置为8,允许最多读取9个字节,但您的缓冲区仅为一个字节。缓冲区可能还需要与32位边界对齐,使用.align 2
播放一段时间,这基本上就是原因所在。我将系统调用切换到12以读取一个字节,这就解决了这个问题。syscall 8中的\n\0正在覆盖我需要的数组部分。