SHARE
TWEET

Untitled

a guest Oct 15th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2.  
  3.     # Prompts the user for an Int as the seed for the randomizer
  4.     seedPrompt: .asciiz "Please enter a seed\n"
  5.  
  6.     # Prompts the user for an Int to use as the max possible target
  7.     maxPrompt: .asciiz "Please specify the max number\n"
  8.  
  9.     # Prompts the user for their guess
  10.     guessPrompt: .asciiz "Please enter your guess!\n"
  11.  
  12.     # Informs the user about their guess
  13.     wrongVeryFar: .asciiz "Your guess was very far! Try again!\n"
  14.     wrongFar: .asciiz "Your guess was far! Try again!\n"
  15.     wrongNear: .asciiz "Your guess was near! Try again!\n"
  16.     wrongVeryNear: .asciiz "Your guess was very near! Try again!\n"
  17.     correct: .asciiz "Your guess was Correct! Well Done!!\n"
  18.  
  19.     vn: .float 0.05
  20.     n: .float 0.1
  21.     f: .float 0.25
  22.  
  23. .text
  24.     .globl main
  25.         main:
  26.             # Asks user for max number in guess range
  27.             li $v0, 4
  28.             la $a0, maxPrompt
  29.             syscall    
  30.            
  31.             # Gets user's max number
  32.             li $v0, 5
  33.             syscall
  34.  
  35.             # Moves max number to $t0
  36.             add $t0, $zero, $v0
  37.  
  38.             # Asks the user for their seed
  39.             li $v0, 4
  40.             la $a0, seedPrompt
  41.             syscall
  42.            
  43.             # Gets user's seed
  44.             li $v0, 5
  45.             syscall    
  46.            
  47.             # Moves seed to t1
  48.             add $t1, $zero, $v0
  49.            
  50.             # Sets seed
  51.             li $v0, 40 
  52.             # Creates ID
  53.             li $a0, 0      
  54.             # Moves seed to $a1
  55.             add $a1, $zero, $t1
  56.             syscall
  57.  
  58.             # Random Number Generator
  59.             li $v0, 42 
  60.             # Gives ID to generator
  61.             li $a0, 0  
  62.             # Puts max in $a1              
  63.             add $a1, $zero, $t0
  64.             syscall
  65.  
  66.             # Returns to getLimits
  67.             jal getLimits
  68.  
  69.             # Main guessing and comparing loop
  70.             loop:
  71.                 jal getUserInput
  72.                 add $a0, $zero, $v0
  73.  
  74.                 jal compareNumber
  75.                 b loop
  76.  
  77.         # Determines where very near, near, far and very far lie
  78.         getLimits:
  79.  
  80.             addi $sp, $sp, -4
  81.             sw $ra, 0($sp)
  82.        
  83.             # Moves target to $s0
  84.             add $s0, $zero, $a0  
  85.             add $s1, $zero, $a1  
  86.        
  87.             # Moves target to a float register
  88.             mtc1 $s0, $f0
  89.             mtc1 $t0, $f4
  90.             # Converts target to a float
  91.             cvt.s.w $f0, $f0
  92.             cvt.s.w $f4, $f4
  93.  
  94.             l.s $f1, vn # 5%
  95.             l.s $f2, n # 10%
  96.             l.s $f3, f # 25%
  97.        
  98.             # 5% range of target
  99.             mul.s $f1, $f4, $f1
  100.             # 10% range of target
  101.             mul.s $f2, $f4, $f2
  102.             #25% range of target
  103.             mul.s $f3, $f4, $f3
  104.  
  105.             # 5% above target
  106.             add.s $f5, $f0, $f1
  107.             # 10% above target
  108.             add.s $f6, $f0, $f2
  109.             #25% above target
  110.             add.s $f7, $f0, $f3
  111.             # 5% below target
  112.             sub.s $f8, $f0, $f1
  113.             # 10% below target
  114.             sub.s $f9, $f0, $f2
  115.             #25% below target
  116.             sub.s $f10, $f0, $f3
  117.  
  118.             # Converting ranges to integers
  119.             cvt.w.s $f5, $f5
  120.             cvt.w.s $f6, $f6
  121.             cvt.w.s $f7, $f7
  122.             cvt.w.s $f8, $f8
  123.             cvt.w.s $f9, $f9
  124.             cvt.w.s $f10, $f10
  125.        
  126.             # Moving ranges to integer registers
  127.             mfc1 $s1, $f5
  128.             mfc1 $s2, $f6
  129.             mfc1 $s3, $f7
  130.             mfc1 $s4, $f8
  131.             mfc1 $s5, $f9
  132.             mfc1 $s6, $f10
  133.        
  134.             # Returns to start loop
  135.             lw $ra, 0($sp)
  136.             addi $sp, $sp, 4
  137.             jr $ra
  138.  
  139.         # Gets the user's guess
  140.         getUserInput:
  141.             # Saves return address
  142.             addi $sp, $sp, -4
  143.             sw $ra, 0($sp)
  144.  
  145.             # Asks the user for their guess
  146.             li $v0, 4
  147.             la $a0, guessPrompt
  148.             syscall
  149.  
  150.             # Gets the user's guess
  151.             li $v0, 5
  152.             syscall
  153.  
  154.             # Returns to compare
  155.             lw $ra, 0($sp)
  156.             addi $sp, $sp ,4
  157.             jr $ra
  158.  
  159.         compareNumber:
  160.             # Stores return address
  161.             addi $sp, $sp, -4
  162.             sw $ra, 0($sp)
  163.  
  164.             # Determine's if the user's guess is correct, very near, near, or far
  165.             beq $a0, $s0, Exit # Correct Guess
  166.  
  167.             # Tests if the user's guess is below the target
  168.             blt $a0, $s6, veryFarGuess
  169.             blt $a0, $s5, farGuess
  170.             blt $a0, $s4, nearGuess
  171.  
  172.             # Tests if the user's guess is above the target
  173.             bgt $a0, $s3, veryFarGuess
  174.             bgt $a0, $s2, farGuess
  175.             bgt $a0, $s1, nearGuess
  176.  
  177.  
  178.             # veryNearGuess
  179.             # Tells the user that their guess was within 5% of the target
  180.             li $v0, 4
  181.             la $a0, wrongVeryNear
  182.             syscall
  183.  
  184.             # Returns for another guess
  185.             lw $ra, 0($sp)
  186.             addi $sp, $sp, 4
  187.             jr $ra
  188.  
  189.         nearGuess:
  190.             # Tells the user that their guess was within 10% of the target
  191.             li $v0, 4
  192.             la $a0, wrongNear
  193.             syscall
  194.  
  195.             # Returns for another guess
  196.             lw $ra, 0($sp)
  197.             addi $sp, $sp, 4
  198.             jr $ra
  199.  
  200.         farGuess:
  201.             # Tells the user that their guess was within 25% of the target
  202.             li $v0, 4
  203.             la $a0, wrongFar
  204.             syscall
  205.  
  206.             # Returns for another guess
  207.             lw $ra, 0($sp)
  208.             addi $sp, $sp, 4
  209.             jr $ra
  210.  
  211.         veryFarGuess:
  212.             li $v0, 4
  213.             la $a0, wrongVeryFar
  214.             syscall
  215.  
  216.             # Returns for another guess
  217.             lw $ra, 0($sp)
  218.             addi $sp, $sp, 4
  219.             jr $ra
  220.  
  221.  
  222.         Exit:
  223.             # Tells the user that their guess was correct
  224.             li $v0, 4
  225.             la $a0, correct
  226.             syscall
  227.  
  228.             # Exits the program
  229.             li $v0, 10
  230.             syscall
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top