[英]Mips Mars braching statements
我在学校的计算机组装和组织课程中,我们在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;
}
因为您的问题不是特定的,并且您仅询问如何在此处编写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:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.