内存被覆盖mips

时间:2013-04-22 15:28:45

标签: memory mips

我遇到了一些MIPS代码的问题,其中数组的前2个元素被覆盖。我接受来自用户的4个不同的输入,每个字节,然后将它们存储在大小为4的'.space'中。当我打印它们时,前2个元素是空白的。我认为这与回车有关,但我不完全确定。以下是我正在使用的内容:

    .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的输出。 enter image description here

1 个答案:

答案 0 :(得分:2)

假设您正在使用垃圾邮件:

$a1的{​​{1}}参数是要读取的字符数。您的代码将syscall 8设置为8,允许读取最多9个字节,但您的缓冲区只是一个字节。缓冲区也可能需要使用$a1对齐到32位边界。