In: Computer Science
#use MPLAB X IDE
# Question 1 for HW 3
# Your solution should implement the following conditional statement:
#   if (A < 10)
#       D = C + 10
#   else if (A == 20)
#       D = C - B
#   else
#       D = C + B
        
    .global    main
    .data
### THESE VARIABLES ARE SIMPLY GIVEN VALUES TO START
###   WITH--CHANGE THEIR VALUES AND VERIFY YOUR PROGRAM
###   WORKS APPROPRIATELY IN ALL CASES
A:  .int    3
B:  .int    1
C:  .int    7
D:  .int    0
        
    .text
    .set       noreorder
    .ent       main
main:
    # Load variables into registers
    lw  $t0, A
    lw  $t1, B
    lw  $t2, C
    
    # Implement conditional statement described above
    # Make sure final result is in memory in variable "D"
        
# This code simply loops infinitely
spin:   
    j          spin
    nop
    .end        main    
The program required to have the logic in question is as below.
.global    main
    .data
### THESE VARIABLES ARE SIMPLY GIVEN VALUES TO START
###   WITH--CHANGE THEIR VALUES AND VERIFY YOUR PROGRAM
###   WORKS APPROPRIATELY IN ALL CASES
A:  .int    3
B:  .int    1
C:  .int    7
D:  .int    0
        
    .text
    .set       noreorder
    .ent       main
main:
    # Load variables into registers
    lw  $t0, A
    lw  $t1, B
    lw  $t2, C
    
    # Implement conditional statement described above
   bge $t0,10,next  # if a<10 execute below instruction else gonto lable next
   addi $t3,$t2,10  #D = C+10
   j done
  next: 
   bne $t0,20,else  #if a=20 execute below else go to else label
   sub $t3,$t2,$t1  #D = C-B
   j done
  else:
   add $t3,$t2,$t1  # D= C+B
    # final result is in memory in variable "D"
    done:
     sw $t3,D  #store value of D in memory  
    nop
    .end        main