flat assembler
Message board for the users of flat assembler.

Index > Main > imul vs. mul

Author
Thread Post new topic Reply to topic
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.
Post 17 Dec 2005, 20:46
View user's profile Send private message Reply with quote
comrade



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

and yes, 8*-8=64

what's with the !?
Post 17 Dec 2005, 20:52
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
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.
Post 17 Dec 2005, 22:54
View user's profile Send private message Visit poster's website Reply with quote
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.
Post 18 Dec 2005, 07:45
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
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 Smile
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.
Post 19 Dec 2005, 13:50
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.