在MIPS整数数组:数组设置[索引]来迭代值吗? [英] Integer Arrays in MIPS : Setting array[index] to iteration value i?
问题描述
我正在翻译下面的C ++ code,以MIPS(这是我卡上的程序的只是一小部分),我理解了如何设置 $ T
注册采取给出的数组的值,但我完全卡在
POS [统计] =我;
我试过 SW
, LW
,但每次我尝试这些,我得到的地址超出范围的例外/等等。
能否给我一个人解释发生了什么错在这里?当循环到达 POS [计数] = I
,我需要改变 POS [计数]
从为0xffffffff
到(我)
每个循环迭代。即将到来的错误,因为我需要调整为-1的 POS []
?
我完全丢失,一直没能找到对这个问题非常相似,任何解释。
(用于格式化道歉,因为MIPS拥有如此多的标签线,张贴在这里的格式是非常古怪的)
。数据
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 97SP:.asciiz
ENDL:.asciiz\\ n#$ S0计数
#$ S1键
#$ S2我 。文本主营:阿迪$ S0,$ 0,0#诠释计数= 0;
阿迪$ S1,$ 0时,24#INT键= 24;
阿迪$ S2,$ 0,0
LA $ S3,D
LA $ S4,POS
LA $ S5,X
#的for(int i = 0;我小于10;我++){
循环:MUL $ T0,$ S2,4#如果(D [I]≥=键){
加$ T0,T0 $,$ S3
LW $ T0,($ T0) BLT $ T0,$ S1,循环1 SLL $ T1,$ S0,2#$ T1 =计数*的sizeof(INT)
阿杜$ T2,$ S4,$ T1#$ T2 =&放大器; POS [统计]
LW $ T2,($ T2)#$ T2 = POS [统计] 加$ T3,$ S5,$ T1#$ T3 =&放大器; X [统计]
LW $ T3,($ T3)#$ T3 = X [统计]
的sw $ S2($ T2)#POS [统计] =我;
#X [计] = D [I];循环1:阿迪$ S2,S2 $,1#我++;
阿迪$ S0,S0 $ 1#计数++;
#}
#}
下面是C ++的code等同的:
的#include<&iostream的GT;
使用命名空间std;INT×〔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};
诠释主(){诠释计数= 0;
INT键= 24;
对(INT I = 0; I&小于10;我++){
如果(D [I]≥=键){
POS [计数] =我;
X [计数] = D [I];
算上++;
}
}对(INT I = 0; I&小于10;我++){
如果(POS [1] - ; 0)
打破;
COUT<< POS [1] - ;&下; << X [1] - ;&下; ENDL;
} }
这部分是错误的:
LW $ T2,($ T2)#$ T2 = POS [统计]
加$ T3,$ S5,$ T1#$ T3 =&放大器; X [统计]
LW $ T3,($ T3)#$ T3 = X [统计]
的sw $ S2($ T2)#POS [统计] =我;
你为什么要装 POS [计数]
和 X [计数]当你要同时写入
?这不仅是不必要的,它会破坏 $ T2
和 $ T3
所以当你真的想写点什么,他们会不再有效。
此外,循环结束是错误的,计数++
应该是状态里面。对于您将需要交换的最后两行(包括标签)。
一个稍微清理版本可能看起来像:
。数据
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计数
#$ S1键
#$ S2我 。文本
.globl主
主营:阿迪$ S0,$ 0,0#诠释计数= 0;
阿迪$ S1,$ 0时,24#INT键= 24;
阿迪$ S2,$ 0,0#INT I = 0;
#的for(int i = 0;我小于10;我++){
循环:SLL $ T0,$ S2,2#$ T0 = sizeof的我*(INT)
LW $ T0,D($ T0)#$ T0 = D [I]
BLT $ T0,$ S1,回路1#如果(D [1] - ;键) SLL $ T1,$ S0,2#$ T1 =计数*的sizeof(INT)
的sw $ S2,POS($ T1)#POS [统计] =我
SW $ T0,X($ T1)#X [计] = D [I]
阿迪$ S0,S0 $ 1#计数++;循环1:阿迪$ S2,S2 $,1#我++;
BLT $ S2,10,环
I'm working on translating the below C++ code to MIPS (this is just a small portion of the program that I'm stuck on), and I understand the gist of how to set $t
registers to take the array values given, but I'm completely stuck on
pos[count] = i;
I've tried sw
, lw
, but everytime I try these, I get address out of range exceptions/etc.
Can someone explain to me what's going wrong here? When the loop gets to pos[count] = i
, I need to change pos[count]
from 0xffffffff
to (i)
for each loop iteration. Is the error coming because I need to adjust for the -1's in pos[]
?
I'm completely lost and haven't been able to find any explanations that are similar enough to this problem.
(Apologies for the formatting, because MIPS has so many tabbed lines, the formatting for posting here is exceptionally wacky)
.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++;
# }
# }
Here's the C++ code equivalent:
#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;
}
}
This part is wrong:
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;
Why are you loading pos[count]
and x[count]
when you want to write to both? Not only is this unnecessary, it will destroy $t2
and $t3
so when you really want to write, they will no longer be valid.
Also, the end of the loop is wrong, the count++
should be inside the condition. For that you will need to swap the last two lines (including the label).
A slightly cleaned up version could look like:
.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
这篇关于在MIPS整数数组:数组设置[索引]来迭代值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!