Advertisement
SageTheWizard

Triangles.asm

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