The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: shadow on July 02, 2005, 06:05:22 AM

Title: using the mul opcode
Post by: shadow on July 02, 2005, 06:05:22 AM
How do you use the mul opcode?  How do I represent a register pair in MASM?
Title: Re: using the mul opcode
Post by: Jeff on July 02, 2005, 08:38:59 AM
MUL is an unsigned integer multiplication.

it multiplies the accumulator and the register/memory operand and stores the result in the corresponding registers depending on the size of the operand.

MUL r8/m8       ; al *  r8/m8  = ax
MUL r16/m16     ; ax * r16/m16 = dx:ax
MUL r32/m32     ;eax * r32/m32 = edx:eax
Title: Re: using the mul opcode
Post by: Tedd on July 02, 2005, 10:51:20 AM
You don't represent the register pair - it's just part of what the instruction does. You don't get the choice of which the pair is either :P
In terms of handling the result, you have to treat each of the registers separately. So, in the case that your result is so big that the high part of the value is in edx (and lower part in eax) then anything you do with that needs to take this into account - which usually means doing the same (or similar, depending) thing to both.
Title: Re: using the mul opcode
Post by: Mark Jones on July 02, 2005, 12:17:37 PM
See the OPCODES.HLP file. :bg

Then try to make a console app which adds two numbers and displays them. Then try subtract, then multiply. Here's a start. :)
http://www.masmforum.com/simple/index.php?topic=2041.msg16278#msg16278
Title: Re: using the mul opcode
Post by: shadow on July 09, 2005, 01:00:03 AM
Hmmm I know this is a really basic problem... so thanks for taking the time. 
MUL r32/m32     ;eax * r32/m32 = edx:eax

I'm not sure I understand the format here.  Where do I put the multiplicand if I want to multiply 6x7 if I have 6 in eax?
Title: Re: using the mul opcode
Post by: donkey on July 09, 2005, 01:24:00 AM
You cannot multiply the value in EAX by an immediate operator so you would generally place it in a register or if you already have it in memory then use that..

MOV EAX, 6 ; you already have this
MOV ECX, 7 ; here we'll use a register
MUL ECX

.data
Seven DD 7
.code
MOV EAX, 6
MUL DWORD PTR [Seven]


EAX will contain the result in both cases
Title: Re: using the mul opcode
Post by: raymond on July 09, 2005, 02:53:43 AM
QuoteMUL r32/m32     ;eax * r32/m32 = edx:eax

The way to read this is that the content of the EAX register gets multiplied by the content of whichever 32-bit register or 32-bit memory variable (global or local) which is specified in the instruction, and the result would be placed in the EDX:EAX registers. Examples:

mul ebx
mul edi
mul ebp
mul eax


To expand on donkey's example using a memory variable, MASM can accept the simpler following syntax:

mul seven

You can also use dereferenced memory data but you then have to specify its size such as:

mul dword ptr[esi]

Raymond
Title: Re: using the mul opcode
Post by: Jeff on July 09, 2005, 03:02:34 AM
Quote from: donkey on July 09, 2005, 01:24:00 AM
You cannot multiply the value in EAX by an immediate operator so you would generally place it in a register or if you already have it in memory then use that..

MOV EAX, 6 ; you already have this
MOV ECX, 7 ; here we'll use a register
MUL ECX

.data
Seven DD 7
.code
MOV EAX, 6
MUL DWORD PTR [Seven]


EAX will contain the result in both cases

... and EDX will contain zero.   :U

with the MUL op, the resultant value could be at most, two times larger (bitwise) than the operands.  so multiplying two 8bit numbers will produce an 16bit answer.  two 16bit numbers a 32 bit answer, and two 32bit numbers a 64bit answer.

consider the following:

MOV eax,80000000h
MOV ebx,80000000h
MUL ebx
;eax = 00000000h
;edx = 40000000h

MOV eax,0FFFFh
MOV ebx,0FFFFh
MUL bx
;ax = 0001h
;dx = FFFEh


in other words, (e)ax contains the lower (double)word of the result and (e)dx contains the higher (double)word of the result.