+1 (315) 557-6473 

Math Quiz in MIPS Assembly Language Assignment Sample

You need to write a program that asks random math questions in MARS. It should ask +,-,* and % questions with values between 1 to 20. After you finish the quiz, it should show many you got right as a percentage value. For more MIPS assembly language assignments contact us for a quote,

Solution:

.data
title: .asciiz “Hello, welcome to MathQuiz, here is your first problem:\n”
labelSum: .asciiz ” + ”
labelDiff: .asciiz ” – ”
labelProd: .asciiz ” * ”
labelMod: .asciiz ” % ”
labelValid: .asciiz “Correct!\n”
labelInvalid: .asciiz “Incorrect!\n”
newLine: .asciiz “\n”
question: .asciiz “? ”
askOp: .asciiz “What is ”

resultMsg1: .asciiz “\nYou solved ”
resultMsg2: .asciiz ” math problems and got ”
resultMsg3: .asciiz ” correct and ”
resultMsg4: .asciiz ” incorrect, for a score of ”
resultMsg5: .asciiz “%.\nThanks for playing!\n”

.text
.globl main
main:
# Seed our random generator using time
li $v0, 30 # Prepare system call to get time
syscall # Get the time

move $t0, $a0 # Get the time

li $a0, 1 # We use $a0 for generating random
move $a1, $t0 # Use time data to seed random
li $v0, 40 # Prepare system call to seed time
syscall # Seed the random generator

# Print initial message
li $v0, 4 # Prepare system call for print string
la $a0, title # Prepare string to print
syscall # Print the string

li $s0, 0 # s0 will count the number of math problems solved
li $s1, 0 # s1 will count the number of math problems solved correctly
li $s2, 0 # s2 will count the number of math problems solved incorrectly
mathLoop:

# Generate a random number for first operand
li $a0, 1 # We use $a0 for generating random
li $a1, 21 # Set the random generator’s upper bound value (max value 20)
li $v0, 42 # Prepare system call to produce a random number
syscall # Create a random number, $a0 holds the random number
move $t1, $a0 # Store the first operand value at $t1

# Generate a random number for the second operand
li $a0, 1 # We use $a0 for generating random
li $a1, 21 # Set the random generator’s upper bound value (max value 20)
li $v0, 42 # Prepare system call to produce a random number
syscall # Create a random number, $a0 holds the random number
move $t2, $a0 # Store the second operand value at $t2

# Generate a random operator to use 0 = Add, 1 = Subtract, 2 = Multiply, 3 = Modulo division
li $a0, 1 # We use $a0 for generating random
li $a1, 4 # Set the random generator’s upper bound value (max value 3)
li $v0, 42 # Prepare system call to produce a random number
syscall # Create a random number, $a0 holds the random number
move $t3, $a0 # Store the operator value at $t3

li $v0, 4 # Prepare system call for print string
la $a0, askOp # Prepare string to print
syscall # Print the string

# Print the first operand
li $v0, 1 # Prepare system call for integer print
move $a0, $t1 # Prepare integer to print
syscall # Print the integer

# Print the operator
beq $t3, 0, printAdd
beq $t3, 1, printSubtract
beq $t3, 2, printMultiply
beq $t3, 3, printModulo
b afterOperatorPrint

printAdd:
# Before printing, initialize the expected sum to $t4
add $t4, $t1, $t2

li $v0, 4 # Prepare system call for print string
la $a0, labelSum # Prepare string to print
syscall # Print the string
b afterOperatorPrint

printSubtract:
# Before printing, initialize the expected difference to $t4
sub $t4, $t1, $t2

li $v0, 4 # Prepare system call for print string
la $a0, labelDiff # Prepare string to print
syscall # Print the string
b afterOperatorPrint

printMultiply:
# Before printing, initialize the expected sum to $t4
mul $t4, $t1, $t2

li $v0, 4 # Prepare system call for print string
la $a0, labelProd # Prepare string to print
syscall # Print the string
b afterOperatorPrint

printModulo:
# Before printing, initialize the expected modulo to $t4
div $t1, $t2
mflo $t4 # Extract the quotient

li $v0, 4 # Prepare system call for print string
la $a0, labelMod # Prepare string to print
syscall # Print the string
b afterOperatorPrint

afterOperatorPrint:

# Print the second operand
li $v0, 1 # Prepare system call for integer print
move $a0, $t2 # Prepare integer to print
syscall # Print the integer

# Print a question mark
li $v0, 4 # Prepare system call for print string
la $a0, question # Prepare string to print
syscall # Print the string

li $v0, 5 # Prepare system call for user input (code 5)
syscall # Capture entered input
move $t5, $v0 # Store the captured value to register $t5

beq $t5, -1, endInput # if the user entered -1, end the program

addi $s0, $s0, 1 # increment number of solved math problems

# Validate the user’s answer
beq $t4, $t5, printValid

printInvalid:
# Tell user their answer is incorrect and print out the correct answer
li $v0, 4 # Prepare system call for print string
la $a0, labelInvalid # Prepare string to print
syscall # Print the string

addi $s2, $s2, 1 # increment number of incorrectly solved math problems
b mathLoop

printValid:
# Tell user their answer is correct
li $v0, 4 # Prepare system call for print string
la $a0, labelValid # Prepare string to print
syscall # Print the string

addi $s1, $s1, 1 # increment number of correctly solved math problems
b mathLoop

endInput:
# Print the end message
li $v0, 4 # Prepare system call for print string
la $a0, resultMsg1 # Prepare string to print
syscall # Print the string

# Print the number of math problems
li $v0, 1 # Prepare system call for integer print
move $a0, $s0 # Prepare integer to print
syscall # Print the integer

# Print the second part of end message
li $v0, 4 # Prepare system call for print string
la $a0, resultMsg2 # Prepare string to print
syscall # Print the string

# Print the number of correctly solved math problems
li $v0, 1 # Prepare system call for integer print
move $a0, $s1 # Prepare integer to print
syscall # Print the integer

# Print the third part of end message
li $v0, 4 # Prepare system call for print string
la $a0, resultMsg3 # Prepare string to print
syscall # Print the string

# Print the number of incorrectly solved math problems
li $v0, 1 # Prepare system call for integer print
move $a0, $s2 # Prepare integer to print
syscall # Print the integer

# Print the fourth part of end message
li $v0, 4 # Prepare system call for print string
la $a0, resultMsg4 # Prepare string to print
syscall # Print the string

# calculate percentage
li $t0, 100 # load t1 to multiply by 100
mult $t0, $s1 # multiply correct count by 100
mflo $t0 # get multiplication result in t0
div $t0, $s0 # divide correct answers*100 over total answers to get percentage
mflo $t0 # get quotient in $t0

# Print percentage of correctly solved math problems
li $v0, 1 # Prepare system call for integer print
move $a0, $t0 # Prepare integer to print
syscall # Print the integer

# Print the last part of end message
li $v0, 4 # Prepare system call for print string
la $a0, resultMsg5 # Prepare string to print
syscall # Print the string

return:
li $v0, 10 # Prepare system call to end program
syscall # End program