News:

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

this might sound silly but...

Started by prodigy, August 06, 2006, 08:44:03 AM

Previous topic - Next topic

prodigy

how do you simple math..


I know add, sub...

I don't know mul and div...

Examples???

Jackal

to multiply i i use imul... for example


mov eax, 5
imul eax, 2


to divide


   xor edx, edx
   mov eax, YDay
   div dNum7


edx holds the remainder and if you dont clear it with xor then you can get an error later after dividing multiple times. the dNum7 is the number that you are dividing by. the ending result will be in the eax register.

These can also be done with shl and shr but not every number can be done with these i dont think.

dsouza123

For division of a 64 bit (qword) number, edx:eax pair, with the upper 32 bits (upper dword) edx 0 :


mov ecx, 7

mov edx, 0   ; what the less obvious but frequently used   xor edx, edx   does
mov eax, 100
div ecx  ; edx:eax / ecx --> eax gets dividend (integer answer), edx gets the remainder (also an integer)

eax = 14 ; dividend
edx = 2  ; remainder


Dividing by 0, ie if  ecx was 0 instead of 7, will cause an exception.

The upper dword, the value of edx, must be smaller than the divisor
so the answer, dividend, will fit in a dword.


mov ecx, 7

mov edx, 6  ; upper dword is LESS THAN the divisor 7, calculation OK
mov eax, whatever
div ecx


If the upper dword, in the edx:eax pair, is greater than or equal ( >, >=, == ) to the divisor
it will cause an exception.