News:

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

Main Menu

memory error

Started by dusty, May 27, 2013, 01:05:00 PM

Previous topic - Next topic

dusty

using visual studio 2010, the code runs fine in debug mode, but I get a corrupt memory error in release mode ?
; double HCAR( HCURVE*, double ang, double radius) c/c++ calling function

include C:\C_Projects\winasm64\winasm64\source\masm\MasmMath\mConst.inc

HCURVE STRUCT
Ang REAL8 ?
Rad REAL8 ?
degA REAL8 ?
degC REAL8 ?
Len REAL8 ?
Tan REAL8 ?
Chd REAL8 ?
M REAL8 ?
E REAL8 ?
Seg REAL8 ?
Sect REAL8 ?
HCURVE ENDS

.DATA

pi2 PI2_8
raddegA REAL8 5729.57795130823208     ;//76798154814105
raddegC REAL8 5729.65067400651548     ;//65188680062561


.CODE

align 8
HCAR PROC PUBLIC

LOCAL   two :REAL8
LOCAL ang_2 :REAL8 ; 1/2 angle
LOCAL rad :REAL8 ; save copy of radius for fpu use

sub rsp, 24 ; save room for locals

finit
fld1
fadd st, st
fstp two

fldz
fstp REAL8 PTR[R8]
comisd xmm1, REAL8 PTR[R8]
je angzero
fldpi
fstp REAL8 PTR[R8]
comisd xmm1, REAL8 PTR[R8]
je ang180
jmp contin

angzero:
fldz
fst [rcx].HCURVE.Len
fst [rcx].HCURVE.Tan
fst [rcx].HCURVE.Chd
fst [rcx].HCURVE.E
fst [rcx].HCURVE.M
fst [rcx].HCURVE.degA ; degree curvature arc
fst [rcx].HCURVE.degC ; degree curvature chord
fst [rcx].HCURVE.Seg
fstp [rcx].HCURVE.Sect
movsd [rcx].HCURVE.Ang, xmm1
movsd [rcx].HCURVE.Rad, xmm2
jmp alldone

ang180:
movsd [rcx].HCURVE.degA, xmm3 ; degree curvature arc
movsd [rcx].HCURVE.degC, xmm4 ; degree curvature chord
movsd rad, xmm2
fld rad
fst [rcx].HCURVE.M
fld st
fmul two
fldpi
fmul
fstP [rcx].HCURVE.Len
fmul two
fstp [rcx].HCURVE.Chd
fldz
fst [rcx].HCURVE.Tan
fstp [rcx].HCURVE.E
fld rad
fmul st, st ; rad^2
fldpi
fmul ; pi * rad^2
fdiv two
fst [rcx].HCURVE.Seg
fstp [rcx].HCURVE.Sect
movsd [rcx].HCURVE.Ang, xmm1
movsd [rcx].HCURVE.Rad, xmm2
movsd xmm3, raddegA
movsd xmm4, raddegC
divsd xmm3, xmm2
divsd xmm4, xmm2
movsd [rcx].HCURVE.degA, xmm3 ; degree curvature arc
movsd [rcx].HCURVE.degC, xmm4 ; degree curvature chord

jmp alldone

contin:
movsd ang_2, xmm1
movsd rad, xmm2

fld ang_2
fld two
fdiv
fstp ang_2 ; save 1/2 angle
movsd [rcx].HCURVE.Ang, xmm1
movsd [rcx].HCURVE.Rad, xmm2

calcLen:
movsd xmm3, xmm1
mulsd xmm3, xmm2
movsd [rcx].HCURVE.Len, xmm3 ; length

caldDegC:
movsd xmm3, raddegA
movsd xmm4, raddegC
divsd xmm3, xmm2
divsd xmm4, xmm2
movsd [rcx].HCURVE.degA, xmm3 ; degree curvature arc
movsd [rcx].HCURVE.degC, xmm4 ; degree curvature chord

calcTan:
fld ang_2
fptan
fstp st ; remove 1.0 left on stack
fld rad
fmul
fabs
fst REAL8 PTR[rcx].HCURVE.Tan ; tangent

