Advertisement
Silver_Smoulder

[ASM][Inc]Project 2

Apr 15th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##Write a program that checks if a year is a leap year
  2. #The year can be evenly divided by 4;
  3. #If the year can be evenly divided by 100, it is NOT a leap year, unless;
  4. #The year is also evenly divisible by 400. Then it is a leap year.
  5. #It is to have at least 3 functions
  6. #no function is to call another function (we haven't done that yet)
  7. #at least 1 function must return at least 1 value
  8. #at least 1 function must have at least 1 argument
  9. #at least 1 function must be used more than once
  10. #(these criteria are not mutually exclusive)
  11. #arguments must be passed in "a" registers, returned values must be returned in "v" registers
  12. #The program is to keep running, asking for additional years until a year of 0 is entered.  Any year entered that is a negative number should be flagged as an error.
  13.  
  14.  
  15. ##So, in Python this can be evaluated in like, 5 lines of code, with the main function being
  16. #if ((year%4 == 0 and year%100 != 0) or (year%4 == 0 and year%100 == 0 and year%400 == 0)), and the default should be that the leap year status should be false
  17.  
  18. #so we will have 3 functions: leap_quad, leap_cent, leap_quant, all of which are going to be 'rem'.
  19. #if leap_quad = 1 and leap_cent = 0, it's a leap year; and if leap quad= 0, leap_cent = 0, and leap_quant = 0 then it's a leap year.
  20. #since we need to use the a registers, our success value is if ( a1 (leap_quad) = 1 AND a2 (leap_cent) = 0 ) OR ( a1 = 1 a2 = 1 a3 = 1 )
  21.  
  22. .globl main
  23. .text
  24.  
  25. main:
  26.  
  27.     li $t0, 0 #we define leap-year status to be false
  28.     li $t1, 4 #value of leap_quad
  29.     li $t2, 100 #value of leap_cent
  30.     li $t3, 4000 #value of Leap_quant
  31.     li $t4, 0 #this is going to be our user input
  32.     li $t5, 1 #we will need this to compare thing sin eval
  33.    
  34.     li $v0, 5 #string output
  35.     la $a0, quad
  36.     syscall
  37.    
  38.     li $v0, 5 #user integer input store in t0
  39.     syscall
  40.     move $t4, $v0
  41.    
  42. leap_quad:
  43.     $a1, 1 #we have to assume it's true, since we're manipulating the exit conditions
  44.     rem $s0, $t4, $t1 #we see if there is a remainder when we divide by 4
  45.     beqz $s0, leap_cent #if there is no remainder, go to the next function, which will test to see if it's divisible by 100
  46.     $a1, 0 #if it's not divisible by 4, then this evaluates to false
  47.  
  48. leap_cent:
  49.     $a2, 1 #assume it's true
  50.     rem $s1, $t4, $t2 #test to see if there's a remainder when we divide by 100
  51.     beqz $s1, leap_quant #if there is a remainder, go to the next function, which will test if it's divisible by 400
  52.     $a2, 0 #if it's not divisible by 100, this becomes false
  53.     j eval #if we don't go to leap_quant, we can skip it because if it's divisible by 4 and not divisible by 100 then it's a leap year anyway
  54.  
  55. leap_quant:
  56.     $a3, 1 #assume it's true
  57.     rem $s2, $t4, $t3 #test to see if it's divisible by 400
  58.     beqz $s2, eval #if it is divisible by 400, we can go ahead and evaluate it
  59.     $a3, 0 #if it's not divisible by 400, this becomes false
  60.    
  61. eval:
  62.     beqz $a1, fail #if it's not divisible by 4, it will fail
  63.     beqz $a2, pass_final #if it is divisible by 4 AND NOT divisible by 100, then it IS a leap year
  64.    
  65.     beq $a1, $t5, pass_1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement