Computer Architecture - Lab 5: Integer Multiplication, Division and Arithmetic Shift Instructions
Computer Architecture - Lab 5: Integer Multiplication, Division and Arithmetic Shift Instructions
Computer Architecture - Lab 5: Integer Multiplication, Division and Arithmetic Shift Instructions
1 Instruction
This lab topics:
• Integer multiplication and division
The two 8-bit operands result in a 15-bit product. Also shown is the same product done
with base 16 and base 10 notation.
The multiply unit of MIPS contains two 32-bit registers called hi and lo. These are not
general purpose registers. When two 32-bit operands are multiplied, hi and lo hold the 64 bits
of the result. Bits 32 through 63 are in hi and bits 0 through 31 are in lo.
MIPS Multiplication
Here are the instructions that do this. The operands are contained in general-purpose
registers.
There is a multiply instruction for unsigned operands, and a multiply instruction for signed
operands (two’s complement operands).
There are two instructions that move the result of a multiplication into a general purpose
register:
The hi and lo registers cannot be used with any of the other arithmetic or logic instructions.
If you want to do something with a product, it must first be moved to a general purpose register.
However there is a further complication on MIPS hardware:
Rule: Do not use a multiply or a divide instruction within two instructions after mflo or
mfhi. The reason for this involves the way the MIPS pipeline works. On the SPIM simulator
this rule does not matter.
Example Program
## newMult.asm
##
## Program to calculate 5*x - 74
##
## Register Use:
## $t0 x
## $t1 result
.text
.globl main
main:
ori $t0, $0, 12 # put x into $t0
2
ori $t2, $0, 5 # put 5 into $t2
mult $t0, $t2 # 5x -> hi & lo
mflo $t3 # $t3 = 5x
addiu $t1, $t3,-74 # $t1 = 5x - 74
## End of file
The div and the divu Instructions
With N-digit integer division there are two results, an N-digit quotient and an N-digit
remainder. With 32-bit operands there will be (in general) two 32-bit results. MIPS uses the
hi and lo registers for the results:
Here are the MIPS instructions for integer divide. The ”u” means operands and results are in
unsigned binary.
.text
.globl main
main:
ori $t1, $0, 8 # put x into $8
ori $t2, $0, 36 # put y into $9
addu $t3, $t2, $t1 # $10 = (y+x)
subu $11, $t2, $t1 # $11 = (y-x)
div $t3, $t4 # hilo = (y+x)/(y-x)
mflo $t3 # $10 = quotient
mfhi $t4 # $11 = remainder
## End of file
3
The sra Instruction MIPS has a shift right arithmetic instruction:
Sometimes you need to divide by two. This instruction is faster and more convenient than
the div instruction.
2 Exercises
1. Exercise 1
Write a program to evaluate a polynomial, similar to newM ult.asm from the chapter.
Evaluate the polynomial:
3x2 + 5x − 12
Pick a register to contain x and initialize it to an integer value (positive or negative) at the
beginning of the program. Assume that x is small enough so that all results remain in the
lo result register. Evaluate the polynomial and leave its value in a register.
Verify that the program works by using several initial values for x. Use x = 0 and x = 1
to start since this will make debugging easy.
Optional: write the program following the hardware rule that two or more instructions
must follow a mflo instruction before another mult instruction. Try to put useful instructions
in the two slots that follow the mflo. Otherwise put no-op instructions, sll $0,$0,0, in
the two slots.
(3x + 7)/(2x + 8)
Verify that the program works by using several initial values for x. Use x = 0 and x = 1 to
start since this will make debugging easy. Try some other values, then check what happens
when x = −4.
3. Exercise 3 Write a program that determines the value of the following expression:
(x ∗ y)/z
Use x = 1600000(= 0x186A00), y = 80000(= 0x13880), and z = 400000(= 0x61A80).
Initialize three registers to these values. Since the immediate operand of the ori instruction
is only 16 bits wide, use shift instructions to move bits into the correct locations of the
registers.
Choose wisely the order of multiply and divide operations so that the significant bits always
remain in the lo result register.
4
4. Exercise 4
Write a program that multiplies the contents of two registers which you have initialized
using an immediate operand with the ori instruction. Determine (by inspection) the
number of significant bits in each of the following numbers, represented in two’s complement.
Use the program to form their product and then determine the number of significant bits
in it.
3 References
1. https://chortle.ccsu.edu/AssemblyTutorial/Chapter-14/ass14 1.html