The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: prodigy on August 06, 2006, 08:44:03 AM

Title: this might sound silly but...
Post by: prodigy on August 06, 2006, 08:44:03 AM
how do you simple math..


I know add, sub...

I don't know mul and div...

Examples???
Title: Re: this might sound silly but...
Post by: Jackal on August 06, 2006, 11:15:28 AM
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.
Title: Re: this might sound silly but...
Post by: dsouza123 on August 06, 2006, 06:07:43 PM
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.