2013-10-10 96 views
2

這只是我第二次處理MIPS組件(即任何類型的組件),所以請溫和。所以我從頭開始爲MIPS提供了一個多功能。這比我想象的要容易 - 我測試了它,它對於一個值是完美的。不幸的是,當數組進入圖片時,我完全失去了。如何遍歷MIPS中的數組?

我甚至不知道如何開始。我感到遲鈍,因爲我不能問一個具體的問題,因爲我不明白這個大想法。我分配的空間,有數組常量,但真的不知道如何:

A.)加載常數值(例如5,2,3,10,7)進入陣列。

B.)讓我的外環繼續。

我的代碼如下,所有我需要的是一個辦法讓我的外循環下去。有任何想法嗎??

/* 
Name: MrPickl3 
Date: October 10, 2013 
Purpose: Program creates a multiply function from scratch. Uses two arrays to 
     test the program. 
*/ 

#include <xc.h> 

. data 

X: .space 80 
Y: .space 80 
N: .space 4 
MAC_ACC .word 0x00000000 

    .text 
    .globl main 

main: 
    li t0, 0x00000000 //i = 0 
    li t1, 0x00000005 //Offset of array 
    li t2, MAC_ACC //Mac_acc (i.e. product register) 
    lw t9, 0(t2) //Refers to MAC_ACC's data 
    la t3, X //Address of X[0] 
    lw t4, 0(t3) //Data of X 
    la t5, Y //Address of Y[0] 
    lw t6, 0(t5) //Data of Y 

loop: 
    addiu t0, t0, 4 //i++ 

//t4 = x[i] 
//t6 = y[i] 
//t7 = counter 

mult: 
    beq t6, 0, loop //Check if y = 0. Go to loop, if so. 
    andi t7, t6, 1 /*We want to know the nearest power of two. 
         We can mask the last bit to 
         test whether or not there is a power of two 
         left in the multiplier.*/ 
    beq t7, 0, shift //If last bit is zero, shift 
    addu t9, t9, t4 //Add multiplicand to product 

shift: 
    sll t3, t3, 1 //Multiply x[i] by 2 
    srl t4, t4, 1 //Multiply y[i] by 2 

lab2_done: 
    j lab2_done 
    nop 

.end main 

X_INPUT: .word 5,2,3,10,7 
Y_INPUT: .word 6,0,8,1,2 
N_INPUT: .word 5 
+0

你是什麼意思的「加載到陣列」?值5,2,3,10,7已經存在於'X_INPUT'數組中,因爲在聲明數組時它們放在那裏。你的意思是「從陣列加載」? – Michael

回答

2

這聽起來像你試圖找出如何訪問數組的第i個元素,當你看到語法lw $t4, 0($t3)。我想你已經知道你可以用lw $t4, 4($t3)得到下一個單詞,但是你一直在如何使這個索引變成動態的。

的技巧是,你不改變立即值(0,4,8等)。而是改變寄存器的內容,在上面的例子中,它指向數組中的第一個字。

下面是我寫的,我CompArch類的分配實現一個簡單的do-while循環,一個數組的成員初始化爲零的代碼。我們被告知$ s0已經加載了數組中第一個單詞的地址。

我拿到了4偏移到我想要的元素,乘(左移兩次),然後添加偏移$ S0(第一個字)。現在,$ t1指向我想要設置的int。我所要做的就是將值($零)存儲在$ t1指向的地址中。

 .text 
partC: # Implement a do-while loop (0-100) 
     add $t0, $zero, $zero # i=0 
Cstart: # Get offset to current int 
     sll $t1, $t0, 2 # *4 
     add $t1, $s0, $zero 
     sw $zero, ($t1) 
     add $t0, $t0, 1 # i++ 
     blt $t0, 100, Cstart # i < 100 
Cdone: add $v0, $zero, 10 # terminate program 
     syscall 

注意語法sw $zero, ($t1)只是一個僞操作爲sw $zero, 0($t1)

希望這有助於與基本概念!