MIPS: Integer Multiplication and Division

后端 未结 1 614
萌比男神i
萌比男神i 2020-12-24 15:27

So I\'m building a calculator program in MIPS and I\'m trying to write the multiply and divide functions.

Currently I read in the integers in a loop like so:

1条回答
  •  一向
    一向 (楼主)
    2020-12-24 16:03

    To multiply, use mult for signed multiplication and multu for unsigned multiplication. Note that the result of the multiplication of two 32-bit numbers yields a 64-number. If you want the result back in $v0 that means that you assume the result will fit in 32 bits.

    The 32 most significant bits will be held in the HI special register (accessible by mfhi instruction) and the 32 least significant bits will be held in the LO special register (accessible by the mflo instruction):

    E.g.:

    li $a0, 5
    li $a1, 3
    mult $a0, $a1
    mfhi $a2 # 32 most significant bits of multiplication to $a2
    mflo $v0 # 32 least significant bits of multiplication to $v0
    

    To divide, use div for signed division and divu for unsigned division. In this case, the HI special register will hold the remainder and the LO special register will hold the quotient of the division.

    E.g.:

    div $a0, $a1
    mfhi $a2 # remainder to $a2
    mflo $v0 # quotient to $v0
    

    0 讨论(0)
提交回复
热议问题