Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k 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 Reply

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...