The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: sportskid300 on June 14, 2007, 12:29:49 AM

Title: mul and div
Post by: sportskid300 on June 14, 2007, 12:29:49 AM
How do these two functions work?
imul makes more sense, but idiv, div, and mul are single string functions.
How do I use them?
Title: Re: mul and div
Post by: dsouza123 on June 14, 2007, 01:09:56 AM
All four are x86 ALU instructions
they typically work with 32 bit (or pairs of 32 bit) registers
or 32 bit memory amounts in some cases.

The opcodes.hlp file that comes with MASM32
give fairly detailed descriptions of all four.

They don't work on strings but on binary values.
Title: Re: mul and div
Post by: dsouza123 on June 14, 2007, 01:24:34 AM
Example

div ecx

It takes the unsigned value held in the register pair edx:eax
with edx the upper 32 bits and eax the lower and divides it
by either a 32 bit register or 32 bits of memory.

If edx == 4, eax == 1 and ecx == 256
the result of the division is eax == 4000000h ( 67108864 ), edx == 1
Title: Re: mul and div
Post by: Tedd on June 14, 2007, 11:27:56 AM
12345h * 6789Ah = ...

mov eax,12345h
mov ecx,6789Ah
mul ecx

;; edx => 00000007h    ;result (upper 32-bits)  ;} combine to make
;; eax => 5CD58F82h    ;result (lower 32-bits)  ;} 64-bit value


IMUL can be used in the same way, but treats numbers with their high-bit set as negatives (in two's complement form); or in the 2-register or 3-register forms, which you know.


0123456789ABCDEFh / 0ECA8642h = ...

mov edx,01234567h    ;dividend (high 32-bits)  ;} combine to make
mov eax,89ABCDEFh    ;dividend (low 32-bits)   ;} 64-bit value
mov ecx,0ECA8642h    ;divisor (only 32-bits)
div ecx

;; eax => 13B13B13h    ;quotient
;; edx => 0A10A109h    ;remainder


IDIV is exactly the same, but with the aforementioned interpretation of negatives.
Title: Re: mul and div
Post by: ecube on June 15, 2007, 06:19:12 PM
http://www.masm32.com/board/index.php?topic=7276.0