MIPS条件打印所有情况 [英] MIPS conditional printing all cases

查看:46
本文介绍了MIPS条件打印所有情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定 3 个数字中的最小值.

I am trying to determine the lowest of 3 numbers.

我正在使用 slt 一次比较 2 个数字.

I am using slt to compare 2 numbers at a time.

我正在使用 beq 和 bne,并将它们与 $zero 进行比较(因为 slt 的结果是 0 或 1,并且寄存器 $zero 保存常量 0).通过使用 beq 和 bne,我试图跳转到最终将打印三个中最低值的特定标签.

I am using beq and bne, and comparing them to $zero (because the result of slt is either 0 or 1, and register $zero holds the constant 0). By using beq and bne, I am trying to jump to the specific label that will ultimately print the lowest of the three.

我很困惑所有标签信息都被打印出来了.下面是我的代码.有人可以帮我确定为什么所有案例都在打印吗?

I am puzzled that all of the label messages are getting printed. Below is my code. Can someone help me identify why all cases are printing?

# compare $s0 < $s1 
slt      $t0, $s0, $s1                   # if $s0 < $t1
bne      $t0, $zero, compare_s0_s2      # $t0 == 1, compare $s0 < $s2
beq      $t0, $zero, compare_s1_s2      # $t0 == 0, compare $s1 < $s2

# compare $s0 < $s2
compare_s0_s2:
slt     $t1, $s0, $s2                    # if $s0 < $s2
bne     $t1, $zero, print_lowest_s0     # $t1 == 1, print $s0
beq     $t1, $zero, print_lowest_s2     # $t1 == 0, print $s2

# compare $s1 < $s2
compare_s1_s2:
slt     $t2, $s1, $s2                   # if $s0 < $s2
bne     $t2, $zero, print_lowest_s1    # $t2 == 1, print $s1
beq     $t2, $zero, print_lowest_s2    # $t2 == 0, print $s2


# print $s0
print_lowest_s0:
li  $v0, 1
la  $a0, ($s0)
syscall

# print $s1
print_lowest_s1:
li  $v0, 1
la  $a0, ($s1)
syscall

# print $s2
print_lowest_s2:
li  $v0, 1
la  $a0, ($s2)
syscall

推荐答案

我想通了!打印所有案例的原因是因为我的语法不正确!我翻译了我的条件错误.

I figured it out! The reason why is was printing all cases was because my syntax was incorrect! I translated my conditional wrong.

如果您注意到,我的所有 3 个打印语句都紧随其后.我最初是这样格式化它们的,因为我认为在比较中找到较小的数字后,程序控件会切换到正确的打印语句.但它不是那样工作的.汇编器看到的都是三个连续的打印语句,所以它打印那些.

If you notice, all 3 of my print statements follow one right after another. I originally formatted them this way because I thought the program control would switch to the proper print statement after finding the smaller number in a comparison. But it doesn't work that way. All the assembler sees are three consecutive print statements, so it prints those.

如果我只希望出现一个打印语句,我必须进行某种程序更改,以分支或跳转到不同的代码,或者退出程序.每个打印语句都必须以这样的结尾:

If I only wanted one print statement to occur, I have to do some sort of program change that will either branch or jump to different code, or exit the program. Each print statement would have to end with something like this:

# print $s0 as lowest 
print_lowest_s0:
li  $v0, 1
la  $a0, ($s0)               # loads low number from comparison from $s0
syscall                      # prints low number
j   hi_int_message           # changes program control to another label

我已经改变了我的条件逻辑,所以我不能 100% 确定我上面的问题中列出的逻辑是正确的.这是我现在的逻辑:

I've since changed my conditional logic, so I can't be 100% sure that what's listed in my question above is the correct logic. Here's what my logic looks like now:

# compare $s0 < $s1 
slt $t0, $s0, $s1           # if $s0 < $s1
beq $t0, 0, compare_s1_s2     # else: $t0 == 0, branch to compare $s1 < $s2 label
slt $t1, $s0, $s2           # true: compare if $s0 < $s2
beq $t1, 0, print_lowest_s2  # else: $t1 == 0, $s2 == lowest; branch to label 
j   print_lowest_s0          # true:  $s0 == lowest; jump to $s0 print label

但是,这个问题的重点是停止打印所有内容.希望这可以帮助以后的人!

However, the main point of this question was to stop printing everything. Hope this can help someone later!

这篇关于MIPS条件打印所有情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