5

I am new to assembly language, and I am really confused over multiplying.

I was reading the quick tutorial here (dead link, web archive here)

It says after I use mult $t0, $t1 the results are stored in Hi and Lo, I understand these are special registers for mult and div, but how do I access them?

Lets say I do mult $t0, $t1 where $t0 and $t1 are both 2. How do I get the result? (4)

Nick
  • 1,161
  • 1
  • 12
  • 22
  • There are special instructions for accessing these registers. See [here](http://stackoverflow.com/questions/2320196/in-mips-what-is-hi-and-lo) – pat Mar 23 '14 at 22:35
  • I read that thread, still a little confused, I tried to find an example of multiplication but couldnt, could you tell me how can I access the result of the question above? – Nick Mar 23 '14 at 22:39
  • 1
    Use `mfhi $t0` to move HI to t0, and `mflo $t1` to move LO to t1. Note, you can move HI and LO to any of the GPRS with these instructions. You can also move values into HI and LO with `mthi` and `mtlo`. Check your MIPS instruction reference manual for details. – pat Mar 23 '14 at 22:42

3 Answers3

1

You have to use MFHI and MFLO to move data from HI and LO to general purposes register.

Reference

a.ndrea
  • 536
  • 2
  • 5
  • 19
0

for E.g:

      .globl main
       main:
            li $t0,3
            li $t1,2
            mult $t0,$t1

The mult condition multiplies 2 signed 32 bits and form 64 bit result. To access that first store the values in registers using commands. This stores HI, LO values to general purpose registers.

             mfhi $t2
             mflo $t3

and then print these values using printing statement:

             move $a0,$t2
             li $v0,1
             syscall
 
             move $a0,$t3
             li $v0,1
             syscall  

to get the output on console.

To get the result of multiplication you can use another command i.e.

            mul $t2,$t0,$t1 

where you store the product of values in Register 1 and Register 0 in Register 2.This however corrupts HI and LO.

Sukaina
  • 1
  • 1
0
MULT $t1, $t2 #multiplies 2 registers
MFLO $t3 #stores the product in this register
MFHI $t4 #stores the remainder 

Because there is no modulus command, you can do the multiplication and take whatever result is in MFHI to be the result. For division, it also uses MFLO and MFHI for the result of the operation.

A P
  • 2,131
  • 2
  • 24
  • 36