0

I'm looking at some assembly code for multiplication and am a bit stumped on the output.

move eax, ecx
imul edx

where ecx is 20336 and edx is 1321528399.

The value being returned when i view edx after executing the instruction is 6257, which doesn't make much sense to me.

These are very large numbers, so i figured its likely overflowing -- but i tried applying a mask and didn't arrive at the expected result.

Any clue on how this product can be derived?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Hysii
  • 702
  • 2
  • 10
  • 23
  • 2
    `(20336 * 1321528399) >> 32 = 6257`, the expected result for the high-half of the 32x32 => 64-bit multiply result in EDX:EAX. One-operand `imul` can't overflow the full result, only the low half (i.e. `cdq` would change EDX instead of setting it to the value it already had). – Peter Cordes May 02 '22 at 06:21
  • Thanks @PeterCordes. I'm a n00b, how would i find the lower half? – Hysii May 02 '22 at 06:23
  • See the linked duplicate and my first comment. And read the manual: https://www.felixcloutier.com/x86/imul. BTW, if you only want the low half in the first place, do `imul esi, edi` or whatever instead of using the slow one-operand form of `imul` that has to produce both. – Peter Cordes May 02 '22 at 06:24
  • @Hysii What does eax contain afterwards? – Sebastian May 02 '22 at 06:35

0 Answers0