【问题标题】:Mips Mars braching statementsMips Mars 分支语句
【发布时间】:2017-03-01 21:17:57
【问题描述】:

我在学校的计算机组装和组织课程中,我们在 MIPS MARS 中有一段我无法弄清楚的代码。我们的老师没有教书,所以我无法知道如何在 MIPS MARS 中编写代码。我了解如何将初始值分配给 $s 寄存器,但不知道如何编写 if 语句或它们的外观。任何帮助将不胜感激,因为我无法向这位老师学习来挽救我的生命。我们应该用 MIPS MARS 汇编语言编写以下代码:

1) 使用 MARS 汇编器提交一个工作程序,将以下高级 Java 语句转换为 MIPS 汇编代码? (60 分) 编写以下分支语句。设 a = 10, b = 16, c = 16, d = 6,a 使用寄存器 $s0,b 使用 $s1,以此类推。

    if(a==b){
        Z = a+a;
        Z=Z+b+c+d;
    }

    if(a==b){
        Z=a;
        Else{
            Z = (a+b+c)-d;
        }

    if (a != b){
    Z=a;
    Else{
        Z = (a+b+c)-d;
    }
  1. 循环 1 到 10 次,写出循环计数器。
  2. 使用循环计算 10 个数字的总和。写出结果。

【问题讨论】:

  • 在 MARS 中,编辑器会提示我可能的指令。所有条件分支都以b 开头...如果您无法从提示中弄清楚它实际上在做什么,请尝试用谷歌搜索特定指令(大多数分支指令也是伪指令)。一个警告,如果你要搜索汇编条件分支,你会发现一些关于不同 CPU 的很好的解释,它可能会谈论“标志”之类的东西。 MIPS 中没有这样的东西,MIPS 分支总是基于作为分支指令参数提供的值,所以只搜索 MIPS。
  • MIPS 也有 指令来避免分支,例如slt 将在第一个参数小于第二个参数时将目标设置为1(否则值设置为0)。在您的任务中,这些不会有太大帮助,但有时 0/1 没有分支就足以计算所需的结果。

标签: assembly mars-simulator


【解决方案1】:

因为你的问题并不具体,你只问如何写一个if statement 这是一个代码示例,它从用户输入中打印两个整数中的较大者。我已经评论了if statement 的开始位置,您可以在 MARS 上运行它。

.text


 .data
 message: .asciiz " Enter a number\n"
 message2: .asciiz "Enter another number\n"
 main:
.text

la $a0, message      #print out message
li $v0, 4
syscall


li $v0, 5       # read user input (integer)
syscall

move $t0,$v0          # t0 = user input number 1

la $a0, message2       #print out message2
li $v0,4
syscall

li $v0, 5          #read user input 
syscall

move $t1,$v0       # t1 = user input number 2
#********************************************
# if statement begins her
#*************************************** 
bgt $t0,$t1, bigger    # branch to bigger if t0 > t1
move $t2,$t1           # t2 = t1
b   endif              
bigger:
move $t2,$t0           # t2 = t0
endif:  
# ************************************
# if finish here
#************************************
move $a0,$t2           # move the result in the argument a0 
li $v0, 1              # print it out
syscall

li $v0,10
syscall

她也是if statement的伪代码

branch $a0,$a1, lable   #in case you use `beq` means ( if a0 ==a1 jump to lable)
#branch to lable if condition is met
#if body
b   endif
lable:
#if body

endif: 

让我们转换你的第一个if statement来详细解释一下。

  if(a==b){
       Z = a+a;
       Z=Z+b+c+d;



   beq $s0,$s1,L
   add $t0,$0,$s0
   add $t1,$t0,$s1
   add $t2,$s2,$s3
   add $t0,$t1,$t2
   b   endif
L:

endif:

【讨论】:

    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多