2014-02-11 63 views
1

我有問題要在C++程序翻譯成MIPS彙編兩次:子程序呼籲MIPS

原來的C++代碼,鑑於教授爲實例

float vect1[64], vect2[64]; 
int num1,num2; 
float average; 
int numag; 
void main() { 
    int i, count; 
    float sum; 
    float fnum; 
    read_vect_float(vect1,num1); 
    read_vect_float(vect2,num2); 
    sum=0.0; 
    for(i=0;i<num1;i++) 
     sum=sum+vect1[i]; 
    fnum=(float)num; 
    sum=sum/fnum; 
    average=sum; 

    cout<< "The average is: " << average; 
    cout << "\n"; 

    count=0; 
    for(i=0;i<num2;i++) 
     if(vect2[i]>average) 
      count=count+1; 
    numag=count; 
    cout << "In vector 2 there are "; 
    cout << numag; 
    cout << " elements major of "; 
    cout << average; 
    cout << "\n"; 
} 

void read_vect_float(float v[],int &n) { 
    int i; 
    float x; 

    cout << "Number of elements: "; 
    cin >> n; 
    for(i=0;i<n;i++) { 
     cout=<< "Element: " ; 
     cin >> x; 
     vect[i]=x; 
    } 
} 

C++代碼審查的ME,所以你可以運行

#include <iostream> 

using namespace std; 

void read_vect_float(float v[],int &n); 

float vect[64],vect1[64], vect2[64]; 
int num,num1,num2; 
float average; 
int numag; 

int main() { 
    int i, count; 
    float sum; 
    float fnum; 
    read_vect_float(vect1,num1); 
    read_vect_float(vect2,num2); 
    sum=0.0; 
    for(i=0;i<num1;i++) 
     sum=sum+vect1[i]; 
    fnum=(float)num; 
    sum=sum/fnum; 
    average=sum; 

    cout<< "The average is: " << average; 
    cout << "\n"; 

    count=0; 
    for(i=0;i<num2;i++) 
     if(vect2[i]>average) 
      count=count+1; 
    numag=count; 
    cout << "In vector 2 there are "; 
    cout << numag; 
    cout << " elements major of "; 
    cout << average; 
    cout << "\n"; 
    return 0; 
} 

void read_vect_float(float v[],int &n) { 
    int i; 
    float x; 

    cout << "Number of elements: "; 
    cin >> n; 
    for(i=0;i<n;i++) { 
     cout << "Element: " ; 
     cin >> x; 
     vect[i]=x; 
    } 
} 

該程序的目的是讀取輸入兩個浮點數組的元素,計算存儲的項的平均值,並找出第二個數組的多少個元素大於第一個數組。

這是我的MIPS彙編代碼:

.data 
vect: .float 
.space 256 
vect1: .float 
.space 256 
vect2: .float 
.space 256 
num1: .word 
.space 4 
num2: .word 
.space 4 
average: .float 
.space 4 
numag: .word 
.space 4 
i: .word 
.space 4 
count: .word 
.space 4 
sum: .float 
.space 4 
fnum: .float 
.space 4 
str1: .asciiz "The average is: " 
.align 2 
str2: .asciiz "\n" 
.align 2 
str3: .asciiz "In vector 2 there are " 
.align 2 
str4: .asciiz " element major of " 
.align 2 
x: .float 
.space 4 
str5: .asciiz "Number of elements: " 
.align 2 
n: .word 
.space 4 
str6: .asciiz "Element: " 
.align 2 
.text 
.globl main 
main: 
# read_vect_float(vect1,num1); 
lwc1 $f12,vect1 
lw $a0,num1 
jal read_vect_float 
# read_vect_float(vect2,num2); 
lwc1 $f13,vect2 
lw $a1,num2 
jal read_vect_float 
# sum=0.0; 
mtc1 $zero,$f0 
cvt.s.w $f0,$f0 
# for(i=0;i<num1;i++) 
li $t0,0 
for3: 
# sum=sum+vect1[i]; 
sll $t0,$t0,2 
lwc1 $f1,vect1($t0) 
add.s $f2,$f2,$f1 

