SageTheWizard

Triangles.asm

Apr 9th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Author:   Madeline Chubb  +  Jacob Gallucci
  2. # Course:   CMPSC 312
  3. # Assignment:   MIPS Programming Project
  4. # Due Date: 4/11/2018
  5. # File: TriangleWithLoop.asm
  6. # Purpose:  This program prompts a user to enter 3 positive integers and reports whether or not they form a triangle.
  7. # Simulator:    Mars 4.5
  8. # Operating
  9. # System:   macOS High Sierra 10.13.2 (Maddie)  + Debian 9 Stretch (Jacob)  + Windows 8.1 (Jacob)
  10. #               (SO basically this should work on literally any operating system... *BSD untested but we're sure it works
  11. #    References:    Class demo programs
  12. #       https://www.youtube.com/watch?v=B0GAXDjfdbQ (How to get user input)
  13. #       http://forums.devshed.com/programming-languages-139/mips-finding-largest-integers-3-inputs-858246.html
  14. #       (How to compare integers)
  15.  
  16. .data  # Output Variables
  17.  
  18. # Prompt user for input
  19. prompt:     .asciiz "Enter 3 positive integers by pressing <enter> after each: "
  20.  
  21. # New Line
  22. newline:    .asciiz "\n"
  23.  
  24. # Invalid Input Response
  25. error:      .asciiz "Invalid Input!\n"
  26.  
  27. # Separator Line
  28. format:     .asciiz "*************************************************************************\n"
  29.  
  30. # Prompt user to input another triangle
  31. continue:   .asciiz "Do you want to test more integers? (Type 0 to quit, 1 to run again.): "
  32.  
  33. # Lengths do not form a triangle
  34. nope:       .asciiz "These integers do NOT form a triangle.\n"
  35.  
  36. # Lengths do form a triangle
  37. yes:        .asciiz "These integere DO form a triangle.\n"
  38.  
  39. .text # Section containing code
  40. .globl main # Execution start point
  41.        
  42. main:      
  43.         li $v0, 4      # Loads string to print (4)
  44.         la $a0, prompt # Prints String
  45.         syscall        # Executes above code
  46.        
  47.         li $v0, 4           # Loads string to print (4)
  48.         la $a0, newline     # Prints string new line
  49.         syscall             # Executes above code
  50.        
  51.         li $v0, 5           # Gets user integer input (5)
  52.         syscall             # Executes above code
  53.        
  54.         move $t0, $v0       # Stores v0 register in t0
  55.         blez $t0, invalid   # If t0 is less than or equal to 0 Goes to label invalid for user to input another integer
  56.         li $v0, 5           # Gets user input (5)
  57.         syscall             # Executes above code
  58.        
  59.         move $t1, $v0       # Stores v0 in t1
  60.         blez $t1, invalid   # If t1 is less than or equal to 0 Goes to label invalid for user to input another integer
  61.         li $v0, 5           # Gets user input (5)
  62.         syscall             # Executes above code
  63.        
  64.         move $t2, $v0       # Stores v0 in t2
  65.         blez $t2, invalid   # If t2 is less than or equal to 0 Goes to label invalid for user to input another integer
  66.        
  67.         li $v0, 4           # Loads string to print (4)
  68.         la $a0, newline     # Prints string new line
  69.         syscall             # Executes above code
  70.        
  71.         add $t3, $t0, $t1   # Adds t0 and t1 and stores in t3
  72.         bge $t2, $t3, notTri    # if t3 is bigger than t2, go to label not triangle (notTri)
  73.         add $t4, $t0, $t2   # Adds t0 and t2 and stores in t4
  74.         bge $t1, $t4, notTri    # if t4 is bigger than t1, go to label not triangle (notTri)
  75.         add $t5, $t1, $t2   # Adds t1 and t2 and stores in t5
  76.         bge $t0, $t5, notTri    # if t0 is bigger than t5, go to label not triangle (notTri)
  77.        
  78.         b yesTri # if passes all above tests go to yes it is a triangle (yesTri)   
  79.  
  80. # invalid label    
  81. invalid:   
  82.         li $v0, 4     # Loads string to print (4)
  83.         la $a0, error # Prints error string (invalid input)
  84.         syscall       # Executes above code
  85.         b main        # Goes back to main label
  86.  
  87. # notTriangle label
  88. notTri:    
  89.         li $v0, 4     # Loads string to print (4)
  90.         la $a0, nope  # Prints not a triangle string
  91.         syscall       # Executes above code
  92.         b loop        # Goes to loop label (to ask user if they wish to go again)
  93.  
  94. # yesTri label (if inputted lengths are a triangle)
  95. yesTri:    
  96.         li $v0, 4    # Loads string to print (4)
  97.         la $a0, yes  # Prints string (yes it is a triangle)
  98.         syscall      # Executes above code
  99.         b loop       # Goes to loop label (to ask user if they wish to go again)
  100.  
  101. # Loop label (prompts user to go again)    
  102. loop:      
  103.         li $v0, 4       # Loads string to print (4)
  104.         la $a0, format  # Prints string (divider line)
  105.         syscall         # Executes above cude
  106.        
  107.         li $v0, 4        # Loads string to print (4)
  108.         la $a0, continue # Prints string (prompts user to choose to go again)
  109.         syscall          # Executes above code
  110.        
  111.         li $v0, 5        # Gets user integer input (5)
  112.         syscall          # Executes above code
  113.         move $t7, $v0    # stores v0 in t7
  114.        
  115.         li $v0, 4        # Loads string to print
  116.         la $a0, format   # Prints string (divider line)
  117.         syscall          # Executes above code
  118.        
  119.         blez $t7, end       # if input is less than or equal to 0 , goes to end label (to end the program)
  120.         bge $t7, 1, main    # if input is greater or equal to 1, goes to main label (loops again - go again)
  121.  
  122. # End label (ends program)
  123. end:       
  124.         li $v0, 10     # Loads exit program      
  125.         syscall        # Executes above code (and ends)
Advertisement
Add Comment
Please, Sign In to add comment