Haakon Sjøgren
Haakon Sjøgren

Reputation: 5

How do I multiply in assembly?

I'm about to pull my hair out for being stuck on such a trivial problem...

I need to multiply 2 with 3 with 4 with 5 in one statement and store it in the bl register. My code so far is:

TITLE PA2.asm

INCLUDE Irvine32.inc

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode : DWORD

.data
product BYTE ?

.code
main PROC
    product = 2*3*4*5
    mov bl, product

    exit
main ENDP
END main

The error I'm getting is error A2005: symbol redefinition : product

Upvotes: 0

Views: 4280

Answers (2)

Ira Baxter
Ira Baxter

Reputation: 95354

That's because you have defined "product" twice: once as the name of memory location containing a BYTE, and (again, oops) as a "equate" with a specific "compile-time" value. That double definition simply isn't allowed, but that has nothing to do with whether or how you can ask the assembler to accomplish a multiply.

The assembler can accomplish "multiply" for you two different ways:

  • at assembly-time (more generally, at the time you run the assembler), but only with constant values known at assembly-time
  • when your assembled program is executed, with arbitrary runtime values

In your example, you appear to be trying to do assembly-time multiplies. That will work fine if you don't get tangled up in double-definitions. To fix your example, change it as follows:

...
main PROC
   product2 = 2*3*4*5
   mov bl, product2

   exit
main ENDP 

This should stop the double-definitions complaint. Alternatively, you could simply delete the line containing the BYTE declaration; it serves no useful purpose in this program.

However, I suspect that's not what you really want to do. I'm guessing you are a student, and somebody wants you to write a program that multiplies at run time.

In that case, you'll need to use a multiply instruction instead of an equate.

You can do that by writing the following:

          mul   <constant>

which multiplies EAX, at runtime, by a constant value defined at assembly-time. There are other variants of the MUL instruction that multiply by the runtime value of other registers or memory locations; as a student, you should go look this up to understand your choices. This brief discussion may help you understand why it is a little clumsy to use: Intel instruction set: multiply with EAX, EBX, ECX or EDX?. You should be reading the Intel reference manuals if you want useful detail on what the CPU can really do; since you mention "Irvine.32.inc" (my alma mater), I'd guess but don't know that there is documentation related to Irvine32 and the related coursework that you better know. Welcome to studentville, and what the rest of your chosen career is likely to feel like ("don't know the topic? go find out").

Since this appears to be homework, I'm leaving you with the above hint. You'll need some more instructions (not much) to set up EAX with a value to be multiplied, and moving the answer to BL.

Upvotes: 2

User.1
User.1

Reputation: 2642

Assembler can multiply for you.

Try this...

     Mov    BL, (2*3*4*5)

Upvotes: 0

Related Questions