February 22

MIPS Multiplication: Using MUL, MULT and SLL

0  comments

MIPS multiplication is a little bit tricky as compared to addition and subtraction, but here we will simplify it for you. To learn MIPS multiplication, you must go through the following topics:

MIPS multiplication uses arithmetic and logical format, and it can be performed using two opcode MUL and MULT. Both opcodes have a little bit difference in operation and syntax. We will discuss in detail below:

MIPS Multiplication Using MUL

The following example program describing the functionality of “mul” opcode. The mul instruction needed three operands registers.

MUL can be used in three possible ways:

  • MULTIPLY REGISTERS(mul $t1,$t2,$t3)

The above statement is also known as multiplications with overflow. Note the Hi register will be used to store high-order 32 bits, Lo and $t1 to low order 32 bits of the product of $t2 and $t3(we can use mfhi to access HI, mflo to access Lo)

  • MULTIPLY 16-bit SIGNED IMMEDIATE(mul $t1,$t2,-200)

Note Hi register to store high-order 32 bits, Lo and $t1 to low order 32 bits of the product of $t2 and any 16 bit signed immediate(we can use mfhi to access HI, mflo to access Lo)

  • MULTIPLY 32-bit SIGNED IMMEDIATE(mul $t1,$t2,100021)

Note Hi register to store high-order 32 bits, Lo and $t1 to low order 32 bits of the product of $t2 and any 32 bit signed immediate(we can use mfhi to access HI, mflo to access Lo)

Example Multiplication Program Using MUL

PROGRAM

.data 

m: .asciiz "The result of multiplication is: "

.text

addi $s0, $zero,10
addi $s1, $zero,4

mul $t0, $s0,$s1

li $v0,4
la, $a0,m
syscall

li $v0,1
add $a0, $zero,$t0  
syscall

li $v0,10
syscall

OUTPUT

The result of multiplication is: 40

EXPLANATION:

In the above program, we initialized registers $s0 and $s1 with values 10 and 4. Then we performed multiplication using mul opcode. And at last, we printed the result of the multiplication.

MIPS Multiplication Using MULT

The following example program describing the functionality of “mult” opcode. The mult instruction needed two operands registers.

Example Multiplication Program Using MULT

PROGRAM

.data

n1:	.asciiz "Enter the first value:  "
n2:	.asciiz "Enter the second value:  "
m: .asciiz "The result of multiplication is: "

.text

li $v0,4
la $a0,n1
syscall

li $v0,5	
syscall
move $t0,$v0	

li $v0,4
la $a0,n2
syscall

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

mult $t0,$t1
mflo $s0	

li $v0,1
add $a0, $zero,$s0 
syscall

li $v0,10
syscall

OUTPUT

Enter the first number: 4
Enter the second number: 7
THe result of multiplication is: 28

EXPLANATION:

In the above program, we take two values from the user and then we performed multiplication using mult instruction. If we are multiplying two 32-bit number then the result become 64-bit so we need the first 32-bit result that is in lo(first 32 bit). In order to move the result to a register, we used instruction mflo.

MIPS Multiplication Using SLL

Sll also called shift left logical. It is the most efficient way to multiply the number in power of 2.

Example Multiplication Program Using SLL

PROGRAM

.data

.text

addi  $s0,$zero,2
sll $t0,$s0,1

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

li $v0,10
syscall

OUTPUT

4

EXPLANATION:

We use shift logical left instruction for multiplication. we have stored the value 2 in the register $t0. In line#6 we performed the logical operation. we have given the value 1, so it will be shifted one time.


State your Queries in the comment box!


Tags


You may also like