Jack P. Oakley Portfolio Game Programmer/Designer

Basic Math Ops

This MIPS program provides a menu for some basic math operations that can be performed on two input values. The downloaded file requires QTSpim or similar interpreter to run.

#MIPS Basic Calculations by Jack P. Oakley

	.data
	FIRSTOUT: .asciiz "Please Enter the First Decimal Number: "
	SECONDOUT: .asciiz "Please Enter the Second Decimal Number: "
	RESULTOUT: .asciiz "The Result Is: "
	OPOUT: .asciiz "Select the Operation (1=Addition, 2=Subtraction, 3=Multiplication, 4=Division, 5=Exponent): "
	DIVOUT: .asciiz "The Remainder is: "
	ERRORMSG: .asciiz "You have exceeded the input range, please try again."
	REPEATQ: .asciiz "Select an option (6=Repeat for New Numbers, 7=Repeat Using Partial Answer, 8=Exit): "
	OVERFLOW: .asciiz "The Result is Invalid Due to Overflow."
	NEWLN: .asciiz "\n"
	
	.text
	
	main:
	
firstEntry:						#Get First Number and check its size <= 16 bits
	la $a0 FIRSTOUT				#Loads address of first output message
	li $v0, 4					#Loads the syscall function to display string
	syscall						#Calls syscall to use the above instructions
	li $v0, 5					#Loads the syscall function to receive input
	syscall						#Calls syscall to use the above instructions
	slti $t0, $v0, -32768		#Sets $t0 if $v0 < -32768
	mul $t1, $v0, -1			#Multiplies $v0 by -1 and stores into $t1
	slti $t1, $t1, -32767		#Sets $t1 if $t1 < -32767
	or $t0, $t0, $t1			#ORs $t0 with $t1
	beq $t0, 1, errMsg1			#Branches to errMsg1 if $t0 == 1
	move $s0, $v0				#Moves $v0 into $s0
	
secondEntry:					#Get Second Number and check its size <= 16 bits
	la $a0 SECONDOUT
	li $v0, 4
	syscall
	li $v0, 5
	syscall
	slti $t0, $v0, -32768
	mul $t1, $v0, -1
	slti $t1, $t1, -32767
	or $t0, $t0, $t1
	beq $t0, 1, errMsg2
	move $s1, $v0
	
	#Get Operation Select
	la $a0 OPOUT
	li $v0, 4
	syscall
	li $v0, 5
	syscall
	move $s2, $v0
	
	bne $s2, 1, subtract
	add $t3, $s0, $s1
	j display
	
subtract:
	bne $s2, 2, multiply
	sub $t3, $s0, $s1
	j display
	
multiply:
	bne $s2, 3, divide
	mul $t3, $s0, $s1
	j display

divide:
	bne $s2, 4, exponent
	beq $s1, 0, ovrflwMsg		#Check divisor, if 0 branch to ovrflwMsg
	div $s0, $s1
	mfhi $t4
	mflo $t3
	j display
	
exponent:
	beq $s1, 0, power0
	beq $s1, 1, power1
	mul $t3, $s0, $s0
	li $t4, 1
	sub $s1, $s1, $t4
	beq $s1, 1, display
	j powerLoop
	
power0:
	li $t3, 1
	j display
	
power1:
	move $t3, $s0
	j display
	
powerLoop:
	mul $t3, $t3, $s0
	sub $s1, $s1, $t4
	bne $s1, 1, powerLoop
	j display
	
errMsg1:
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 ERRORMSG
	li $v0, 4
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	j firstEntry
	
errMsg2:
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 ERRORMSG
	li $v0, 4
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	j secondEntry
	
ovrflwMsg:
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 OVERFLOW
	li $v0, 4
	syscall
	j done
	
display:							#Display blank lines and results
	li $s3, 4294967296
	bgeu $s3, $t3, ovrflwMsg	 	#Branch to ovrflwMsg if $t3 is >= $s3
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 RESULTOUT
	li $v0, 4
	syscall
	move $a0, $t3
	li $v0, 1
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	bne $s2, 4, done
	la $a0 DIVOUT
	li $v0, 4
	syscall
	move $a0, $t4
	li $v0, 1
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 NEWLN
	li $v0, 4
	syscall
	
done:
	#Asks to repeat or quit
	la $a0 NEWLN
	li $v0, 4
	syscall
	la $a0 REPEATQ
	li $v0, 4
	syscall
	li $v0, 5
	syscall
	move $s2, $v0
	beq $s2, 6, firstEntry
	move $s0, $t3
	beq $s2, 7, secondEntry
	li $v0, 10
	syscall

#EOF