The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Danesh on November 11, 2006, 09:15:15 PM

Title: Too large integer value
Post by: Danesh on November 11, 2006, 09:15:15 PM
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.

Title: Re: Too large integer value
Post by: Tedd on November 11, 2006, 10:08:12 PM
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.)
Title: Re: Too large integer value
Post by: Ratch on November 11, 2006, 10:44:33 PM
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
Title: Re: Too large integer value
Post by: Danesh on November 12, 2006, 01:02:05 PM
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.

Title: Re: Too large integer value
Post by: dsouza123 on November 12, 2006, 10:24:46 PM
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.
Title: Re: Too large integer value
Post by: Ratch on November 13, 2006, 03:35:39 AM
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

;#########################################################################
Title: Re: Too large integer value
Post by: Danesh on November 13, 2006, 03:17:36 PM
Thanks Ratch and Dsouza123. Your solutions were excellent. My problem is fixed now.