Search code examples
assemblyx86multiplication

How can i multiply two registers and store them in a different register?


So I am attempting and failing to make a GCD program for assembly language(Intel x86, using NASM). Where I keep getting compiler errors is when trying to multiply two registers. I have users values stored in the registers ebx, ecx, and edx. I want to multiply all 3 & store the product of ebx and ecx in ebx, then multiply ebx and edx, and store in ebx, and display the result. Im attempting to do this by using this code imul ebx, ebx, ecx imul ebx, ebx, edx Is this not a valid way to multiply registers?


Solution

  • The 3 operand version of imul only takes an immediate as third operand. Luckily, you can use the 2 operand version, since one of your operands is the same as the destination. Thus:

    imul    ebx, ecx ; ebx *= ecx
    imul    ebx, edx ; ebx *= edx
    

    will do what you want.