Pseudo-instruction for unit in MIPS

What is a pseudo-division instruction in MIPS? Like the pseudo-multiplication instruction, this is MIPS "mul", which makes life a little easier. I am using QT Spim.

Hello

+4
source share
1 answer

To perform integer division, you can use div $t1, $t2, $t3 , which sets $t1 to integer division $t2/$t3 or div $t1, $t2, imm , where imm is immediate.

Similarly, to calculate the remainder of integer division, you can use rem $t1, $t2, $t3 or rem $t1, $t2, imm

These pseudo instructions will basically execute div $t1, $t2 , which stores the results in special registers LO and HI (for private and the rest), and then move these values ​​to the target register using mfhi and mflo .

To perform floating point division, you need to use the floating point registers $ f0 - $ f31. When you have floating point numbers stored in some of these registers (correctly encoded as floating point numbers, for example, in $f1 and $f2 ), you issue the instruction div.s $f0, $f1, $f2 so that get into $f0 result is $f1/$f2 . Now, to get the remainder, you can subtract the division result with truncating the result. For instance:

  li $a1, 20 mtc1 $a1, $f1 cvt.sw $f1, $f1 # $f1 = 20 li $a1, 7 mtc1 $a1, $f2 cvt.sw $f2, $f2 # $f2 = 7 div.s $f0, $f1, $f2 # $f0 = 20/7 trunc.ws $f3, $f0 cvt.sw $f3, $f3 # $f3 = trunc(20/7) sub.s $f4, $f0, $f3 # $f4 = remainder of 20/7 
+5
source

Source: https://habr.com/ru/post/1447249/


All Articles