Search code examples
assemblyx86masm

Initializing Big Numbers with Constants in MASM


I am trying to write some macros in MASM to handle big numbers (i.e. Numbers with values greater than can be stored in registers). However, I have run into a problem with creating bignums from constants at assemble time. It seems that MASM has limits on the size of numerical constants. What I would like to be able to do is something like this:

DECLARE_BIGINT example, 19292109310024103209481293008

But I get this error:

error A2071: initializer magnitude too large for specified size

Which I assume means that the constant was larger than MASM can handle. I thought I might be able to kludge my way around it by declaring the constant as text:

DECLARE_BIGINT example, <19292109310024103209481293008>

and then parsing digits out, but I have not been able to figure out how (or even if it's possible) to manipulate text constants in MASM.

Any help would be greatly appreciated. Also, I'm doing this for fun, and to try to learn assembly and MASM better, so I'd rather not just use a library that does this for me (takes all of the fun out of it).


Solution

  • The integer constant doesn't fit in 64 bits, which is the biggest integer any practical x86/64 assembler can possibly support these days.

    Specifically for MASM, look up the official documentation, read this page. It lists the data declaration types supported by MASM. Among them the biggest integer types are:

    QWORD = 8 bytes (or 64 bits) and
    TWORD = 10 bytes (or 80 bits).

    Your integer constant needs log2(19292109310024103209481293008) ≈ 94 bits or 12 bytes. That just won't fit into a TWORD.

    Now, you can gain access to the individual characters of the string that comes as a parameter to the macro. Use FORC/IRPC for that.

    This would translate a string parameter into declaration of bytes, each representing the ASCII code of the respective character:

    FORC value, <012345>
    DB   '0' + value
    ENDM
    

    which is equivalent to

    DB '0'
    DB '1'
    DB '2'
    DB '3'
    DB '4'
    DB '5'
    

    But that alone isn't going to help much.

    You might be able to do something useful with macro functions and string-manipulating macros (CATSTR, INSTR, SUBSTR). Some loops or recursion would be necessary as well to transform a string, representing a decimal integer into a sequence of bytes with the binary representation of that same integer. At the moment this doesn't seem easy and I'm not entirely sure it's possible.