flat assembler
Message board for the users of flat assembler.

 Index > Main > imul vs. mul
Author
Plue

Joined: 15 Dec 2005
Posts: 151
Plue
I'm a novice, and the difference (or rather the lack of such) between imul and mul is bothering me.

Using mul:
Code:
```!mov [v_usa], 8
!mov [v_usb], 8
!mov eax, [v_usb]
!mul [v_usa]
!mov [v_usb], eax    ```
The result stored in v_usb seems to be 64.

Using imul:
Code:
```!mov [v_usa], 8
!mov [v_usb], 8
!mov eax, [v_usb]
!mul [v_usa]
!mov [v_usb], eax    ```
The result stored in v_usb STILL seems to be 64.

----

Using mul:
Code:
```!mov [v_usa], 8
!mov [v_usb], -8
!mov eax, [v_usb]
!mul [v_usa]
!mov [v_usb], eax    ```
Result seems to be -64.

Using imul:
Code:
```!mov [v_usa], 8
!mov [v_usb], -8
!mov eax, [v_usb]
!imul [v_usa]
!mov [v_usb], eax    ```
Result still -64??

What is the difference I am missing?

_________________
Roses are red
Violets are blue
Some poems rhyme
And some don't.
17 Dec 2005, 20:46

Joined: 16 Jun 2003
Posts: 1140
Location: Russian Federation
you should edx too, the result is 64-bit stored in edx:eax

and yes, 8*-8=64

what's with the !?
17 Dec 2005, 20:52
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8000
Location: Kraków, Poland
Tomasz Grysztar
When multiplying N-bit number by other N-bit one, the low N bits of the result are the same no matter whether you do the signed or unsigned multiplication - you can read first chapter of my tutorial, which I have written to explain such things (though I'm not sure whether I wrote it clearly enough there, if you have any suggestions or questions, please let me know).

So both 32-bit MUL and IMUL produce the same low 32 bits of result. For this reason though there are additional variants of IMUL that can store result in any 32-bit register or memory location, there are no such for MUL, since they would do exactly the same. The difference is in case when the result is stored as 64 bits in EDX:EAX, and high 32 bits (in EDX) may differ.
17 Dec 2005, 22:54
Plue

Joined: 15 Dec 2005
Posts: 151
Plue
> you should edx too, the result is 64-bit stored in edx:eax
But what if I only have a 4-byte integer to but the result into?

> and yes, 8*-8=64
But I get -64?

> what's with the !?
Oh, sorry, it's inline assembly from another programming language. Forgot to remove them.
18 Dec 2005, 07:45

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
You should always assume that DWORDxDWORD=QWORD and preserve 8 bytes. If you are really sure that there is no result larger than 4.2billion then you can ignore EDX and use only EAX.
Why imul? Well, you can use all kinds of interesting modifiers
Code:
```;You can only do:
mul     reg
mul     mem
;But with imul you can do:
imul    reg
imul    mem
imul    reg, reg, immediate
imul    reg, mem, immediate
imul    reg, immediate
imul    reg, reg
imul    reg, mem
;So this is perfectly legal:
imul eax,[eax+8*edx+402000h],2147483647 ;13 bytes
;...but this gives an error:
mul eax,[eax+8*edx+402000h],2147483647
```

With imul you can define your source address or register, the multiplicand and the destination. MUL takes eax by default (edx also gets modified) and you can only multiply it with another register or memory address, but not a constant.
19 Dec 2005, 13:50
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum