Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster way for any number(16bit) divide by 3 in assembly without DIV opcode

I want to divide a unsigned integer by 3, in 8086 assembly or similar , any way to do it faster which I dont want to use DIV opcode.

like image 225
Kent Liau Avatar asked Jan 21 '26 20:01

Kent Liau


1 Answers

Read the appropriate chapter 10 “Integer division by constants” of the book Hacker's Delight. Bonus content is available for that chapter (but not the chapter itself).

Or use libdivide, a library that will apply known algorithms in a first step to find the proper constants, so that the division by a given denominator is then faster.

As pointed out on the libdivide page, compilers know how to transform divisions by compile-time constants into multiplications and shifts, so the simplest way may just be to use a compiler. I would do it for you but I do not have a 16-bit compiler. If done with a 32-bit compiler, the result is as follows:

    movw    $-21845, %ax
    mulw    8(%ebp)
    andl    $65534, %edx
    movl    %edx, %eax
    shrl    %eax

For the C function:

int f(unsigned short d)
{
  return d / 3;
}
like image 118
Pascal Cuoq Avatar answered Jan 24 '26 17:01

Pascal Cuoq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!