문제

I have problem to translate this program in C++ to MIPS assembly:

ORIGINAL C++ CODE AS GIVEN BY PROFESSOR AS EXAMPLE

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++ CODE REVIEWED BY ME, SO THAT YOU CAN RUN

#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;
    }
}

Purpose of the program is to read the input elements of two float arrays, compute the average of the stored items and find out how many elements of second array is greater than the first array.

This is my MIPS assembly code:

.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

But when I run the code on MARS MIPS simulator the result is not as expected and I wanted some advice from you to understand where is the problem.

I think the problem is in the subroutine called twice and will not be save elements of first array before going to read elements of the second array and overwrite them.

I think that to solve the problem need to save this elements in a stack, used as a support, but I do not know how to do it well.

도움이 되었습니까?

해결책

I managed to solve the exercise this way:

.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

I would like to know if the exercise as fixed goes well or you can do some improvement.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top