我有问题将这个程序用C ++翻译成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 ++代码,所以你可以运行
#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;
}
}
程序的目的是读取两个float数组的输入元素,计算存储项的平均值,并找出第二个数组的元素大于第一个数组的数量。
这是我的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 :(得分: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
我想知道固定的运动是否顺利,或者你可以做些改进。