2017-03-01 56 views
0

我在我的學校的計算機組裝和組織類中,我們有一段代碼在MIPS MARS中,我無法弄清楚。我們的老師並沒有教出這本書,因此我無法知道如何在MIPS MARS中對其進行編碼。我知道如何將初始值分配給$ s寄存器,但不知道如何編寫if語句或應該如何查看。任何幫助將不勝感激,因爲我不能從這位老師那裏學習拯救我的生命。我們應該使用MIPS MARS彙編語言編寫以下代碼:Mips Mars braching語句

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至10次的循環
  • 圍棋,寫出來的循環計數器。
  • 使用循環計算10個數字的總和。寫出結果。
  • +1

    在MARS中,編輯器給了我關於可能的指示的提示。所有的條件分支都以'b'開始......如果你不能從提示中找出它實際在做什麼,請嘗試谷歌特定的指令(大多數分支指令也是僞指令)。一個警告,如果你要搜索彙編條件分支,你會發現一些不同的CPU的好的解釋,它可能會談論像「旗幟」。在MIPS中沒有這樣的事情,MIPS分支總是基於作爲分支指令參數提供的值,因此只能搜索MIPS。 – Ped7g

    +1

    MIPS還有指令來避免分支,例如當第一個參數小於第二個參數(否則設置爲「0」)時,'slt'將目標設置爲'1'。在你的任務中,這些沒有多大幫助,但是有時沒有分支的'0/1'就足以計算出所需的結果。 – Ped7g

    回答

    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: