Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - Multiply numbers without using instructions MUL, IMUL, SHL, SHR, LOOP

Is it possible to calculate result of multiplication without using instructions MUL, IMUL, SHL, SHR, LOOP, JMP in x86 assembly language?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code will multiply the contents of the registers ecx and edx and store the result in register eax. The content of the registers ebx and edx is destroyed:

  mov ebx, 1
  mov eax, 0
repeat:
  test ecx, ebx
  jz dontadd
  add eax, edx
dontadd:
  add edx, edx
  add ebx, ebx
  jnz repeat

without ... LOOP

If "LOOP" does not only cover the "LOOP" instruction but any conditional jump instructions:

Doing a multiplication without conditional jump instructions is a bit more difficult but not impossible; the following example does so (Input: ecx and edx, output eax, the content of all registers used will be destroyed):

mov ebx, 1
mov eax, 0
not ecx
  # Repeat the following code 32 times:
mov esi, ebx
and esi, ecx
dec esi
and esi, edx
add eax, esi
add edx, edx
add ebx, ebx
  # (repeat the code here)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...