Create a calculator which could do the following operations. Such as
i) Addition and subtruction of two single digits
ii) Other than this it could say whether the single digit is Odd or Even.
iii) Put 2 single digits and say whether 1st number is greater than 2nd number, less than 2nd number or equal.
#MIPS
.data
NewLine: .asciiz "\n"
EnterN1: .asciiz "Enter Number 1: "
EnterN2: .asciiz "Enter Number 2: "
Add_Sub: .asciiz "Enter + or -\n"
Addition: .asciiz "Addition: "
Subtruction: .asciiz "\nSubtruction: "
OddNumber: .asciiz " Odd"
EvenNumber: .asciiz " Even"
greater: .asciiz "Number1 is greater than Number2 "
less: .asciiz "Number1 is less than Number2"
equal: .asciiz "Number1 is equal to Number2 "
.text
.globl main
main:
li $v0,4 # write string
la $a0, EnterN1 # "Enter Number 1: "
syscall
li $v0,5 # read_int
syscall # read int number to $v0
add $t1, $0, $v0 # save n1 in $t1
li $v0,4
la $a0, EnterN2 # "Enter Number 2: "
syscall
li $v0,5
syscall # read int number
add $t2, $0, $v0 # save n2 in $t2
_op:
li $v0,4 # write string
la $a0, Add_Sub # "Enter + or -" "
syscall
li $v0,12 # read char
syscall
add $t3, $0, $v0 # save char +/- in $t3
li $v0,4 # write string
la $a0, NewLine
syscall
beq $t3, '+', _addition
beq $t3, '-', _subtruction
j _op
_addition:
li $v0,4
la $a0, Addition # "Addition: "
syscall
add $a0,$zero,$t1 # n1
add $a0,$a0,$t2 # n1 + n2
li $v0, 1 # print integer sum
syscall # make the syscall
j _continue
_subtruction:
li $v0,4
la $a0, Subtruction # "Subtruction: "
syscall
add $a0,$zero,$t1 # n1
sub $a0,$a0,$t2 # n1 - n2
li $v0, 1 # print integer sub
syscall # make the syscall
_continue:
li $v0,4 # load syscall write string into $v0
la $a0, NewLine
syscall
# is N1 Odd or Even
li $v0, 1
add $a0,$zero,$t1 # n1
syscall
andi $t0, $t1, 1
beq $t0, $0, _oddN1
li $v0,4
la $a0, EvenNumber # "Even"
syscall
j _testN2
_oddN1:
li $v0,4
la $a0, OddNumber # "Odd"
syscall
# is N2 Odd or Even
_testN2:
li $v0,4
la $a0, NewLine
syscall
li $v0, 1
add $a0,$zero,$t2 # n2
syscall
andi $t0, $t2, 1
beq $t0, $0, _oddN2
li $v0,4
la $a0, EvenNumber # "Even"
syscall
j _testCmp
_oddN2:
li $v0,4
la $a0, OddNumber # "Odd"
syscall
_testCmp:
li $v0,4 # load syscall write string into $v0
la $a0, NewLine
syscall
li $v0,4 # load syscall write string into $v0
la $a0, EnterN1 # "Enter Number 1: "
syscall
li $v0,5 # load syscall read_int into $v0
syscall # read int number to $v0
add $t1, $0, $v0
li $v0,4
la $a0, EnterN2 # "Enter Number 2: "
syscall
li $v0,5 # load syscall read_int into $v0
syscall # read int number to $v0
add $t2, $0, $v0
beq $t1, $t2, _equal
blt $t1,$t2, _less
li $v0,4
la $a0, greater
syscall
j final
_equal:
li $v0,4
la $a0, equal
syscall
j final
_less:
li $v0,4
la $a0, less
syscall
final: nop
addi $v0, $zero, 10 # Sets $v0 to "10" to select exit syscall
syscall # make the syscall Exit
Comments
Leave a comment