A MIPS calculator

One of my other modules this year included a Computer Architecture course, which included some basic assembly programming in the first part and some hardware basics in the second part. The assembly language we learnt was MIPS, and used the SPIM simulator to test the programming. Our final project was a simple MIPS calculator which I have uploaded below. Got an A grade in this one tooo!

[sourcecode language=”css”]
.data

str0: .asciiz "nWelcome to Interactive Calculator 1.0"
str1: .asciiz "nn Enter first value: "
str2: .asciiz " Enter (+, -, /, *): "
str3: .asciiz "n Enter second value: "
str4: .asciiz "n Invalid Operator! Try again. "
str5: .asciiz "n Answer is: "
str6: .asciiz "nn Another Calculation? y, n: "
str7: .asciiz "nn Invalid input! Please enter y or n."
str8: .asciiz "nn Calculator Terminated."

.text
.globl main

main:
addi $v0, $0, 4

la $a0, str0

syscall #printing str0

calc:

addi $v0, $0, 4

la $a0, str1

syscall #printing str1

addi $v0, $0, 6

syscall
mov.s $f1, $f0 #reading and storing first value

addi $v0, $0, 4

la $a0, str2

syscall #printing st2

addi $v0, $0, 12

syscall #reading operator

add $a1, $v0, $0 #storing operator

addi $v0, $0, 4

la $a0, str3
syscall #printing str3

addi $v0, $0, 6

syscall
mov.s $f2, $f0 #reading and storing second value

addi $9, $0, 0x2b
beq $a1, $9, addIt #checking if +

addi $9, $0, 0x2d
beq $a1, $9, subIt #checking if –

addi $9, $0, 0x2a
beq $a1, $9, mulIt #checking if *

addi $9, $0, 0x2f
beq $a1, $9, divIt #checking if

addi $v0, $0, 4

la $a0, str4

syscall #printing str4
j calc

addIt:
add.s $f12, $f1, $f2 #adding values
j printAnswer

subIt:
sub.s $f12, $f1, $f2 #subtracting values
j printAnswer

mulIt:
mul.s $f12, $f1, $f2 #multiplying values
j printAnswer

divIt:
div.s $f12, $f1, $f2 #dividing values
j printAnswer

printAnswer:
addi $v0, $0, 4

la $a0, str5

syscall #printing str5

li $v0, 2
syscall #printing answer
j anotherCalc

anotherCalc:
addi $v0, $0, 4

la $a0, str6

syscall #printing str6

addi $v0, $0, 12

syscall #reading command

add $a1, $v0, $0 #storing command

addi $9, $0, 0x79
beq $a1, $9, calc #checking if y

addi $9, $0, 0x6e
beq $a1, $9, exitApp #checking if n

addi $v0, $0, 4

la $a0, str7

syscall #printing str7
j anotherCalc

exitApp:
addi $v0, $0, 4

la $a0, str8

syscall #printing str8
jr $31 # Exit main subroutine.
[/sourcecode]

Also read...

Comments

    • the calculator was suppose to complete one calculation and ask for another. that is why it jumps to anothercalc, which is short for another calculation. So in the “anotherCalc” function if you press ‘y’, it will go back into the beginning, and ‘n’ will terminate the program.

      Reply
  1. I have been asked to show the MIPS assembly code for addition in this project, So can you please tell me what is the MIPS code in ur code for addition , subtraction etc.

    Show MIPS assembly codes like:
    e.g. lw $t0, 3 # or lw $t0, X
    lw $t1, 5 # or lw $t1, Y
    add $s0, $t0, $t1
    sw $s0, $s3

    Reply
    • the program above is has been divided into different functions so as to remove repeating code. The code for just an addition would be:

      addi $v0, $0, 6
      syscall
      mov.s $f1, $f0 #reading and storing first value

      addi $v0, $0, 6
      syscall
      mov.s $f2, $f0 #reading and storing second value

      add.s $f12, $f1, $f2 #adding values

      At the end of these instructions the answer will reside in $f12, which can then be printed out.

      Reply
  2. i need help to make calculater in mips by using mars like this =>
    first number:1
    second number:1
    then operand :+
    and after that give result by push and pop ???

    Reply
  3. I have been checking out a few of your posts and i must say pretty nice stuff. I will make sure to bookmark your website.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *