首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C++到MIPS汇编语言的排序数组代码转换

从C++到MIPS汇编语言的排序数组代码转换
EN

Stack Overflow用户
提问于 2020-04-15 18:59:36
回答 1查看 198关注 0票数 0

我在学校做了一个练习,把C++代码转换成MIPS汇编语言。我对某些函数或过程中的参数有一些问题。代码是对最多包含5个成员的数组进行排序,本练习在c++中有三个函数。第一个是创建一个通过输入将数组中的数字相加的函数,它是一个叶过程,另外两个函数是对数组进行排序。这两个函数是汇编语言中的嵌套过程,我不能真正理解如何实现它们,我不能理解的是如何向下面的代码中的另一个过程发送5个参数。

代码语言:javascript
复制
#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);

}
EN

回答 1

Stack Overflow用户

发布于 2021-03-31 09:44:39

代码语言:javascript
复制
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

我希望这对你有所帮助,请随时纠正我

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61227066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档