2012-11-16 44 views
0

我想將下面的C代碼翻譯成MIPS彙編語言,但我有點理解它,但是,我失去了第一行的等價物在彙編中。MIPS:將C代碼翻譯爲程序集

int ary[3] = {2,3,4}; 

我很感激,如果有人可以看看我的C大會'翻譯',並驗證我在正確的軌道上。

C代碼

int ary[3] = {2,3,4}; 
int i=0; 

//loop to double array values 
for(i=0; i < 3; i++){ 
    ary[i] = ary[i]*2; 
} 

我試了一下:

add $t0, $s0, $zero #get base address of the array 'ary' (dont understand this part) 
addi $t1, baseAddress, 8 #cut off point to stop the loop; array[2] 
addi $t1, $zero, $zero #initialize i=0 


Start: 
lw $t2, base(offset) 
sll $t2, $t0, 1 #mutiply $t2 by 2 
sw $t2, base(offset) 
addi $t0, $t0, 4 # Increment the address to the next element 
bne $t0, $t1, Start # $t0 will keep increasing until reaches stopping point $t1 
Exit: 

回答

2

如果這是一個本地數組,你在棧上爲其分配空間,然後從代碼初始化。 C代碼的可能ASM翻譯可能看起來像:

addi $sp, $sp, -12  # allocate space for 3 words, $sp is now the address of the array 
    addi $t0, $zero, 2 
    sw $t0, ($sp)   # ary[0]=2 
    addi $t0, $zero, 3 
    sw $t0, 4($sp)   # ary[1]=3 
    addi $t0, $zero, 4 
    sw $t0, 8($sp)   # ary[2]=4 

    addi $t0, $zero, 0  # initialize i=0 

Start: 
    sll $t1, $t0, 2  # i*4 for element size 
    add $t1, $t1, $sp  # add base address of array, $t1 is now &ary[i] 
    lw $t2, ($t1)   # load ary[i] 
    sll $t2, $t2, 1  # mutiply by 2 
    sw $t2, ($t1)   # store back to ary[i] 
    addi $t0, $t0, 1  # i++ 
    addi $t1, $t0, -3  # check if i<3 by doing (i-3)<0 
    bltz $t1, Start 
    addi $sp, $sp, 12  # free the array 

你的彙編代碼正在採取稍微不同的方法,C版看起來會是這樣的:

int* end = &ary[3]; 
for(int* ptr = ary; ptr != end; ptr++) 
{ 
    *ptr = *ptr * 2; 
} 

和固定ASM版本因爲這是:

addi $t1, $sp, 12  # end=&ary[3] 
    addi $t0, $sp, 0  # ptr=ary 

Start: 
    lw $t2, ($t0)   # load ary[i] 
    sll $t2, $t2, 1  # mutiply by 2 
    sw $t2, ($t0)   # store back to ary[i] 
    addi $t0, $t0, 4  # ptr++ (note it is incremented by 4 due to element size) 
    bne $t0, $t1, Start # ptr!=end 
+0

您好,非常感謝您的回覆,我很感激。我想知道爲什麼沒有結束語。另外,爲什麼lw和sw指令中沒​​有基地址? – AnchovyLegend

+1

不確定你在說什麼結束語句,也許是'jr $ ra'?由於你的C代碼不是一個完整的函數或程序,我只是以類似的方式提供了asm代碼。至於缺少的基地址:這是因爲地址已經通過添加'$ sp'(數組地址)來計算,所以基數只是0.您可以根據需要寫出。請注意,基址不能是另一個寄存器,「lw $ t2,$ sp($ t1)'是非法的。 – Jester

+0

我很欣賞這個:)謝謝! – AnchovyLegend

0

有在你的代碼很多錯誤

addi $t1, baseAddress, 8 #cut off point to stop the loop; array[2] 
addi $t1, $zero, $zero #initialize i=0 

在第一行中,除非baseAddress是一個寄存器,否則不存在這樣的指令。第二行應該是一個add,不addi因爲$零不立即

Start: 
lw $t2, base(offset) 
sll $t2, $t0, 1 #mutiply $t2 by 2 
sw $t2, base(offset) 

以上線路有問題了。你剛剛加載一個字到$ t2,然後馬上將另一個值存儲到$ t2,所以以前的加載是沒有意義的

-2
#include <iostream> 
    using namespace std; 

    //prototypes 

    int maxIs (int *x, int n); 
    int minIs (int *x, int n); 
    void avgIs (int *x, int n, int *theAvg, int *theRem); 

    int main(void) 
    { 
    int n = 8; 
    int x[] = {1,2,3,4,5,6,7,8}; 
    int theMax, theMin, theAvg, theRem; 

    theMax = maxIs(x,n); 
    theMin = minIs(x,n); 
    avgIs(x,n,&theAvg, &theRem); 

    cout << "max = " << theMax << "\n"; 
    cout << "min = " << theMin << "\n"; 
    cout << "avg = " << theAvg << " " << theRem << "/" << n << "\n"; 
    cout << "Bye!\n"; 
    } 

    //functions 

int maxIs (int *x, int n) 
{ 
int i; 
int theMax = 0; 
for (i=0; i<n; i++) 
{ 
    if (x[i]>theMax) theMax =x[i]; 
} 
return (theMax); 
} 

int minIs (int *x, int n) 
{ 
int i; 
int theMin = 0x7FFF; 
for (i=0; i<n; i++) 
{ 
    if (x[i]>theMin) theMin =x[i]; 
} 
return (theMin); 
} 

void avgIs (int *x, int n, int *theAvg, int *theRem) 
{ 
int i; 
int theSum = 0; 
for (i=0; i<n; i++) 
{ 
    theSum += x[i]; 
} 
*theAvg = theSum /n; 
*theRem = theSum %n; 
} 
+1

請向您的代碼專用答案添加一些解釋... – Werner

+0

您是否在錯誤的地方發佈了這條消息?這與此無關,並且不回答這個問題。 – Blastfurnace