addi $t0,$t0,1 
blt $t0,$t1,for3 
# fnum=(float)num; 
mtc1 $t1,$f3 
cvt.s.w $f3,$f3 
# sum=sum/fnum; 
div.s $f2,$f2,$f3 
# average=sum; 
swc1 $f2,average 
# cout<< "The average is: " << average; 
li $v0,4 
la $a0,str1 
syscall 

li $v0,2 
lwc1 $f12,average 
syscall 
# cout << "\n"; 
li $v0,4 
la $a0,str2 
syscall 
# count=0; 
li $t2,0 
sw $t2,count 
# for(i=0;i<num2;i++) 
li $t0,0 
for2: 
# if(vect2[i]>average) 
#ble $f3,$f2,next 
c.le.s $f2,$f3 
bc1t next 
# count=count+1; 
addi $t2,$t2,1 
sll $t0,$t0,2 
sw $t2,numag 
addi $t1,$t1,1 
blt $t0,$t1,for2 
next: 
# cout << "In vector 2 there are "; 
li $v0,4 
la $a0,str3 
syscall 
# cout << numag; 
li $v0,1 
lw $a0,numag 
syscall 
# cout << " elements major of "; 
li $v0,4 
la $a0,str4 
syscall 
# cout << average; 
li $v0,2 
lwc1 $f12,average 
syscall 
# cout << "\n"; 
li $v0,4 
la $a0,str2 
syscall 

li $v0,10 
syscall 

read_vect_float: 
# cout << "Number of elements: "; 
li $v0,4 
la $a0,str5 
syscall 
# cin >> n; 
li $v0,5 
syscall 
sw $v0,n 
lw $t5,n 
# for(i=0;i<n;i++) 
li $t0,0 
for: 
# cout=<< "Element: " ; 
li $v0,4 
la $a0,str6 
syscall 
# cin >> x; 
li $v0,6 
syscall 
swc1 $f0,x 
# vect[i]=x; 
sll $t0,$t0,2 
swc1 $f0,vect($t0) 

addi $t0,$t0,1 
blt $t0,$t5,for 

jr $ra 

但是,當我在MARS MIPS模擬器上運行的代碼,結果並不如預期,我想一些建議從你明白問題出在哪裏。

我認爲問題是在調用兩次的子例程中,在讀取第二個數組的元素並覆蓋它們之前,不會保存第一個數組的元素。

我認爲解決這個問題需要將這個元素保存在一個堆棧中,用作支持,但是我不知道如何做得很好。

+0

你從哪裏得到C++代碼?它指的是一個名爲'vect'的變量/參數,但沒有聲明這樣的變量/參數。我的猜測是它應該說'v'而不是'vect'。無論如何,你的程序集版本似乎忽略了你傳遞它的任何參數,所以它並不真正遵循C++版本。 – Michael

+0

還有更多的拼寫錯誤,例如'vet2',其中可能有'vect2'的意思。你能解決這些問題嗎,在模擬器上重新運行它,然後複製/粘貼你在上面問題中運行的內容? – wimh

+0

@Michael C++代碼已被作爲如何設置彙編代碼的例子。 – linuxterm

回答

0

我設法解決這個運動是這樣的:

