News:

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

How to multiply two big numbers?

Started by Smith2008, April 14, 2009, 04:45:56 AM

Previous topic - Next topic

Smith2008

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:

MichaelW

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
eschew obfuscation


Smith2008

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.

akane

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

MichaelW

And there is the CRT_atoi64 function.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    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.
eschew obfuscation

Smith2008