MIPS Addition: How to Add Two Numbers

mips-addition-how-to-add-two-numbers

In this article, we will discuss how we can add two numbers in the MIPS instruction set (MIPS Addition). For MIPS addition, mostly R Format is used which is an opcode ADD. In order to understand MIPS addition, you must have knowledge about the following aspects:

  • MIPS ISA Syntax
  • MIPS read and print value
  • MIPS Opcodes
After the understanding of the above topics, let's move towards the examples of MIPS addition.

MIPS Addition Examples

Q. Write a  MIPS program to obtain 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, three registers are used that are $t1, $t2, and $t3. In the register $t1 and $t2 we have saved the values from the results obtained from the user and in register $t3 we have saved the result of the addition. We will also use add Opcode for addition purpose and arithmetic and logical format as well.
Now, let's take another example of MIPS addition.

Q. Write a MIPS program to take values of your choice 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 have initialized $t0 and $t1 register with 5 values only. We have not used any labels to input values from the user. We have utilized only one label 'message' to display the results.

FAQ

What is MIPS in assembly language?

MIPS assembly language points towards to the assembly language of the MIPS processor. The term MIPS is an acronym that stands for Microprocessor without Interlocked Pipeline Stages, and it js a reduces instruction set architecture that was developed by an organization called MIPS Technologies.

How does SLT work in MIPS?

SLT is a MIPS assembly instruction that stands for 'Set If Less Than'. SLT in MIPS is used for a specific condition. For example, if one value is less than the other value then set the value for a particular register. It can be used with both register or can be utilized with an immediate value.
MIPS Addition: How to Add Two Numbers MIPS Addition: How to Add Two Numbers Reviewed by Tech Boy on September 24, 2019 Rating: 5

No comments:

Powered by Blogger.