In this section, we will discuss how to add two number in MIPS instruction set(MIPS Addition). For addition MIPS we use R format in which an opcode ADD is used. To understand MIPS addition you must understand the following topics:
After the understanding of above topics proceed towards MIPS addition examples:
MIPS Addition Examples
We will discuss two types of examples here:
Q: Write a MIPS program to Take two values from the user, Add these values and print the output.
PROGRAM:
.data msg1: .asciiz "Enter the first number: " msg2: .asciiz "\nEnter the second number: " result: .asciiz "\nThe result of addition is: " .text li $v0,4 la $a0,msg1 syscall li $v0,5 syscall move $t1,$v0 li $v0,4 la $a0,msg2 syscall li $v0,5 syscall move $t2,$v0 Add $t3,$t1,$t2 li $v0,4 la $a0,msg3 syscall li $v0,1 move $a0,$t3 syscall li $v0,10 syscall
OUTPUT:
Enter the first number: 5 Enter the second number: 5 The result of addition is: 10
Explanation
In the above program we use three registers $t1, $t2 and $t3. In register $t1 and $t2 we saved the values taken from the user and in register $t3 we saved the result of addition. We use Add Opcode for addition and used arithmetic and logical format.
Let’s take another example of MIPS Addition:
Q: Write a MIPS program to Take two values of your choice, Add these values and print the output.
PROGRAM:
.data msg: .asciiz "The result of addition is: " .text li $t0,5 li $t1,5 Add $t3,$t0,$t1 li v0,4 la $a0,msg syscall li $v0,1 move $a0,$t3 syscall li $v0,10 syscall
OUTPUT
The result of addition is: 10
Explanation
In the above program, we initialize $t0 and $t1 register with values 5. We didn’t used labels to input values from the user. We used only one label “msg” to display the results.