News:

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

Atol with leading '+', overflow flag

Started by Sleepy_w, January 16, 2011, 12:20:53 PM

Previous topic - Next topic

Sleepy_w

Hello everyone,

First of all thanks for the great SDK with documentation and this forum :clap:

I am trying to do an application where user could input string with the simple equation and get a result, e.g.:
"+123*456", "-0056+85", "-056|34", "+4&389", "5/2", "-0-2"
Also input should be verified on-the-fly, so user should not be able to enter "---3", "450380493403" (overflow) and so on.

I have found a way to get incoming characters where based on several logic flags i will verify them, block or echo them to the console and use later.
Now i have stumbled upon several things which i don't quite understand:
1. Using atol on the "+1234" string results in 0 (well i was using sval but it just call atol so there should not be difference). Though works fine with the "-1234" input.
According to documentation (MASM32 Reference Library):
QuoteThe algorithm will accept the two sign specifiers "+" and "-" and will treat a numeric string with no sign specifier as a positive integer.
2. How can i spot if overflow will occur as a result of arithmetic operation? For example i am trying to add 3000000000 to 3000000000, both are fine dword numbers yet the result will cause overflow. In such case i'd like to print some kind of error message to user.

I have attached a sample i am currently playing with though i doubt it will help anyone :)

Thank you.

Best regards,
Sleepy_w

Sleepy_w


hutch--

Hi Sleepy,

Welcome on board. If you want to play with big numbers, do it in 80 bit FP instructions. You can look up Ray Filiatreault''s very good floating point tutorials. Look in the forum links at the top right of the page.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave


Sleepy_w

Thank you for your replies! :U

QuoteWelcome on board. If you want to play with big numbers, do it in 80 bit FP instructions. You can look up Ray Filiatreault''s very good floating point tutorials. Look in the forum links at the top right of the page.
No, in this case i just simply want to catch the moment of "we co still add/multiply and so on" or "it would be incorrect". Though of course saved a bookmark for future reference :)

Quotewelcome to the forum
you might have a look at this thread...
http://www.masm32.com/board/index.php?topic=15602.0
It's almost the same i am trying to do but with a great overkill (more than one operation and their order is applied). Code is a bit hard for me to follow right now but i saw how overflow is checked there: through a JO flag.
I've tried to create small sample to check it and it behaves not the way i am expecting. Can someone please point in the right direction so i could investigate this matter?
Sample code:
;--------------------------------------------------------------------------
include \masm32\include\masm32rt.inc
.data
;--------------------------------------------------------------------------
.code

start: 
call main                   ; branch to the "main" procedure
exit
main proc
mov eax, 1234567890
mov ebx, 3234567890
add ebx, eax
jo overflow_error ;jumping here if overflow is detected
print str$(ebx), 13,10 ;continue here is everything was ok
ret
overflow_error:
print chr$("Overflow error occured."),13,10
ret
main endp    
end start                       ; Tell MASM where the program ends

Test cases:
1. 5+6 - ok
2. 1234567890+1234567890=overflow (i was expecting 2469135780)
3. 1234567890+3234567890=174168484 ( :dazzled: , i was expecting overflow)
4. -1234567890+3234567890=overflow (why not -2000000000?)

Thanks again.

Best regards,

dedndave

well - you have to understand "two's compliment"
the value 3234567890 is causing you trouble
in hexidecimal, that is 0C0CB96D2h
you can quickly see that bit 31 is set
it is being treated as a negative value - thus, no overflow
if you want to treat it as an unsigned value, use JC

if you want to handle larger values...
;add EDX:EAX + ECX:EBX

        mov     eax,1234567890
        mov     edx,0
        mov     ebx,3234567890
        mov     ecx,0
        add     eax,ebx
        adc     edx,ecx
        or      edx,edx
        jnz     overflow