我有問題要在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模擬器上運行的代碼,結果並不如預期,我想一些建議從你明白問題出在哪裏。
我認爲問題是在調用兩次的子例程中,在讀取第二個數組的元素並覆蓋它們之前,不會保存第一個數組的元素。
我認爲解決這個問題需要將這個元素保存在一個堆棧中,用作支持,但是我不知道如何做得很好。
你從哪裏得到C++代碼?它指的是一個名爲'vect'的變量/參數,但沒有聲明這樣的變量/參數。我的猜測是它應該說'v'而不是'vect'。無論如何,你的程序集版本似乎忽略了你傳遞它的任何參數,所以它並不真正遵循C++版本。 – Michael
還有更多的拼寫錯誤,例如'vet2',其中可能有'vect2'的意思。你能解決這些問題嗎,在模擬器上重新運行它,然後複製/粘貼你在上面問題中運行的內容? – wimh
@Michael C++代碼已被作爲如何設置彙編代碼的例子。 – linuxterm