calcE:
fmul st, st ; tangent^2
fld rad
fmul st, st ; rad^2
fadd ; tan^2 + rad^2
fsqrt
fld rad
fsub
fabs
fstp REAL8 PTR[rcx].HCURVE.E ; save E external

calcChd:
fld ang_2
fsin
fld rad
fld two
fmul
fmul
fabs
fst REAL8 PTR[rcx].HCURVE.Chd ;save chord

calcM:
fld two
fdiv ; 1/2 chord
fmul st, st ; (1/2 chord)^2
fld rad
fmul st, st ; rad^2
fxch
fsub
fsqrt
fld rad
fxch
fsub
;fabs
fstp REAL8 PTR[rcx].HCURVE.M ;save M mid ordinate

; calculate area segment and sector
SegArea:
fld rad
fmul st, st ; rad^2
fldpi
fmul ; pi * rad^2
fld  REAL8 PTR[rcx].HCURVE.Ang
fld pi2
fdiv
fmul
fst REAL8 PTR[rcx].HCURVE.Seg ; save segment area leave copy on stack

fldpi
fstp REAL8 PTR[R8]
comisd xmm1,REAL8 PTR[R8]
jb SectArea
ja AdjustDelta
jmp alldone

AdjustDelta:

fld pi2
fld REAL8 PTR[rcx].HCURVE.Ang
fsub ; 360-ang
fld st ; copy to stack
fdiv pi2
fld REAL8 PTR[rcx].HCURVE.Rad
fmul st, st ; r^2
;fmul
fldpi
fmul
fmul
fxch
fdiv two
fstp ang_2 ; ang/2.0 "half delta"


;movsd ang_2, xmm2

SectArea:
fld ang_2
fcos
fld rad
fmul
fld REAL8 PTR[rcx].HCURVE.Chd
fld two
fdiv
fmul
fsub
fstp REAL8 PTR[rcx].HCURVE.Sect;


alldone:

add rsp, 24
ret
HCAR endp
end

dedndave

when you use the LOCAL directive, no need to sub/add rsp,24

i am not a 64-bit guy, but i am guessing you have a stack alignment issue
i think windows expects the stack to be 16-aligned for 64-bit code
another possibility is some SSE instructions also require 16-aligned data

qWord

R8 is used as pointer, but never initialized. Also, you maybe want to reduce the FPU usage to the needed minimum.

Quote from: dedndave on May 27, 2013, 01:17:07 PMi am not a 64-bit guy, but i am guessing you have a stack alignment issue
i think windows expects the stack to be 16-aligned for 64-bit code
another possibility is some SSE instructions also require 16-aligned data
because the function does not call any API (direct or indirect), there is no need to align the stack. The scalar SSEx code also does not require alignment.

BTW: I can't see where see return value (XMM0) is set.
MREAL macros - when you need floating point arithmetic while assembling!

dusty

qWord

; double HCAR( HCURVE*, double ang, double radius) c/c++ calling function
that should be ; void HCAR( HCURVE*, double ang, double radius) c/c++ calling function
it doesn't return a double, my boo boo.
I have tried to keep the fpu stack clean, most everything is poped off using fstp as I go along.
??

japheth

#4
Since you have local variables, your procedure isn't a leaf function; for a frame function, AFAIK, the correct prologue should looks like this:


align 8
HCAR PROC PUBLIC frame

LOCAL   two :REAL8
LOCAL ang_2 :REAL8 ; 1/2 angle
LOCAL rad :REAL8 ; save copy of radius for fpu use

sub rsp, 24 ; save room for locals
.allocstack 24
.endprolog

finit
fld1


Be aware that ml64 handles local variables differently for leaf and frame procedures. For the latter case, there's no stack reserved automatically, but for the first case there is! You'll see this in the listing if you add option -Sg.

Edit: I forgot that, if the procedure has the frame attribute, ml64 will neither allocate space for local variables nor set up a stack base pointer ( i.e. RBP) - you'll have to save,init and restore RBP manually.

dusty

Japheth

thanks for your help. that had not been a problem before, but I did make the change you suggested and it didn't solve the problem. I think it's a pointer problem in c++, it's late and I will take a look at the code again after 8-10 hours of sleep. A few hours of sleep has solved previous problems  ::)

dusty

Turns out the problem was with my data grid not with the code. At least for now it seems to be working  :shock: