Facebook
From Beige Lechwe, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 174
  1.                 .data
  2.  
  3. numFirst:       .word 0         # /****
  4. numSecond:      .word 0         # * Declaring and initializing variables
  5. numThird:       .word 0         # *
  6.                                 # *
  7. i:              .word 0         # *
  8. D:              .word 15        # ****/
  9.                                
  10. marks:          .word 10, 20, 30, 40, 30, 60, 70, 80, 90, 99, 64, 12, 56, 64, 79 #Declaring and initializing array
  11. analysis:       .space 12       # Reserving space for three 4-byte integers
  12.  
  13. AGradePrompt:   .asciiz "\nA Grade Marks (70%+) - "
  14. BGradePrompt:   .asciiz "\nB Grade Marks (60%+) - "
  15. CGradePrompt:   .asciiz "\nC Grade Marks (50%+) - "
  16.  
  17. string:         .asciiz "\nNum: "
  18. string70:       .asciiz "70"
  19. string60:       .asciiz "60"
  20. string50:       .asciiz "50"
  21.  
  22.                 .text
  23. main:
  24.                
  25. lw $s1, numFirst        # Store the value of 'numFirst' in register $s1
  26. lw $s2, numSecond       # Store the value of 'numSecond' in register $s2
  27. lw $s3, numThird        # Store the value of 'numThird' in register $s3
  28.  
  29. lw $t0, D               # Number of grades in marks array stored in $t1
  30. lw $t1, i               # Loop Counter stored in register $t0
  31.  
  32.  
  33. addi $t0, $zero, 0      # Offset for array iteration
  34.  
  35. arrayLoop:
  36.         beq $t0, 60, exitLoop   # Iterates through 15 times
  37.  
  38.         lw $t6, marks($t0)      # Loads the offset value from array into register $t6
  39.        
  40.                                
  41.         addi $t0, $t0, 4        # Adds 4 to the array offset (1 byte)
  42.        
  43.         slti $t7, $t6, 70       # Sets $t7 to 1 if value in register $t6 is less than 70 - in higher-level this is equal to 'if (marks[i] > 70) grade70()'
  44.         beq $t7, $zero, grade70 # Checks if the value set in the previous line is 1 or 0, if it is 0, it branches to label 'grade70'
  45.  
  46.         slti $t7, $t6, 60       # Sets $t7 to 1 if value in register $t6 is less than 70 - in higher-level this is equal to 'if (marks[i] > 70) grade70()'
  47.         beq $t7, $zero, grade60 # Checks if the value set in the previous line is 1 or 0, if it is 0, it branches to label 'grade70'
  48.  
  49.         slti $t7, $t6, 50       # Sets $t7 to 1 if value in register $t6 is less than 70 - in higher-level this is equal to 'if (marks[i] > 70) grade70()'
  50.         beq $t7, $zero, grade50 # Checks if the value set in the previous line is 1 or 0, if it is 0, it branches to label 'grade70'
  51.        
  52.         j arrayLoop             # Loops back to the beggining of the loop
  53.        
  54.  
  55. grade70:
  56.         addi $s1, $s1, 1        # Adds 1 to $s1 register for each grade over or equal to 70
  57.         j       arrayLoop       # Loops back to the beginning of loop
  58.  
  59. grade60:
  60.         addi $s2, $s2, 1        # Adds 1 to $s2 register for each grade over or equal to 60
  61.         j       arrayLoop       # Loops back to the beginning of loop
  62.  
  63. grade50:
  64.         addi $s3, $s3, 1        # Adds 1 to $s3 register for each grade over or equal to 50
  65.         j       arrayLoop       # Loops back to the beginning of loop
  66.  
  67. exitLoop:       # Loop Finished (All 15 Grades have been iterated through)
  68.  
  69.  
  70.         # Storing numFirst ($s1), numSecond ($s2), and numThird ($s3) in analysis array
  71.        
  72.         la $t0, analysis
  73.        
  74.         sw $s1, ($t0)
  75.         sw $s2, 4($t0)
  76.         sw $s3, 8($t0)
  77.  
  78.         # Additional prints for testing purposes
  79.        
  80.         li $v0, 4
  81.         la $a0, AGradePrompt    # Prints "\nA Grade Marks (70%+) - "
  82.         syscall
  83.        
  84.         li $v0, 1
  85.         move $a0, $s1           # Prints the number of grades equal to or over 70%
  86.         syscall
  87.        
  88.         li $v0, 4
  89.         la $a0, BGradePrompt    # Prints "\nB Grade Marks (60%+) - "
  90.         syscall
  91.        
  92.         li $v0, 1
  93.         move $a0, $s2           # Prints the number of grades equal to or over 60%
  94.         syscall
  95.        
  96.         li $v0, 4
  97.         la $a0, CGradePrompt    # Prints "\nC Grade Marks (50%+) - "
  98.         syscall
  99.        
  100.         li $v0, 1
  101.         move $a0, $s3           # Prints the number of grades equal to or over 50%
  102.         syscall
  103.        
  104.         # End of prints
  105.        
  106.         li $v0, 10              # Terminates program                                                                                                                                                          
  107.         syscall
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. -----------------------------------------------------------------------
  117.  
  118.  
  119.  
  120.  
  121.  
  122. int numFirst, numSecond, numThird;
  123.  
  124. // Declare degree counters
  125.  
  126. int i;
  127.  
  128. // Declare loop counter
  129.  
  130. int D ;
  131.  
  132. // Declare number of degree marks to analyse
  133.  
  134.  
  135.  
  136. int marks[];
  137.  
  138. // Array "marks" stores the mark for each student in a course
  139.  
  140. int analysis[];
  141.  
  142. // Array "analysis" stores the degree counters
  143.  
  144.  
  145.  
  146. numFirst=0;
  147.  
  148. // Initialize number of Firsts counter
  149.  
  150. numSecond=0;
  151.  
  152. // Initialize number of Seconds counter
  153.  
  154. numThird=0;
  155.  
  156. // Initialize number of Thirds counter
  157.  
  158. D=15;
  159.  
  160. // Assume 15 grades in the marks array
  161.  
  162. // Analyse each degree mark
  163.  
  164. for (i=0; i<D; i++) {
  165.  
  166.     if(marks[i] >= 70)
  167.  
  168.            numFirst = numFirst + 1;
  169.  
  170.     else if(marks[i] >= 60)
  171.  
  172.            numSecond = numSecond + 1;
  173.  
  174.     else if(marks[i] >= 50)
  175.  
  176.             numThird = numThird + 1;
  177.  
  178. }
  179.  
  180.  
  181.  
  182. // Store the degree counters above to the analysis array as follows
  183.  
  184. analysis[0]=numFirst ;
  185.  
  186. analysis[1]=numSecond;
  187.  
  188. analysis[2]=numThird;