我从学校做一个练习,将C ++代码转换为MIPS汇编语言。我在某些功能或过程中的参数有一些问题。代码是对最多5个成员的数组进行排序,此练习在c ++中具有三个功能。第一个是创建一个函数,该函数通过输入将数组中的数字相加,这是一个叶过程,而另外两个函数是对数组进行排序。这两个函数是汇编语言中的嵌套过程,我无法真正理解如何实现它们,我不理解的是理解如何将5个参数发送到另一个过程,例如下面的代码。
#include <iostream>
#include <string>
using namespace std;
int addElements(int a[])
{
int n;
cout << "Enter the size of array: ";
cin >> n;
cout << "\nAdd elements one by one: \n";
for (int i = 1; i <= n; i++) {
cin >>a[i];
}
void secondFunction(int p, int n, int &min, int a[], int &loc)
{
for (int k = p + 1; k <= n; k++)
{
if (min > a[k])
{
min = a[k];
loc = k;
}
}
}
void firstFunction(int a[], int n)
{
int min, loc, tmp;
for (int p = 1; p <= n - 1; p++) // Loop for Pass
{
min = a[p]; // Element Selection
loc = p;
secondFunction(p, n, min, a, loc);
tmp = a[p];
a[p] = a[loc];
a[loc] = tmp;
}
cout << "\nPrinting array: \n";
for (int i = 1; i <= n; i++) {
cout << a[i] << endl;
}
}
int main()
{
int a[5], n = 0;
n = addElements(a);
firstFunction(a, n);
}
答案 0 :(得分:1)
firstFunction:
la $s2,myArr
la $s3,myArr
li $t9,0 #index for loop
li $t3,1
sub $t3,$t0,$t3
jal loop
loop:
#min=a[p] not passing this as argument,have made it a global variable
lw $s0,0($s2)
move $a0,$t9
#s0 is min and a0 is loc (p)
#t0 is n, $s2 is myArr -these I have made global var as they are used throughout the program
#the only argument passed to second Function is p that I have saved in $a0
jal secondFun
back4:
lw $s1, 0($s2) #s1 is tmp
sll $t7,$a0,2 #index of array at a0
addu $t7,$t7,$s3
lw $t5,0($t7)
sw $t5,0($s2)
sw $s1,0($t7)
addi $t9,$t9,1
addi $s2,$s2,4
blt $t9,$t3,loop
jal print
secondFun:
addi $t2,$a0,1
jal loop2
loop2:
sll $t8,$t2,2 #index of array at a0
addu $t8,$t8,$s3
lw $t6,0($t8)
blt $t6,$s0,innerif
back3:
addi $t2,$t2,1
blt $t2,$t0,loop2
jal back4
innerif:
lw $s0,0($t8)
move $a0,$t2
jal back3
希望对你有帮助,欢迎指正