The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Smith2008 on April 14, 2009, 04:45:56 AM

Title: How to multiply two big numbers?
Post by: Smith2008 on April 14, 2009, 04:45:56 AM
Hi 2 All.
I`ve got problem in multipling two big numbers.I`ve read iczelion`s tutorial but I`m stuck on this. ::)


;mov eax,dword ptr [edi]        Our first num
;mov ebx,dword ptr [esi]        2nd num
mul ebx


After this we have a carry inside "edx" register.But How we can get our real result.
Any help or hint will be appreciated. :clap:
Title: Re: How to multiply two big numbers?
Post by: MichaelW on April 14, 2009, 05:26:22 AM
The MUL instruction leaves the result in EDX:EAX, meaning that the high-order 32 bits are in EDX and the low-order 32 bits in EAX. You could display the result registers separately, but this makes interpreting a decimal (base 10) result difficult at best (although it will work fine for hex, octal, or binary). Assuming that you are using the MASM32 package, another choice for a console app would be to use the CRT printf function to display the result as a 64-bit integer, something like this:

invoke crt_printf, chr$("%I64d%c"),edx::eax,10
Title: Re: How to multiply two big numbers?
Post by: Smith2008 on April 14, 2009, 05:59:48 AM
Thanks.
Title: Re: How to multiply two big numbers?
Post by: Smith2008 on August 20, 2009, 08:40:20 AM
I have another question.
Is there a function to Convert ascii string signed number into a 64 bit signed integer?

I know the function "atol" makes this for 32 bit signed integer.

Thanks in advance.
Title: Re: How to multiply two big numbers?
Post by: akane on August 20, 2009, 09:13:24 AM
Try with sscanf, it is even better than atol/atof, because it returns the number of fields successfully converted and assigned.
invoke sscanf, string, format, &variable{, &variable, ...}
; example:
invoke sscanf, "12345678901234 0.1", "%I64d %f", offset variable64, offset variableDouble

Eax will be 2 on success (two values returned), 1 if only variable64 was returned, 0 on failure (nothing converted), EOF (-1) if ascii string is too short
Title: Re: How to multiply two big numbers?
Post by: MichaelW on August 20, 2009, 09:17:40 AM
And there is the  CRT_atoi64 function (http://msdn.microsoft.com/en-us/library/hc25t012(VS.71).aspx).

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke crt__atoi64, chr$("-9223372036854775808")
    invoke crt_printf,  cfm$("%I64d\n\n"),edx::eax

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


In case it's not obvious, 64-bit integers are normally returned in EDX:EAX.
Title: Re: How to multiply two big numbers?
Post by: Smith2008 on August 20, 2009, 10:26:43 AM
Thanks for all replies. :clap: