In: Computer Science
Briefly explain why there are no immediate multiply or divide integer ALU instructions for MIPS.
In MIPS assembly language, there is a multiplication instruction for signed integers, mult, and for unsigned integers multu. Since multiplication takes two 32 bit numbers and returns a 64 bit number, special treatment must be given to the result. The 64 bit product is located in a “product” register. You access the contents of this register using two separate instructions.
mult $s0, $s1 # Multiply the numbers stored in these registers.
# This yields a 64 bit number, which is stored in two
# 32 bits parts: "hi" and "lo"
mfhi $t0 # loads the upper 32 bits from the product register
mflo $t1 # loads the lower 32 bits from the product register
You can only read from the product register. You cannot manipulate it directly. In MARS, the p product register is shown as two 32 bit registers, HI and LO. If the HI register has all 0’s, then the product that is computed can be represented by 32 bits (what’s in the LO register). Otherwise, we have a number that is bigger than the maximum int and it would need to be treated separately.
About division:- To understand division, we need to recall some terminology. If we divide onepositive integer by another, say 78/21, or more generally ”dividend/divisor” then we get a quotient and a remainder, i.e.
dividend = quotient ∗ divisor + remainder
e.g. 78 = 3 ∗ 21 + 15
In MIPS, the divide instruction also uses the HI and LO registers, as follows:
div $s0, $s1 # Hi contains the remainder, Lo contains quotient
mfhi $t0 # remainder moved into $t0
mflo $t1 # quotient moved into $t1
The mult, div, mfhi, mflo are all R format instructions.
In more brief regarding to MIPS programming language:-
MIPS solution is 2 kinds of arithmetic instructions to recognize 2 choices:
–add (add), add immediate (addi), and subtract (sub) cause exceptions on overflow
–add unsigned (addu), add immediate unsigned (addiu), and subtract unsigned (subu) do not cause exceptions on overflow
Unsigned integers commonly used for address arithmetic where overflow ignored . MIPS C compilers always produce addu, addiu, subu.
Since MIPS includes add multiple and division and since immediate can be positive or negative, its range is :
Divide or multiply immediate with a negative number is equivalent to multiply and divide immediate with positive number, so divided or multiplied immediate would be redundant.
NOTE:- This is a very detailed answer and that's why it is lengthy, you must read it thoroughly and if any query raises please let me know in the comment section.