【问题标题】:If greater than or equal in MIPS如果在 MIPS 中大于或等于
【发布时间】:2014-11-09 15:41:50
【问题描述】:

使用系统调用提示并输入两个整数“a”和“b”

根据 a>b、a=b 或 a 显示以下语句之一

  • 您输入的 a 大于 b

  • 您输入的等于 b

  • 您输入的 a 小于 b

我必须得到这个提示,我非常努力地完成它。这就是我卡住的地方,非常感谢您的帮助。

    .data  
p1: .asciiz "Please enter the first number ? "  
p2: .asciiz " Please enter the second number? "  
ans1: .asciiz " \nYou entered a greater than b "  
ans2: .asciiz " \nYou entered a equal to b "  
ans3: .asciiz " \nYou entered a less than b "  


        .text
        .globl main

main:    
    li $v0, 4     #system call code for print_str  
    la $a0, p1  #address of string to print  
    syscall     #print the first prompt  


    li $v0, 5   #system call code for read_int
    syscall     #read first integer
    move $t1, $v0   #store it till later

    li $v0, 4   #system call code for print_str
    la $a0, p2  #address of string to print
    syscall     #prints the second prompt

    li $v0, 5   #system call code for read_int
    syscall     #read first integer
    move $t2, $v0   #store it till later

    slt $t1,$s1,$s0      # checks if $s0 > $s1
    beq $t1,1,label1 

我真的不知道如何使用分支语句,这真的很混乱。我想知道如何解决它。

【问题讨论】:

    标签: if-statement assembly mips equals


    【解决方案1】:

    为什么要将数字读入$t1$t2 然后比较$s1$s0?哪里混乱了?

    只需使用sltbeq/bne,即可涵盖您需要的所有比较案例。

    假设a在$s0,b在$s1

    • a

      slt $t0, $s0, $s1
      bne $t0, $zero, a_lt_b # $t0 == 1 != 0 if a < b
      

    • a = b:

      beq $s0, $s1, a_eq_b   # nothing more to explain
      
    • a > b:

      slt $t0, $s1, $s0
      bne $t0, $zero, b_lt_a # $t0 == 1 != 0 if b < a
      
    • a >= b:

      slt $t0, $s0, $s1
      beq $t0, $zero, a_ge_b # $t0 == 0 if a >= b or !(a < b)
      
    • a

      slt $t0, $s1, $s0
      beq $t0, $zero, b_ge_a # $t0 == 0 if b >= a or !(b < a)
      

    【讨论】:

    • 谢谢,但这不是我猜的答案。但我真的很感谢你的时间和帮助。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2011-12-09
    • 2011-01-25
    • 2013-09-02
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多