.data 
vect1: .float 
.space 256 
vect2: .float 
.space 256 
num1: .word 
.space 4 
num2: .word 
.space 4 
average: .float 
.space 4 
numag: .word 
.space 4 
i: .word 
.space 4 
count: .word 
.space 4 
sum: .float 
.space 4 
fnum: .float 
.space 4 
str1: .asciiz "The average is: " 
.align 2 
str2: .asciiz "\n" 
.align 2 
str3: .asciiz "In vector 2 there are " 
.align 2 
str4: .asciiz " elements major of " 
.align 2 
x: .float 
.space 4 
str5: .asciiz "Number of elements: " 
.align 2 
str6: .asciiz "Element: " 
.align 2 
.text 
.globl main 
main: 
# read_vect1_float(vect1,num1); 
lwc1 $f12,vect1 
lw $a0,num1 
jal read_vect1_float 
# leggi_vet2_float(vect2,num2); 
lwc1 $f13,vect2 
lw $a1,num2 
jal read_vect2_float 
# sum=0.0; 
mtc1 $zero,$f1 
cvt.s.w $f1,$f1 
# for(i=0;i<num1;i++) 
li $t0,0 
for: 
# sum=sum+vect1[i]; 
move $t3,$t0 
sll $t3,$t3,2 
lwc1 $f2,vect1($t3) 
add.s $f1,$f1,$f2 

addi $t0,$t0,1 
blt $t0,$t1,for 
# fnum=(float)num1; 
mtc1 $t1,$f3 
cvt.s.w $f3,$f3 
# sum=sum/fnum; 
div.s $f1,$f1,$f3 
# average=sum; 
swc1 $f1,average 
# cout<< "The average is: " << average; 
li $v0,4 
la $a0,str1 
syscall 

li $v0,2 
lwc1 $f12,average 
syscall 
# cout << "\n"; 
li $v0,4 
la $a0,str2 
syscall 
# count=0; 
li $t4,0 
sw $t4,count 
# for(i=0;i<num2;i++) 
li $t0,0 
for2: 
move $t3,$t0 
sll $t3,$t3,2 
lwc1 $f2,vect2($t3) 
# if(vect2[i]>average) 
c.le.s $f2,$f1 
bc1t next 
# count=count+1; 
addi $t4,$t4,1 

addi $t0,$t0,1 
blt $t0,$t2,for2 
next: 
# numag=count; 
sw $t4,numag 
# cout << "In vector 2 there are "; 
li $v0,4 
la $a0,str3 
syscall 
# cout << numag; 
li $v0,1 
lw $a0,numag 
syscall 
# cout << " elements major of " 
li $v0,4 
la $a0,str4 
syscall 
# cout << average; 
li $v0,2 
lwc1 $f12,average 
syscall 
# cout << "\n"; 
li $v0,4 
la $a0,str2 
syscall 

li $v0,10 
syscall 

read_vect1_float: 
# cout << "Number of elements: "; 
li $v0,4 
la $a0,str5 
syscall 
# cin >> num1; 
li $v0,5 
syscall 
sw $v0,num1 
lw $t1,num1 
# for(i=0;i<num1;i++) 
li $t0,0 
forsub1: 
# cout=<< "Element: " ; 
li $v0,4 
la $a0,str6 
syscall 
# cin >> x; 
li $v0,6 
syscall 
swc1 $f0,x 
# vect1[i]=x; 
move $t4,$t0 
sll $t4,$t4,2 
swc1 $f0,vect1($t4) 

addi $t0,$t0,1 
blt $t0,$t1,forsub1 

jr $ra 

read_vect2_float: 
# cout << "Number of elements: "; 
li $v0,4 
la $a0,str5 
syscall 
# cin >> num2; 
li $v0,5 
syscall 
sw $v0,num2 
lw $t2,num2 
# for(i=0;i<num2;i++) 
li $t0,0 
forsub2: 
# cout=<< "Element: " ; 
li $v0,4 
la $a0,str6 
syscall 
# cin >> x; 
li $v0,6 
syscall 
swc1 $f0,x 
# vect2[i]=x; 
move $t4,$t0 
sll $t4,$t4,2 
swc1 $f0,vect2($t4) 

addi $t0,$t0,1 
blt $t0,$t2,forsub2 

jr $ra 

我想知道,如果運動爲固定順利或者你可以做一些改進。

+0

請描述問題是什麼以及如何解決問題。按照目前的寫法,這個問題/答案沒有任何長期價值。 –