比较数组元素。 Switch 语句/if else - 汇编 - MIPS
这是我的第一个项目,抱歉我缺乏知识,这是我试图复制的代码。
int A[3] = {0, 1, 2};
int x;
for ( int i = 0; i < 3; i++ )
{
if ( A[i] > 1 )
x = 1;
else
{
switch ( A[i] )
{ case 0:
x = 2;
case 1:
x = 3;
}
}
printf(“%d”, x);
}
这是我在汇编中的代码。
main: li $s0, 0
sw $s0, ($s7) #int A[0] = {0};
li $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
li $s0, 2
sw $s0, 8($s7) #int A[2] = {2};
li $s1, 1 #initialize x = 1
li $s0, 0 #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
start: blt $s0, $s2, for
j finish
for: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7
bgt $t0, $s3, exone #a[i] is greater than 1 how to increment the array up?
#switch statement
lw $t0, 0($t1)
jr $t0
j start
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j start
finish: li $v0, 10 # exit system call
syscall
我不确定我在哪里出错,它编译但没有给我想要的输出或任何与此相关的输出。
根据一些信息我更新了我的代码。
main: add $s0,$zero, $zero
li $s7, 200
sw $s0, 0($s7) #int A[0] = {0};
addi $s0, $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
addi $s0, $s0, 1
sw $s0, 8($s7) #int A[2] = {2};
li $s1, 0 #initialize x = 0
li $s0, 0 #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
#check to enter the for loop
for: blt $s0, $s2, enter
j finish
#enter the for loop
enter: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7 #reg A[i]
lw $t2, 0($t0)
bgt $t2, $s3, exone #a[i] is greater than 1 check
#switch statement
jr $t2
#just in case jump back to for loop
j for
#address for the switch statements
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
#address for the if else statement
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j for
finish: li $v0, 10 # exit system call
syscall
输出应该是“231”。
This is my first project sorry for my lack of knowledge, this is the code I'm trying to replicate.
int A[3] = {0, 1, 2};
int x;
for ( int i = 0; i < 3; i++ )
{
if ( A[i] > 1 )
x = 1;
else
{
switch ( A[i] )
{ case 0:
x = 2;
case 1:
x = 3;
}
}
printf(“%d”, x);
}
This is the code that I have in assembly.
main: li $s0, 0
sw $s0, ($s7) #int A[0] = {0};
li $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
li $s0, 2
sw $s0, 8($s7) #int A[2] = {2};
li $s1, 1 #initialize x = 1
li $s0, 0 #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
start: blt $s0, $s2, for
j finish
for: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7
bgt $t0, $s3, exone #a[i] is greater than 1 how to increment the array up?
#switch statement
lw $t0, 0($t1)
jr $t0
j start
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j start
finish: li $v0, 10 # exit system call
syscall
I'm not sure where I'm going wrong it compiles but doesn't give me the output I want or any output for that matter.
Based on some info I have updated my code.
main: add $s0,$zero, $zero
li $s7, 200
sw $s0, 0($s7) #int A[0] = {0};
addi $s0, $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
addi $s0, $s0, 1
sw $s0, 8($s7) #int A[2] = {2};
li $s1, 0 #initialize x = 0
li $s0, 0 #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
#check to enter the for loop
for: blt $s0, $s2, enter
j finish
#enter the for loop
enter: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7 #reg A[i]
lw $t2, 0($t0)
bgt $t2, $s3, exone #a[i] is greater than 1 check
#switch statement
jr $t2
#just in case jump back to for loop
j for
#address for the switch statements
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
#address for the if else statement
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j for
finish: li $v0, 10 # exit system call
syscall
The output should be “231”.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是错误的
jr
表示跳转寄存器,它跳转到$t2
中包含的地址(此时包含a[i]
,其中这里是 0、1 或 2——不是很好的跳转地址)。您可以做很多事情来纠正这个问题(我假设您打算在 C 代码中包含
break
语句),但这里有一些未经测试的快速代码,它使用一组 if- 来模拟开关声明:This is wrong
jr
means Jump Register and it jumps to the address contained in$t2
( which at this point containsa[i]
, which here is 0, 1 or 2 -- not great addresses to jump to).You could do a lot of things to correct this (I'll assume you meant to have
break
statements in your C code), but here's some quick untested code that mimics the switch with a set of if-statements:泵房:
首先确定你想做什么,你的程序没有任何正确的目标。
尽管您应该注意一些要点,例如
问题。
变量应始终在程序顶部声明
而不是在循环中。相同类型的变量应该一起声明。
Pumphouse:
First be sure what you want to do,your program does not have any proper aim.
All though you should have to note some points like,
problem.
Variables should be declared always on the top of program
not in the loop. variables of same type should be declare together.
所以这有效,这并不完全是我想要的方式,但它有效我想在 switch 语句中使用 jr 但我还没有完全做到这一点。
So this works it wasn't exactly how I wanted to do it but it works I wanted to use a jr in the switch statement but I'm not quite there yet.