Instructions



Source Code
.globl main
main:
menu:
li $t0,0 # initialize score to 0
# print menu title
la $a0 menus # load string address
li $v0,04 # syscall number to print a string
syscall # print the string
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print first option
la $a0 first # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print second option
la $a0 second # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print third option
la $a0 third # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print fourth option
la $a0 fourth # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print fifth option
la $a0 fifth # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print sixth option
la $a0 sixth # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# prompt user to enter a choice number
la $a0 choice # load string address
li $v0,04 # syscall number to print a string
syscall
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# read choice number from user
li $v0, 05 # syscall number to read a number
syscall # read the number
move $t1,$v0 # save choice number in t1
generate:
# generate first random number
li $a1,10 # generate a number between 0 and 9
li $v0, 42 # syscall number to generate random number
syscall # generate the number
move $t2,$a0 # save random number in t2
# generate second random number
li $a1,10 # generate a number between 0 and 9
li $v0, 42 # syscall number to generate random number
syscall # generate the number
move $t3,$a0 # save random number in t3
beq $t1,1,add # if choice = 1, go to add
beq $t1,2,sub # if choice = 2, go to sub
beq $t1,3,mul # if choice = 3, go to mul
beq $t1,4,div # if choice = 4, go to div
beq $t1,5,mod # if choice = 5, go to mod
beq $t1,6,exp # if choice = 6, go to exp
# if it's any other option, exit
li $v0,10 # syscall number to exit program
syscall # exit program
add:
li $t4, '+' # operation is +
add $t5, $t2, $t3 # calculate the result of the add operation, save in t5
j read # read user guess
sub:
li $t4, '-' # operation is -
sub $t5, $t2, $t3 # calculate the result of the sub operation, save in t5
j read # read user guess
mul:
li $t4, '*' # operation is *
mult $t2, $t3 # calculate the result of the mul operation
mflo $t5 # save multiplication result in t5
j read # read user guess
div:
beqz $t3, generate # if divisor is zero, generate another pair of numbers
mult $t2, $t3 # multiply to get an exact division all the time
mflo $t2 # use multiplication as first operand
li $t4, '/' # operation is /
div $t2, $t3 # calculate the result of the div operation
mflo $t5 # save division result in t5
j read # read user guess
mod:
beqz $t3, generate # if divisor is zero, generate another pair of numbers
li $t4, '%' # operation is %
div $t2, $t3 # calculate the result of the mod operation
mfhi $t5 # save remainder result in t5
j read # read user guess
exp:
andi $t3, $t3, 3 # use exponents between 0 and 3
li $t4, '^' # operation is ^
li $t5, 1 # initialize result to 1
beqz $t3, read # if exponent is zero, end
move $t6, $t3 # copy exponent to t6
exploop:
mult $t5, $t2 # else, multiply by base again
mflo $t5 # save result in t5
addi $t6, $t6, -1 # decrement exponent
bnez $t6, exploop # repeat while exponent is not zero
read:
# print the first generated number
move $a0, $t2 # load first generated number
li $v0,1 # syscall number to print an integer
syscall # print the number
# print a space
li $a0,' ' # load space character
li $v0, 11 # syscall number to print a character
syscall # print the character
# print the operation
move $a0,$t4 # load selected operation character
li $v0, 11 # syscall number to print a character
syscall # print the character
# print a space
li $a0,' ' # load space character
li $v0, 11 # syscall number to print a character
syscall # print the character
# print the second generated number
move $a0, $t3 # load second generated number
li $v0,1 # syscall number to print an integer
syscall # print the number
# print a space
li $a0,' ' # load space character
li $v0, 11 # syscall number to print a character
syscall # print the character
# print an equal sign
li $a0,'=' # load the equal character
li $v0, 11 # syscall number to print a character
syscall # print the character
# print a space
li $a0,' ' # load space character
li $v0, 11 # syscall number to print a character
syscall # print the character
# read user guess
li $v0, 05 # syscall number to read an integer
syscall # read integer
bne $t5,$v0,gameover # if the result is not equal to calculation it's game over
score:
addi $t0,$t0,1 # else, increment score
j generate # generate another pair
gameover:
# print wrong answer
la $a0 wrong # load message string address
li $v0,04 # syscall number to print a string
syscall # print the string
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print end score message
la $a0 result # load address of message
li $v0,04 # syscall number to print a string
syscall # print the string
# print the score
move $a0,$t0 # load current score
li $v0,01 # syscall number to print an integer
syscall # print the number
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print game over message
la $a0 end # load address of message
li $v0,04 # syscall number to print a string
syscall # print the string
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# print prompt to ask if user wants to play again
la $a0 gameovermenu # load address of message
li $v0,04 # syscall number to print a string
syscall # print the string
# print a newline
li $a0, 10 # load newline character
li $v0, 11 # syscall number to print a character
syscall # print the newline
# read number from user
li $v0, 05 # syscall number to read an integer
syscall # read integer
beq $v0,1,menu # if user selected 1, repeat option menu
stop:
li $v0,10 # syscall number to exit program
syscall # exit program
.data
menus: .asciiz "List of the number associated with the operations:"
first: .asciiz "1 => Add"
second: .asciiz "2 => Sub"
third: .asciiz "3 => Multiplication"
fourth: .asciiz "4 => Divide"
fifth: .asciiz "5 => Modulo"
sixth: .asciiz "6 => Exponent"
choice: .asciiz "Enter your choice"
wrong: .asciiz "Wrong Answer"
end: .asciiz "Game Over"
gameovermenu: .asciiz "Do you want to Play Again 1 => Yes 2 => No"
result: .asciiz "Your Score is: "