0

Hi I have a question about multiplying register pairs. I have a register pair edx:eax and now I want to multiply it, in my case - by 10. Maximum value which will be the result won't be higher than max value on 64-bits, so no overflow will happen. How do I go about this?

.data
ten dd 10
.code
mul dword PTR ten
;instead of multiplying eax by ten i want to do the same on edx:eax.
roditu
  • 59
  • 6
  • 1
    Multiply separately by 10. Add the relevant parts. – Jester Nov 20 '22 at 13:39
  • You didn't specify what the register pair represents. Is it a high and low part of an unsigned long integer or two independent 32 bit signed ints, or what? In general, adding 2x and 8x, which you can get by bit shifting, might be faster than multiplying by 10. – Simon Goater Nov 20 '22 at 16:00

1 Answers1

2

Multiplying EDX:EAX by 10

Because you state that no overflow can happen, next is the simple solution.

  • multiply the high part (EDX) by 10 and store in a temp register (ECX)
  • multiply the low part (EAX) by 10 and keep the 64-bit result in EDX:EAX
  • join both high parts
imul ecx, edx, 10
mul  DWORD PTR ten
add  edx, ecx
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 2
    On older CPUs it might have been worth avoiding mul / imul in favour of `add`/`adc` (multiply by 2) + 2x `mov` + `shld`/`shl` (multiply by another 4 to get x*8) + `add`/`adc` (`x*2 + x*8 = x*10`). As in [Conversion of huge decimal numbers (128bit) formatted as ASCII to binary (hex)](https://stackoverflow.com/q/57133190) for 128-bit numbers on a 32-bit CPU. But on modern x86, yeah this will be faster. – Peter Cordes Nov 20 '22 at 14:10