News:

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

Too large integer value

Started by Danesh, November 11, 2006, 09:15:15 PM

Previous topic - Next topic

Danesh

Hi all,

I use udw2str to convert an unsigned integer value to string. I would like to detect when the input value is larger than a maximum value which can feet in a double word. The problem is that when the input value is a very large value like 99999999999999999999, it is still converted by udw2str but the result is something which seems to be incorrect. Is there anyway to just convert values with proper size?

Regards,

D.


Tedd

It would require detecting overflow during conversion, but yes it's entirely possible :wink
(The only problem is what to return when this happens? -1 is the same as 4294967295 when unsigned, so it's a valid number.)
No snowflake in an avalanche feels responsible.

Ratch

Danesh,

    What does udw2str have to do with your problem?  Any DWORD value you give udw2str is a valid unsigned number, and will be converted.  You need to insure that 99999999999999999999 is not represented by a DWORD value.  That's a different problem not involving udw2str.  If you are receiving 99999999999999999999 from text or keyin, isn't it already in a string format?  If 99999999999999999999 comes from a calculation, then the CARRY flag should be checked during each step of the calculation when there is a possibility that the unsigned number will be greater than the capacity of a DWORD.  Ratch

Danesh

Sorry, I meant ustr2dw, You are right Ratch. But if I enter a long string of numbers, it will not show if overflow have happened.


dsouza123

The max dword value is 4294967295  maximum length 10 digits,
so check the length, 1-9 will always work and 10 sometimes.

Maybe use/write a str2qw routine
it could handle integers to 18446744073709551615  maximum length 20 digits.

I posted code before to convert from long decimal and hex strings
to large unsigned integers, multi dword values (about 20 dwords),
as part of some large integer routines.

Ratch

Danesh,

     Yes, you are right.  The code is defective in that it does not check for input overflow.  The following code will do what you want.  Ratch



;#########################################################################
; ustr2dw

; Parameters
;     pszString - null-terminated string to be converted

; Result
;     EAX = converted number

  .DATA
TEN DWORD 10

  .CODE

;#########################################################################
  ALIGN 4

ustr2d:
  PUSH ESI
  XOR EAX,EAX
  MOV ESI,[ESP+2*DWORD]

  .WHILE TRUE
    MOVZX ECX,BYTE PTR [ESI]
    SUB ECX,'0'
  .BREAK .IF SIGN?
    INC ESI
    MUL [TEN]
    JC ERROR
    ADD EAX,ECX
    JC ERROR
  .ENDW

  POP ESI
  RET DWORD

;#########################################################################

Danesh

Thanks Ratch and Dsouza123. Your solutions were excellent. My problem is fixed now.