News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

using the mul opcode

Started by shadow, July 02, 2005, 06:05:22 AM

Previous topic - Next topic

shadow

How do you use the mul opcode?  How do I represent a register pair in MASM?

Jeff

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

Tedd

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.
No snowflake in an avalanche feels responsible.

Mark Jones

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
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

shadow

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?

donkey

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
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

raymond

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
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Jeff

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.