【问题标题】:Integer Arrays in MIPS : Setting array[index] to iteration value i?MIPS 中的整数数组:将数组 [index] 设置为迭代值 i?
【发布时间】:2013-09-30 19:57:26
【问题描述】:

我正在努力将以下 C++ 代码转换为 MIPS(这只是我坚持的程序的一小部分),并且我了解如何设置 $t 寄存器以获取数组的要点给定的值,但我完全坚持

pos[count] = i;

我试过swlw,但每次我尝试这些时,我都会得到地址超出范围的异常/等等。

有人可以向我解释这里出了什么问题吗?当循环到达pos[count] = i 时,我需要为每个循环迭代将pos[count]0xffffffff 更改为(i)。出现错误是因为我需要调整 pos[] 中的 -1 吗?

我完全迷失了,找不到任何与这个问题足够相似的解释。

(抱歉格式化,因为 MIPS 有很多标签行,这里发布的格式非常古怪)

    .data
x:  .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
pos:    .word   -1
        .word   -1
        .word   -1
        .word   -1
        .word   -1
        .word   -1
        .word   -1
        .word   -1
        .word   -1
        .word   -1
d:      .word   73
        .word   47
        .word   23
        .word   26
        .word   67
        .word   99
        .word   13
        .word   53
        .word   1
        .word   97

sp: .asciiz " "
endl:   .asciiz "\n"

# $s0   count
# $s1   key
# $s2   i

        .text

main:   addi    $s0, $0, 0  #  int count = 0;
        addi    $s1, $0, 24         #  int key = 24;
        addi    $s2, $0, 0
        la  $s3, d
        la  $s4, pos
        la  $s5, x
                       #  for (int i=0; i<10; i++) {
loop:   mul     $t0, $s2, 4 #    if (d[i] >= key) {
        add     $t0, $t0, $s3
        lw  $t0, ($t0)

            blt     $t0, $s1, loop1

            sll     $t1, $s0, 2     # $t1 = count * sizeof(int)
            addu    $t2, $s4, $t1   # $t2 = &pos[count];
            lw      $t2, ($t2)  # $t2 = pos[count];

            add     $t3, $s5, $t1   # $t3 = &x[count];
            lw      $t3, ($t3)  # $t3 = x[count];


            sw    $s2, ($t2)            #      pos[count] = i;
                    #      x[count] = d[i];

loop1:     addi    $s2, $s2, 1     # i++;
           addi    $s0, $s0, 1     # count++;
                    #    }
                    #  }

这是等效的 C++ 代码:

#include <iostream>
using namespace std;

int x[10] = {0};
int pos[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int d[10] = {73, 47, 23, 26, 67, 99, 13, 53, 1, 97};
int main() {

int count = 0;
int key = 24;
for (int i=0; i<10; i++) {
   if (d[i] >= key) {
     pos[count] = i;
     x[count] = d[i];
      count++;
  }
 }

for (int i=0; i<10; i++) {
    if (pos[i] < 0)
      break;
     cout << pos[i] << " " << x[i] << endl;
    }

 }

【问题讨论】:

  • 我无法对此做出正面或反面。如果您展示了您尝试复制的 C++ 代码,将会很有帮助,然后我们就会知道它应该做什么。
  • 明白了。我只是在帖子底部更清楚地添加了 C++ 等效项。我的主要问题是翻译 pos[count] = i;和 x[count] = d[i];到 MIPS

标签: arrays assembly mips mars-simulator


【解决方案1】:

这部分错了:

lw      $t2, ($t2)  # $t2 = pos[count];
add     $t3, $s5, $t1   # $t3 = &x[count];
lw      $t3, ($t3)  # $t3 = x[count];
sw    $s2, ($t2)    #      pos[count] = i;

为什么要同时写入pos[count]x[count]?这不仅没有必要,还会破坏$t2$t3,所以当你真的想写时,它们将不再有效。

另外,循环结束是错误的,count++ 应该在条件内。为此,您需要交换最后两行(包括标签)。

稍微整理后的版本可能如下所示:

    .data
x:      .word   0, 0, 0, 0, 0, 0, 0, 0, 0, 0
pos:    .word   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
d:      .word   73, 47, 23, 26, 67, 99, 13, 53, 1, 97

# $s0   count
# $s1   key
# $s2   i

        .text
.globl main
main:   addi    $s0, $0, 0      #  int count = 0;
        addi    $s1, $0, 24     #  int key = 24;
        addi    $s2, $0, 0      #  int i = 0;
#  for (int i=0; i<10; i++) {
loop:   sll     $t0, $s2, 2     # $t0 = i * sizeof(int)
        lw      $t0, d($t0)     # $t0 = d[i]
        blt     $t0, $s1, loop1 # if (d[i] < key)

        sll     $t1, $s0, 2     # $t1 = count * sizeof(int)
        sw      $s2, pos($t1)   # pos[count] = i
        sw      $t0, x($t1)     # x[count] = d[i]
        addi    $s0, $s0, 1     # count++;

loop1:  addi    $s2, $s2, 1     # i++;
        blt $s2, 10, loop

【讨论】:

  • 非常感谢,你不知道我有多感激。现在连续两天一直在修补这个问题。我在问题设置中给出的加载部分,所以我认为我只使用那种类型的加载地址被卡住了:因此,我不知道你可以做 d($t0),例如。
  • 在清理的脉络上,我可以建议x: .word 0:10 y: .word -1:10
【解决方案2】:

int count(int a[], int n, int x){ int res = 0; int i = 0; for(i = 0; i != n; i++) if(a[i] == x) res = res + 1; return res; }

编写一个 MIPS 汇编程序,该程序将使用上面的函数计数,如下所示:  将以下 10 个值硬编码到数组 a 中: 128、10、23、12、128、9、220、46、128、5  硬编码 n = 10  提示用户输入如下值:“请输入整数值”  读取整数值并将其存储(我们称之为 x)  使用以下参数调用函数 count:count(a, 10, x)  输出结果如下:“x 在列表中出现的次数是 res 次”  退出程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2023-03-07
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2012-06-16
    • 2019-12-18
    相关资源
    最近更新 更多