Integer Multiplication and Division

Introduction

Multiplication and division are integer operations that are important but take many steps.

Let's think about multiplication briefly, before introducing the instructions.

Suppose you want to multiply two 3-digit base ten numbers. What's the maximum number of digits needed? You think about multiplying the two largest numbers possible, which is 999 times 999. This result is smaller than 1000 times 1000 which is 1,000,000 and uses 7 digits as a result. Thus, 999 times 999 must have 6 digits at most.

This gives as a rule that multiplying an m digit number by an n digit number results in a m + n digit number, at most.

Thus, when you multiply two 32 bit binary numbers, the result can be, at most, 32 + 32 = 64 bits.

Example and Semantics

The generic form of the mult (signed 2C integer multiplication) and multu (unsigned UB integer multiplication) is:
mult  $rs, $rt
multu $rs, $rt
mult and multu are R-type instructions where $rd the destination register does not appear. In the instruction encoding, the five bits associated with $rd are set to 0.

The semantics are it below.

 (HI, LO) = R[s] * R[t]
We know that multiplying two 32-bit registers gives us a 64-bit result. The high 32 bits is placed in a register called HI. The low 32 bits is placed in a register called LO.

The generic form of div (signed 2C integer division) and divu (unsigned UB integer division) is:

div  $rs, $rt
divu $rs, $rt
div and divu are R-type instructions where $rd the destination register does not appear. In the instruction encoding, the five bits associated with $rd are set to 0.

The semantics are it below.

 LO = R[s] div R[t]
 HI = R[s] mod R[t]
Thus, LO stores the result of the integer division (i.e., the quotient), while HI stores the remainder. The operation is undefined if the divisor is 0.

To access the HI and LO registers, which are 2 additional registers beyond the 32 registers, specifically used for integer multiplication and division, we use the following instructions:

mfhi  $rd
mflo  $rd
mfhi means "move from HI" to the destination register. mflo means "move from LO" to the destination register.. Oddly enough, this is an R-type instruction where $rs and $rt are all 0's.

Machine Code Representation

Instruction B31-26 B25-21 B20-16 B15-11 B10-6 B5-0
  opcode register s register t register d shift amount function
mult $rs, $rt 000 000 (SPECIAL) - - 00000 00000 011 000
multu $rs, $rt 000 000 (SPECIAL) - - 00000 00000 011 001
div $rs, $rt 000 000 (SPECIAL) - - 00000 00000 011 010
divu $rs, $rt 000 000 (SPECIAL) - - 00000 00000 011 011
mfhi $rd 000 000 (SPECIAL) 00000 00000 - 00000 010 000
mflo $rd 000 000 (SPECIAL) 00000 00000 - 00000 010 010

The dashes are 5-bit encoding of the register number in UB. For example, $r7 is encoded as 00111.

Summary

Integer multiplication and division are important operations, but are slow relative to other instructions. You can't access the result of the multiplication or division for at least two instructions afterwards.

Web Accessibility