2013-04-22 74 views
0

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

.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

回答

2

假设你正在使用SPIM:

$a1论据syscall 8是要读取的字符数。您的代码将$a1设置为8,允许读取最多9个字节,但您的缓冲区只有一个字节。使用.align 2可能还需要将缓冲区对齐到32位边界。

+0

玩了一下,这基本上是原因。我将系统调用切换到12来读取一个字节并修复它。系统调用8中的\ n \ 0覆盖了我需要的数组部分。 – MGZero 2013-04-23 01:57:32