Integer Multiplication and Division in MIPS Assembly Language

In the MIPS Assembly Language, we can perform integer multiplication and division using the mult, multu, div, and divu instructions. These instructions belong to the Arithmetic Core Instruction Set and are used for signed and unsigned integers respectively. The results of these operations are stored in special registers called HI and LO.

In this article, we will cover the syntax and usage of these instructions and provide examples of programs that perform integer multiplication and division.

MIPS Integer Multiplication

The mult and multu instructions are used for signed and unsigned integer multiplication respectively. The generic form of these instructions is as follows:

Signed Integer Multiplication:

mult $Rs, $Rt

Unsigned Integer Multiplication:

multu $Rs, $Rt

When multiplying the contents of two 32-bit registers, the result is a 64-bit value. The higher 32 bits are stored in the HI register, while the lower 32 bits are stored in the LO register. To access these registers, we use the following instructions:

mfhi $Rd     // Move from HI to destination register
mflo $Rd     // Move from LO to destination register

The mfhi means “move from HI” to the destination register. mflo means “move from LO” to the destination register.

Assembly Example: Integer Multiplication Program

Here is an MIPS assembly program that reads two numbers from the user and performs integer multiplication:

.data
Number1: .asciiz "\nPlease Enter the First Number, N1: "
Number2: .asciiz "\nPlease Enter the Second Number, N2: "
Result: .asciiz "\nThe Result of the Product is: "
High: .asciiz "\nContents of Register HI: "
Low: .asciiz "\nContents of Register LO: "

.text
.globl main

main:
    li $v0, 4
    la $a0, Number1
    syscall

    li $v0, 5
    syscall
    add $t0, $v0, $zero

    li $v0, 4
    la $a0, Number2
    syscall

    li $v0, 5
    syscall
    add $t1, $v0, $zero

    mult $t0, $t1

    li $v0, 4
    la $a0, Result
    syscall

    li $v0, 4
    la $a0, High
    syscall

    li $v0, 1
    mfhi $a0
    syscall

    li $v0, 4
    la $a0, Low
    syscall

    li $v0, 1
    mflo $a0
    syscall

End_Prog:
    li $v0, 10
    syscall

How this Code Work?

This MIPS assembly code performs the following tasks:

  1. It prompts the user to enter two numbers (N1 and N2) and calculates their product.
  2. It prints the result of the product as well as the contents of the special-purpose registers HI (high) and LO (low) that store the result of the multiplication.

Here’s a step-by-step explanation of what the code does:

  • The code begins by displaying a message asking the user to enter the first number (N1) using li and la instructions, followed by a system call (syscall) to read the input integer and store it in $t0.
  • Next, it displays a similar message asking the user to enter the second number (N2) and reads the input integer, storing it in $t1.
  • The code then uses the mult instruction to multiply the values in registers $t0 and $t1. This multiplication result is stored in the special-purpose registers HI and LO.
  • It displays a message indicating the start of the result output, followed by another message to display the contents of the HI register. The mfhi instruction loads the content of the HI register into $a0, and a system call is used to print this value.
  • Similarly, it displays a message to show the contents of the LO register. The mflo instruction loads the content of the LO register into $a0, and a system call is used to print this value.
  • Finally, the program exits by setting $v0 to 10 and making a system call to terminate the program.

Overall, this code takes two user-input numbers, multiplies them using the mult instruction, and displays the result along with the contents of the HI and LO registers.

MIPS Integer Division

The div and divu instructions are used for signed and unsigned integer division respectively. The generic form of these instructions is as follows:

Signed Integer Division:

div $Rs, $Rt

Unsigned Integer Division:

divu $Rs, $Rt

Similar to integer multiplication, the HI register stores the remainder of the division and the LO register stores the quotient. We can copy the contents of these registers to other destination registers using the mfhi and mflo instructions:

mfhi $Rd     // Move from HI to destination register
mflo $Rd     // Move from LO to destination register

Assembly Example: Integer Division Program

Here is an MIPS assembly program that reads two numbers from the user and performs integer division:

.data
Dividend: .asciiz "\nPlease Enter the Dividend: "
Divisor: .asciiz "\nPlease Enter the Divisor: "
Result: .asciiz "\nThe Result of the Division is: "
Quotient: .asciiz "\nContents of Register LO (Quotient): "
Remainder: .asciiz "\nContents of Register HI (Remainder): "

.text
.globl main

main:
    li $v0, 4
    la $a0, Dividend
    syscall

    li $v0, 5
    syscall
    add $t0, $v0, $zero

    li $v0, 4
    la $a0, Divisor
    syscall

    li $v0, 5
    syscall
    add $t1, $v0, $zero

    div $t0, $t1

    li $v0, 4
    la $a0, Result
    syscall

    li $v0, 4
    la $a0, Quotient
    syscall

    li $v0, 1
    mflo $a0
    syscall

    li $v0, 4
    la $a0, Remainder
    syscall

    li $v0, 1
    mfhi $a0
    syscall

End_Prog:
    li $v0, 10
    syscall

How Does this Code Work?

This MIPS assembly code performs the following tasks:

  1. It prompts the user to enter two numbers, Dividend and Divisor, and performs integer division.
  2. It prints the result of the division as well as the contents of the special-purpose registers LO (Quotient) and HI (Remainder) that store the result of the division.

Here’s a step-by-step explanation of what the code does:

  • The code begins by displaying a message asking the user to enter the Dividend using li and la instructions, followed by a system call (syscall) to read the input integer and store it in $t0.
  • Next, it displays a similar message asking the user to enter the Divisor and reads the input integer, storing it in $t1.
  • The code then uses the div instruction to perform integer division, dividing the value in register $t0 (Dividend) by the value in register $t1 (Divisor). The quotient is stored in the special-purpose register LO, and the remainder is stored in HI.
  • It displays a message indicating the start of the result output, followed by another message to display the contents of the LO register (Quotient). The mflo instruction loads the content of the LO register into $a0, and a system call is used to print this value.
  • Similarly, it displays a message to show the contents of the HI register (Remainder). The mfhi instruction loads the content of the HI register into $a0, and a system call is used to print this value.
  • Finally, the program exits by setting $v0 to 10 and making a system call to terminate the program.

Overall, this code takes two user-input numbers, performs integer division using the div instruction, and displays the result along with the contents of the LO (Quotient) and HI (Remainder) registers.

Conclusion

In this article, we have explored how to perform integer multiplication and division in the MIPS Assembly Language using the mult, multu, div, and divu instructions. We have provided example programs that demonstrate the usage of these instructions and output the results using the HI and LO registers. By understanding these instructions, you can effectively perform arithmetic operations in MIPS Assembly Language.

You may also like to read:

Leave a Comment