2011-09-24 576 views
2

此代碼即將從數組中找到最大元素,我想將此代碼轉換爲MIPS彙編代碼,任何人都可以幫助我......或者只是告訴我如何在MIPS中初始化一個數組。將C++轉換爲MIPS彙編

void max_array() 
{ 
    int a[10]={2,3,421,4,32,4,3,1,4,5},max; 
    for(int i=0;i<9;i++) 
    { 
     cout<<a[i]; 
    } 
    max=a[0]; 
    for(int j=1;j<9;j++) 
    { 
     if (max<a[j]) 
     { 
      max=a[j]; 
     } 
    } 
    return max; 

} 
+11

難道你們就不能只是一個MIPS C語言編譯器編譯它,並期待在輸出?在GCC中,你可以使用'-S' –

+0

@PeterAlexander -S得到程序集輸出,將產生x86程序集,我想 – niceman

回答

3

下面是一個例子

 .data 
array1:  .space 12    # declare 12 bytes of storage to hold array of 3 integers 
     .text 
__start: la $t0, array1  # load base address of array into register $t0 
     li $t1, 5     # $t1 = 5 ("load immediate") 
     sw $t1, ($t0)    # first array element set to 5; indirect addressing 
     li $t1, 13     # $t1 = 13 
     sw $t1, 4($t0)   # second array element set to 13 
     li $t1, -7     # $t1 = -7 
     sw $t1, 8($t0)   # third array element set to -7 
     done 
+0

只是想知道:有一個原因,數組不能在數據部分中聲明,只是指向?無論如何,它在C代碼中都是有效的... – cHao

+0

你說得對。我相信你也可以做'array1:.word'2','3','421''等 –

3
.data # variable decleration follow this line 

array1: .word 2, 3, 421, 4, 32, 4, 3, 1, 4, 5 #10 element array is declared 
max: .word 2 

la $t0, array1   #load base address of array1 
main:      #indicated the start of the code 
#to access an array element ($t0), 4($t0), 8($t0).......... 
+0

而存儲在一個字節中的421是...? –

+0

是的,這將是一個問題,所以把它改爲WORD –