- .data
- numFirst: .word 0 # /****
- numSecond: .word 0 # * Declaring and initializing variables
- numThird: .word 0 # *
- # *
- i: .word 0 # *
- D: .word 15 # ****/
- marks: .word 10, 20, 30, 40, 30, 60, 70, 80, 90, 99, 64, 12, 56, 64, 79 #Declaring and initializing array
- analysis: .space 12 # Reserving space for three 4-byte integers
- AGradePrompt: .asciiz "\nA Grade Marks (70%+) - "
- BGradePrompt: .asciiz "\nB Grade Marks (60%+) - "
- CGradePrompt: .asciiz "\nC Grade Marks (50%+) - "
- string: .asciiz "\nNum: "
- string70: .asciiz "70"
- string60: .asciiz "60"
- string50: .asciiz "50"
- .text
- main:
- lw $s1, numFirst # Store the value of 'numFirst' in register $s1
- lw $s2, numSecond # Store the value of 'numSecond' in register $s2
- lw $s3, numThird # Store the value of 'numThird' in register $s3
- lw $t0, D # Number of grades in marks array stored in $t1
- lw $t1, i # Loop Counter stored in register $t0
- addi $t0, $zero, 0 # Offset for array iteration
- arrayLoop:
- beq $t0, 60, exitLoop # Iterates through 15 times
- lw $t6, marks($t0) # Loads the offset value from array into register $t6
- addi $t0, $t0, 4 # Adds 4 to the array offset (1 byte)
- 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()'
- 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'
- 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()'
- 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'
- 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()'
- 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'
- j arrayLoop # Loops back to the beggining of the loop
- grade70:
- addi $s1, $s1, 1 # Adds 1 to $s1 register for each grade over or equal to 70
- j arrayLoop # Loops back to the beginning of loop
- grade60:
- addi $s2, $s2, 1 # Adds 1 to $s2 register for each grade over or equal to 60
- j arrayLoop # Loops back to the beginning of loop
- grade50:
- addi $s3, $s3, 1 # Adds 1 to $s3 register for each grade over or equal to 50
- j arrayLoop # Loops back to the beginning of loop
- exitLoop: # Loop Finished (All 15 Grades have been iterated through)
- # Storing numFirst ($s1), numSecond ($s2), and numThird ($s3) in analysis array
- la $t0, analysis
- sw $s1, ($t0)
- sw $s2, 4($t0)
- sw $s3, 8($t0)
- # Additional prints for testing purposes
- li $v0, 4
- la $a0, AGradePrompt # Prints "\nA Grade Marks (70%+) - "
- syscall
- li $v0, 1
- move $a0, $s1 # Prints the number of grades equal to or over 70%
- syscall
- li $v0, 4
- la $a0, BGradePrompt # Prints "\nB Grade Marks (60%+) - "
- syscall
- li $v0, 1
- move $a0, $s2 # Prints the number of grades equal to or over 60%
- syscall
- li $v0, 4
- la $a0, CGradePrompt # Prints "\nC Grade Marks (50%+) - "
- syscall
- li $v0, 1
- move $a0, $s3 # Prints the number of grades equal to or over 50%
- syscall
- # End of prints
- li $v0, 10 # Terminates program
- syscall
- -----------------------------------------------------------------------
- int numFirst, numSecond, numThird;
- // Declare degree counters
- int i;
- // Declare loop counter
- int D ;
- // Declare number of degree marks to analyse
- int marks[];
- // Array "marks" stores the mark for each student in a course
- int analysis[];
- // Array "analysis" stores the degree counters
- numFirst=0;
- // Initialize number of Firsts counter
- numSecond=0;
- // Initialize number of Seconds counter
- numThird=0;
- // Initialize number of Thirds counter
- D=15;
- // Assume 15 grades in the marks array
- // Analyse each degree mark
- for (i=0; i<D; i++) {
- if(marks[i] >= 70)
- numFirst = numFirst + 1;
- else if(marks[i] >= 60)
- numSecond = numSecond + 1;
- else if(marks[i] >= 50)
- numThird = numThird + 1;
- }
- // Store the degree counters above to the analysis array as follows
- analysis[0]=numFirst ;
- analysis[1]=numSecond;
- analysis[2]=numThird;