因爲你的問題不是特定的,你只問在這裏編寫一個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:
在MARS中,編輯器給了我關於可能的指示的提示。所有的條件分支都以'b'開始......如果你不能從提示中找出它實際在做什麼,請嘗試谷歌特定的指令(大多數分支指令也是僞指令)。一個警告,如果你要搜索彙編條件分支,你會發現一些不同的CPU的好的解釋,它可能會談論像「旗幟」。在MIPS中沒有這樣的事情,MIPS分支總是基於作爲分支指令參數提供的值,因此只能搜索MIPS。 – Ped7g
MIPS還有指令來避免分支,例如當第一個參數小於第二個參數(否則設置爲「0」)時,'slt'將目標設置爲'1'。在你的任務中,這些沒有多大幫助,但是有時沒有分支的'0/1'就足以計算出所需的結果。 –
Ped7g