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

后端 未结 4 461
误落风尘
误落风尘 2021-01-22 04:33

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

4条回答
  •  失恋的感觉
    2021-01-22 05:11

    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)
    

提交回复
热议问题