2

I have to solve a MIPS multiplication by hand and I am having trouble.

I have two registers, $8 which holds a two's complement representation of -1073741824 (which is 2^30) and $9 with two's complement of +3, I need to find the result of this MIPS instruction

mult $8, $9

I am pretty lost. Do I need to convert the values to two's complement first and then use binary multiplication?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
drainojunkie
  • 65
  • 1
  • 1
  • 6

3 Answers3

9

The mult instruction is a signed multiplication of two 32 bit registers. It stores the results the the special (Hi,Lo) register which combined gives you a 64 bit result. The reson for this is that when you multiply two 32 bits values the result can be too large to fit in a single 32 bit register.

If the goal of the exercise is to manualy deal with 1s and 0s and do the multiplication yourself then yes you can do that. However you will most likely messup somewhere with that many digits.

Lets see if we can use a little deduction to get an idea of what the result should look like first: In two's complement, the most significant digit is the sign:

  • 0 means +
  • 1 means -

Looking at the operation you know right off that the most significant bit of your result will be a 1. This means that the Hi register's highest bit will be a 1.

This said now lets look at the values we are multiplying (I am omitting the sign for now we will get back to that):

230 x 3 = 230 x (21 + 20)

     = 2^31 + 2^30

We have now turned our multiplication into an addition which is much easier to do. In this case the binary represenation of this value is a 1 at bit 32 and 31 or:

1100 0000 0000 0000 0000 0000 0000 0000

now we need to take this value and turn it back into the negative number representation but now over a 64 bit register: so the 64 bit positive value looks like:

0000 0000 0000 0000 0000 0000 0000 0000 1100 0000 0000 0000 0000 0000 0000 0000

Twos complement works by subtracting the positive number from the max positive number with the same number of bits (all 1s) and then adding one to the result:

example on a smaller scale:

        3 = 0011
       -3 = 1111 - 0011 + 0001 
making -3 = 1101

Now lets apply this to our 64 bit value first let us subrtact the positive result from the max 64 bit int value:

 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
-0000 0000 0000 0000 0000 0000 0000 0000 1100 0000 0000 0000 0000 0000 0000 0000
=1111 1111 1111 1111 1111 1111 1111 1111 0011 1111 1111 1111 1111 1111 1111 1111

adding one to that value makes:

1111 1111 1111 1111 1111 1111 1111 1111 0100 0000 0000 0000 0000 0000 0000 0000

so the registers after the operation will be:

Hi = 1111 1111 1111 1111 1111 1111 1111 1111 
Lo = 0100 0000 0000 0000 0000 0000 0000 0000
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Rob
  • 2,618
  • 2
  • 22
  • 29
  • 1
    This looks like a correct answer. I would like to add that based on my book (Patterson-Hennessy), there is not just one implementation. For example older computers used a single adder (ALU), operated 32 times, while new ones unroll the loop and use 32 adders, thanks to falling prices and smaller sizes of semiconductors. As long as it gets the correct result, then I think it is a correct implementation of the MIPS ISA. Each manufacturer and each model may have a different implementation, but they all fulfill the contract of the instruction set. – Panayiotis Karabassis Jan 15 '13 at 16:40
2

If the numbers you want to multiply are signed you use:

mult $8, $9

If they are unsigned just use the unsigned multiplication instruction:

multu $8, $9

The result of the multiplication will be a 64 bit integer, you can access this result using the mflo and mfhi instructions like this:

MFLO $8
MFHI $9
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
0

Some more general information to this question:

The multiply and divide unit produces its result in two additional registers, hi and lo. These instructions move values to and from these registers. The multiply, divide, and remainder pseudoinstructions that make this unit appear to operate on the general registers move the result after the computation finishes.

A-56:

mult rs, rt

Multiply registers rs and rt. Leave the low-order word of the product in register lo and the high-order word in register hi.

Source: Computer Organization and Design: The Hardware/Software Interface (D. Patterson, J. Hennessy)

See also:

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958