2013-04-22 72 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