News:

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

mul and div

Started by sportskid300, June 14, 2007, 12:29:49 AM

Previous topic - Next topic

sportskid300

How do these two functions work?
imul makes more sense, but idiv, div, and mul are single string functions.
How do I use them?
I can't wait to make windows...

dsouza123

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.

dsouza123

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

Tedd

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