0
votes

I am in a Computer Assembly and Organization class at my school, and we have a piece of code in MIPS MARS that I can not figure out. Our teacher doesn't teach out of the book so there is no way for me to know how to code this in MIPS MARS. I understand how to assign the initial values into the $s registers, but have no idea how to code the if statements or how they should look. Any help would be appreciated because I can't learn from this teacher to save my life. We are supposed to code the following in MIPS MARS assembly language:

1) Use the MARS assembler submit a working program that will convert the following High level Java statement to MIPS Assembly code? (60 pts) Code the following branching statements. Let a = 10, b = 16, c = 16, d = 6, use register $s0 for a, $s1 for b and so forth.

    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. Go through a loop from 1 to 10 times, write out the loop counter.
  2. Calculate the summation of 10 numbers using a loop. Write out the result.
1
In MARS the editor gives me hints about possible instructions. All the conditional branches start with b ... if you can't figure out from the hint what is it actually doing, try to google the particular instruction (also most of the branch instructions are pseudo-instructions). One warning, if you would search for assembly conditional branching, and you will find some nice explanation for different CPU, it may talk about something like "flags". There's no such thing in MIPS, MIPS branching is always based on values provided as arguments to the branch instruction, so search for MIPS only.Ped7g
MIPS has also <set_some_condition> instructions to avoid branching, for example slt will set destination to 1 when first argument is less than second argument (otherwise value set is 0). In your task these would not help much, but sometimes having 0/1 without branching is enough to calculate desired result.Ped7g

1 Answers

1
votes

Because your question is not specific and you only asking how write an if statement here is a code example that printing the larger of two integer from user input. I have commented where the if statement begins and you can run it on 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

Her is also pseudo code for an 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: 

Let's convert your first if statement to explain it in detail.

  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: