0

I got my answer of imul How do you manually calculate imul -1 * 3? but now run into idiv.

For unsigned division, I simply use long division and can get the correct result.

50/4:

        1100 
    ________
100|00110010
      100000
    --------
       10010
       10000
    --------
          10  

For signed division, -8/-4, 1111 1000 idiv 1111 1100, the program result is 0000 0010 (al, quotient).

How do I manually get 0010? What is the rule here?

1111 1000 / 1111 1100 doesn't work apparently. So I tried sign extending the dividend:

                  1 0000 0100
          ___________________
1111 1100|1111 1111 1111 1000
          1111 1100
          -------------------
                 11 1111 1000
                 11 1111 0000
                 ------------
                         1000 

this is also wrong...

An example is much appreciated, if 8 bits example is too tedious, 3 bit or 4 bit examples are ok. :)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Rick
  • 7,007
  • 2
  • 49
  • 79
  • 3
    There may be no easy tricks for division. The plain way is to compute absolute values, do an unsigned division and then apply the combined sign to the result. – teapot418 Jun 02 '23 at 08:46
  • Supporting source: ["uncomplemented form" = absolute value](https://stackoverflow.com/questions/33380736/using-2s-complement-to-perform-binary-division-for-signed-number) – teapot418 Jun 02 '23 at 08:56
  • 1
    If you just want the answer, `-8 / -4` mathematically is `+2`, and the bit-pattern for `+2` is `00000010`. (With remainder 0). If the mathematically-correct answer for 16-bit / 8-bit => 8-bit idiv (`AX / src`) doesn't fit in 8 bits, the instruction raises a #DE exception, so there's no special truncation. If you want to understand how to do signed-binary long division, that's fine, but unnecessary for understanding assembly language, especially for an ISA like x86 that has hardware `div` and `idiv` instructions. For BigInt division, there's a shift-and-subtract that works 1 bit at a time. – Peter Cordes Jun 02 '23 at 09:02
  • 1
    I assume you already searched the web, found https://paulmason.me/blog/2018-05-19-dividing-binary-numbers-part-2 and gave that a read? – Mike 'Pomax' Kamermans Jun 02 '23 at 15:35
  • 1
    https://stackoverflow.com/questions/17217440/how-to-do-64-bit-by-64-bit-division-in-assembly/76332918#76332918 shows the algorithm to do signed integer division the way @teapot418 sees it in [the first comment on this post](https://stackoverflow.com/questions/76388460/how-do-you-manually-calculate-8-idiv-4#comment134700022_76388460). – Sep Roland Jun 02 '23 at 17:17
  • @Mike'Pomax'Kamermans Thank you that's helpful. I didn't find that link at first. – Rick Jun 06 '23 at 04:02
  • @teapot418 I am satisfied with your method and the reference question. Thanks :P – Rick Jun 06 '23 at 04:03
  • @Rick remember that SO only works when questions have answers, so if you have an answer after all these hints, write one up. Or, if you don't feel that's appropriate, deleting the question is fine, too =) – Mike 'Pomax' Kamermans Jun 06 '23 at 04:31
  • If it's a _new_ problem, it's a new post. – Mike 'Pomax' Kamermans Jun 06 '23 at 14:47

0 Answers0