News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Overflow problem

Started by Joao Batista, November 27, 2012, 03:17:26 AM

Previous topic - Next topic

Joao Batista

Hello, i'm having issues checking overflow while adding.
We are making a really basic compiler, my code is:


Array b[2.];
b[1.]:=1.;
b[1.]:=b[1.]+1.2;


And this is what we are generating on masm32

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
include    \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib
include    \masm32\include\debug.inc
includelib \masm32\lib\debug.lib
.data
nombre db "Compilador",0
EDIV0 db "Error: Division por 0",0
EOVER db "Error: Overflow en la suma",0
cte0 dq 0
cte6 dq 1.2
cte5 dq 1.
cte4 dq 1.
cte3 dq 1.
cte2 dq 1.
cte1 dq 2.
_@7 dq 0
_@6 dq 0000h
_@5 dq 0
_@1 dq 0
_b dq 2 dup (0000h)

.code
start:

FLD _b
FADD _@1
FSTP _@1
FLD cte3
FSTP qword ptr[_@1]
FWAIT
FLD cte3
FLD cte3
FCOMP
fstsw ax
sahf
JBE ET10
JMP ABORT
ET10:
FLD cte1
FLD cte3
FCOMP
fstsw ax
sahf
JBE ET16
JMP ABORT
ET16:
FLD _b
FADD _@5
FSTP _@5
FLD qword ptr [_@5]
FWAIT
FSTP _@5
FLD _@5
FLD cte6
FADD
FSTP _@6
JC OVER

FLD _b
FADD _@7
FSTP _@7
FLD _@6
FSTP qword ptr[_@7]
FWAIT
invoke ExitProcess,0
DIV0:
invoke MessageBox, NULL, addr EDIV0, addr nombre, MB_OK
JMP ABORT
OVER:
invoke MessageBox, NULL, addr EOVER, addr nombre, MB_OK
JMP ABORT
ABORT:
invoke ExitProcess,1
end start


My questions are:
1. Is correct to use
fstsw ax
sahf
while checking for flags on the coprocessor? or is there another way to do this?
2. Why does this code generate an add overflow while it shouldn't ?

Thanks in advance.

dedndave

you should define those as REAL8, not QWORD

1.7976931348623158^308 is the max  :P

fstsw ax
sahf
is ok, i guess - but, you will get an exception when it overflows (next century), not a math flag

jj2007

FLD _@5
FLD cte6
FADD   ; <<<<<< FPU instruction does not set CPU flags
FSTP _@6
JC OVER

qWord

Quote from: dedndave on November 27, 2012, 04:27:30 AM
you should define those as REAL8, not QWORD
that syntax is strange, but OK as long as using a dot in the declaration.
Quote from: Joao Batista on November 27, 2012, 03:17:26 AM
My questions are:
fstsw ax
sahf
is there another way to do this?
this method is obsolete - use FCOMI[P]/FUCOMI[P] instead.
MREAL macros - when you need floating point arithmetic while assembling!

Joao Batista

mm ok.. so..

FLD _@5
FLD cte6
FADD
FSTP _@6
JC OVER


here the FADD doesn't set the cpu flags.. how do i do that then?

and.. how do i use FCOM? FCOMI.. etc?

i tried something like

FLD _@5
FLD cte6
FCOM
JC OVER
FADD
FSTP _@6


but this doesnt work..
yeah.. im kinda lost here hehe

qWord

MREAL macros - when you need floating point arithmetic while assembling!

dedndave

Ray's FPU tutorial is probably a little easier to understand...

http://www.ray.masmcode.com/fpu.html

Joao Batista

ok ill start reading, ty, but just one problem.
I keep geting "Instruction or register not accepted in current CPU mode" error
how do i set the cpu mode to work with that?

Gunther

Hi Joao,

Quote from: Joao Batista on November 27, 2012, 05:28:20 AM
ok ill start reading, ty, but just one problem.
I keep geting "Instruction or register not accepted in current CPU mode" error
how do i set the cpu mode to work with that?

did you try .387 after .386?

Gunther
You have to know the facts before you can distort them.

jj2007

The fcomip needs probably .586 or .686 - just try...

dedndave

i think .686 is required   :t

Joao Batista

Ok thank you all for the help.. i have to wait until tomorrow to try all this so.. see you tomorrow!
Thanks again!

MichaelW

One possibility is to store the status word and test the exception status bits.

;==============================================================================
include \masm32\include\masm32rt.inc
;.686
;==============================================================================

;--------------------------------------------------
; Equates for the FPU status word exception flags.
;--------------------------------------------------

FEX_INVALID      equ 1
FEX_DENORMALIZED equ 2
FEX_ZERODIVIDE   equ 4
FEX_OVERFLOW     equ 8
FEX_UNDERFLOW    equ 16
FEX_PRECISION    equ 32
FEX_STACKFAULT   equ 64
FEX_ANY          equ 127

;==============================================================================
.data
    r10max      REAL10  1.18E4932  ; approximate
    r8          REAL8   0.0
    fpu_status  dd 0
.code
;==============================================================================
start:
;==============================================================================

    fstsw WORD PTR fpu_status
    test fpu_status, FEX_OVERFLOW
    jz @F
    printf("OF 0\n")
  @@:

    fld r10max
    fld r10max
    fadd

    fstsw WORD PTR fpu_status
    test fpu_status, FEX_OVERFLOW
    jz @F
    printf("OF 1\n")
  @@:

    fstp st
    fclex

    fld r10max
    fstp r8
    fstsw WORD PTR fpu_status
    test fpu_status, FEX_OVERFLOW
    jz @F
    printf("OF 2\n")
  @@:

    fclex

    fld1
    fstp r8
    fstsw WORD PTR fpu_status
    test fpu_status, FEX_OVERFLOW
    jz @F
    printf("OF 3\n")
  @@:

    fclex

    inkey
    exit

;==============================================================================
Well Microsoft, here's another nice mess you've gotten us into.

dedndave

using real8's, and starting at 1.0...
adding 1.2 with each pass...
i don't think you will live long enough to see it overflow   :biggrin:

refer to reply #1 - lol
http://masm32.com/board/index.php?topic=959.msg8657#msg8657

Joao Batista

Hi, yeah the thing with the dq is that i must use them.. i know it sounds silly, but you will have to tell that to my professor -.-
So is a restriction i have to live with.