February 20

MIPS Subtraction: How to subtract two numbers with Examples

0  comments

To understand subtraction in MIPS assembly language, you must have the understanding of following topics in MIPS assembly language:

As now you have the basic understanding of the above topics you can learn subtracting two numbers in MIPS assembly language.

MIPS Subtraction Examples

we will discuss two examples, the first in which we will take values from the user and in the second example we will initialize registers with the value of our own choice.

Q: Write a program in MIPS that takes two values from the user and perform subtraction between then also print the subtraction result.

PROGRAM

.data

msg1: .asciiz "Enter the first number: "
msg2: .asciiz "Enter the second number: "
result: .asciiz "The result is: "

.text

li $vo,4 
la $a0,msg1
syscall

li $v0,5
syscall
move $v0,$t1


li $vo,4 
la $a0,msg1
syscall

li $v0,5
syscall
move $v0,$t2

sub $t1,$t1,$t2

li $v0,4
la $a0,result 
syscall

li $v0,1
move $a0,$t1
syscall

li $v0,10
syscall

OUTPUT

Enter the first number: 7
Enter the second number: 3
The result is: 4

EXPLANATION:

In the above MIPS subtraction example, we first ask the user to input the value by printing msg1 and then ask to enter another value by printing msg 2. Then we moved these values to register $t1 and $t2.
After moving the values to the registers, we used the SUB OPCODE to perform subtraction. After then we store the final result in register $a0 to print it.


Q: Write a program in MIPS, take two values of your choice and perform subtraction between then also print the subtraction result.

PROGRAM

.data
.text

li $f1,3
li $f2,6

sub $f2,f2,f1

move $a0,f2

li $v0,1
syscall

li $v0,10
syscall

OUTPUT

3

EXPLANATION:

In the above program, we directly initialized two registers with integer values. We use SUB opcode to perform subtraction process and finally print the result.


That was all about MIPS subtraction process, comment to show me that you are alive!


Tags


You may also like