Multiplication and Division


Multiplication

When multiplying, the product can be much larger than the numbers being multiplied. For example, 999 * 999 = 998001: the product of two 3 digit numbers can have as many as 6 digits. The rule is that when multiplying an M digit number by an N digit number results in a product that has at most M+N digits. Thus when we multiply two registers, which are 32 bits each, the product could have as many as 64 bits.

The mult instruction multiplies two registers, which are 32 bits each, and creates a 64 bit product. The product is placed in two special registers called high and low. Each of these registers is 32 bits wide; high contains the high order 32 bits of the product, and low contains the low order 32 bits of the product. Since these registers always contain the product, they don't need to be mentioned in the mult instruction. The format of the mult instruction is

   mult     Rt, Rs

For example, the following instruction multiplies $t0 and $t1:

   mult     $t0, $t1

Division

The div instruction performs integer division. The div instruction divides two registers, which are 32 bits each, and calculates the quotient and remainder. It also uses the high and low registers for its results, storing the quotient in low and the remainder in high. The format of the instruction is

   div     Rt, Rs

where Rt is divided by Rs. For example, the following instruction divides $t0 by $t1:

   div     $t0, $t1

Accessing High and Low

The instructions mfhi (move from high) and mflo (move from low) copy values from high or low to another register. The formats are

   mfhi     Rt
   mflo     Rt

In each case, Rt is the register that receives a copy of the value in high or low. For example

   mfhi     $t5
   mflo     $t6

copies the value from high into $t5 and copies the value from low into $t6.

Examples

 
Java
 
MIPS
$t5 = $t3 * $t4; mult   $t3, $t4
mflo   $t5
$t5 = $t3 * 10; li     $t4, 10
mult   $t3, $t4
mflo   $t5
$t5 = $t3 / $t4; div    $t3, $t4
mflo   $t5
$t5 = $t3 % $t4; div    $t3, $t4
mfhi   $t5
$t5 = $t3 / 10; li     $t4, 10
div    $t3, $t4
mflo   $t5
$t5 = $t3 % 10; li     $t4, 10
div    $t3, $t4
mfhi   $t5


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2009