首页 > 解决方案 > 从 C++ 翻译的 MIPS 没有输出

问题描述

这是我做 MIPS 的第一个练习。下面我尝试从 C++ 转换为 MIPS。我试图包含评论来评论我认为这行代码的作用。另外,一切都给了我,我必须填写第一个 for 循环。所以,我的问题是当我在 MARS 上运行它时,没有输出输出是我遗漏的错误吗?

    .data
x:  .word   0       #int x[] = {0, 20, 10, 7, 8, 1, 3, 5};
    .word   20
    .word   10
    .word   7
    .word   8
    .word   1
    .word   3
    .word   5

ascend: .word   0:8     #int ascend[8] = {0};
endl:   .asciiz "\n"

# j $s0
# i $s1
# &x[0] $s2
# &ascend[0] $s3

    .text
main:   li  $s0, 0      #  int j = 0;
        li  $s1, 0      #  int i = 0;
        la  $s2, x
        la  $s3, ascend


        bgt $s1, 7, for1    #  for (int i=0; i<7; i++) {
for:    mul $t1, $s1, 4     #  i * 4
        add $t1, $t1, $s2   #  add i to x   /// x[i]
        lw $a1, ($t1)       # load word

        add $t2, $s1, 1     # i + 1
        mul $t3, $t2, 4     # (i+1) * 4
        add $t3, $t3, $s2   # add (i+1) to x //// x[i+1]
        lw  $a2, ($t3)      # load word

                            #    if (x[i] < x[i+1]) {
        bgt $t1, $t3, for   # if (x[i] > x[i+1] then go back to for loop
                            # else keep going 
        mul $t4, $s0, 4     # j * 4
        add $t4, $t4, $s3   # add j to ascend 
        lw $a3, ($t4)       # load it

        move $t4, $s1       # load i into ascend[j]
                            #      ascend[j] = i;
        addi $s0, $s0, 1    #      j++;
        addi $s1, $s1, 1    #      i++;
        blt $s1, 7, for     # if i < 7 at this time, go back to for loop
                            #    }
                            #  }


       li   $s1, 0      #  for (int i=0; i<j; i++) {
for1:  mul  $t0, $s1, 4
       add  $t0, $t0, $s3
       lw   $a0, ($t0)
       li   $v0, 1
       syscall          #    cout << ascend[i] << endl;
       la   $a0, endl
       li   $v0, 4
       syscall          #  }

       addi $s1, $s1, 1
       blt  $s1, $s0, for1

       li   $v0, 10
       syscall

下面是 C++ 代码:

#include <iostream>
using namespace std;

int x[] = {0, 20, 10, 7, 8, 1, 3, 5};
int ascend[8] = {0};
int main() {
  int j = 0;
  for (int i=0; i<7; i++) {
    if (x[i] < x[i+1]) {
      ascend[j] = i;
      j++;
    }
  }

  for (int i=0; i<j; i++) {
     cout << ascend[i] << endl;
  }

}

非常感谢您的帮助!

标签: assemblymips

解决方案


推荐阅读