Hi, I have a problem with a function that gets a x and an y position and converts it to a "Pos2D Structure"

Here is the "Pos2D Structure":

***

;Note that this structure compiles completly well

Pos2D STRUCT
XPos DWORD ?
YPos DWORD ?
Pos2D ENDS

***

Here is the function:

***

MakePos2D proc X:DWORD, Y:DWORD
ASSUME eax:PTR Pos2D

mov .XPos, X
mov .YPos, Y

ret ; return the value in eax

ASSUME eax:nothing
MakePos2D endp

***


What's the problem? :confused:
Why is the problem? :confused:
How can I fix the problem? :confused:

Thank you very much in advance.

Julian.
Posted on 2003-09-10 09:08:01 by JulianS
Possibly you should initialize pointer eax to something. Hard to say more with your "error description".
Posted on 2003-09-10 09:17:16 by japheth
Well, if you are going to have eax assumed as something you first have to put the address of the structure in memory in eax. Also you must unassume the register before you return. And lastly you cannot move memory to memory (BTW that is simply a POINT structure no need to define a new one):
.data?

p2d POS2D <?>
.code
MakePos2D proc X:DWORD, Y:DWORD
[b]mov eax,OFFSET p2d[/b]
ASSUME eax:PTR Pos2D
mov edx,X
mov [eax].XPos, edx
mov edx,Y
mov [eax].YPos, edx

[b]ASSUME eax:nothing
ret ; return the value in eax[/b]
MakePos2D endp
A better way than using assume, that leads to less errors is the following,it is an alternate syntax that works the same:
.data?

p2d POS2D <?>
.code
MakePos2D proc X:DWORD, Y:DWORD
[b]mov eax,OFFSET p2d[/b]
mov edx,X
mov [eax].POS2D.XPos, edx
mov edx,Y
mov [eax].POS2D.YPos, edx

ret ; return the value in eax
MakePos2D endp
Posted on 2003-09-10 09:44:01 by donkey
What about



MakePos2D proc pStruct:DWORD,X:DWORD, Y:DWORD
mov eax, pStruct
ASSUME eax:PTR Pos2D

;mov [eax].XPos, X ;should not be able to compile
;mov [eax].YPos, Y ; cause it is mov eax+offset, [memory]

mov ecx, x
mov [eax].XPos, ecx
mov ecx, y
mov [eax].YPos, ecx

ret ; return the value in eax

ASSUME eax:nothing
MakePos2D endp
Posted on 2003-09-10 09:45:54 by roticv
Hi, thanks alot for the help.

I have a new function that finds out if a 2D Position is within a triangle or not.
You give the 3 "2D Positions" for the triangle and another 2D Point and then this function returns 1 if it is within.
But there are errors.
Alot.
The most math instructions within [ ] are errors. That is cause I don't know how to make well complicated math operations. Anyway don't worry about the math within the [ ], cause the math is good but the code ain't.

Here it is:

***

IsInTriangle proc PointX:DWORD, PointY:DWORD, TAX:DWORD, TAY:DWORD, TBX:DWORD, TBY:DWORD, TCX:DWORD, TCY:DWORD
mov eax, 0
LOCAL lbc :DWORD
LOCAL lca :DWORD
LOCAL lab :DWORD
LOCAL lap :DWORD
LOCAL lbp :DWORD
LOCAL lcp :DWORD
LOCAL abc :DWORD

mov lbc,
mov lca,
mov lab,
mov lap,
mov lbp,

mov lcp,

add abc, lbc
add abc, lca
add abc, lab

cmp abc,0
push abc
pop abc
shl abc,24
sar abc,30
xor abc,1
.If > 0
.If > 0
.If > 0
mov eax, 1
.EndIf
.EndIf
.EndIf

ret

IsInTriangle endp

***

Please solve the errors and give me a math tutorial or something so I know how to do math opertations.

Thanks alot.

If you are asking what these functions are to, then I can tell you that they are for a 3D Engine, and I have all the codes in "Visual Basic"(VB) made by myself, even texturing. It all works, but to slow. It takes about 1 second to take care of a triangle(Thats what my system works on.) and if you want texturing it may take up to 4 seconds for each triangle. That's where Asm comes into the picture. I am sure I could make it take care of a triangle in well 0.1 seconds or less, if I code it in ASM.

Julian.
Posted on 2003-09-10 12:15:54 by JulianS
Can give me the formula, I am abit lazy to look at your code and derive it out. Just a point to note,

mov lbc,

will definitely give a error firstly because you are multiplying 2 memory and subtracting them. You need to do the individual add and imul/mul and sub.

Just a question, do you mind me coming up with mmx codes, seems like I can produce one for this.
Posted on 2003-09-10 12:42:52 by roticv
Please solve the errors and give me a math tutorial or something so I know how to do math opertations.

Why don't you read a bit of the MASM.HLP file and solve them yourself, they are very simple basic concepts and are all covered there. You will not learn anything if we do your work for you, and this is not rent-a-coder, if you have a specific question we will answer it, but "solve the errors" sounds more like you want us to write the program for you.
Posted on 2003-09-10 12:52:37 by donkey
Here, no math:
PtInTriangle proc x1,y1,x2,y2,x3,y3,TestX,TestY

LOCAL ptarray[3] :POINT
LOCAL TestPt :POINT
LOCAL hRgn :DWORD

mov eax,x1
mov ptarray[0].x,eax
mov eax,y1
mov ptarray[0].y,eax

mov eax,x2
mov ptarray[8].x,eax
mov eax,y2
mov ptarray[8].y,eax

mov eax,x3
mov ptarray[16].x,eax
mov eax,y3
mov ptarray[16].y,eax

invoke CreatePolygonRgn,ADDR ptarray,3,WINDING
mov hRgn,eax

invoke PtInRegion,hRgn,TestX,TestY
push eax
invoke DeleteObject,hRgn
pop eax
ret

PtInTriangle endp
Posted on 2003-09-10 13:11:28 by donkey
Geesh this looks like HLA to me ;)


mov lbc,




finit ;init fp
; mov lbc, [TBX * TCY - TBY * TCX]
fld TBX ;load
fmul TCY ;TBX * TCY
fld TBY ;load TBY
fmul TCX ;TBY*TCX
fsub ;(TBX*TCY)-(TBY*TCX)
fstp lbc ;drop in LBC


This whole proc can be rewritten using that little snippet.... I actually started doing it.. but I thought you could need the training :p and that doesn't come from copy pasting ;)
Posted on 2003-09-10 15:03:05 by JimmyClif
Here's an even faster version using the stack instead of moving the data, roticv should like this ;)
PtInTriangle proc x1,y1,x2,y2,x3,y3,TestX,TestY

LOCAL hRgn :DWORD

mov eax,esp ; mov the stack pointer to eax
add eax,12 ; mov the pointer to the start of the array

invoke CreatePolygonRgn,eax,3,ALTERNATE
mov hRgn,eax

invoke PtInRegion,hRgn,TestX,TestY
push eax
invoke DeleteObject,hRgn
pop eax
ret

PtInTriangle endp
Posted on 2003-09-10 15:23:21 by donkey

Hi, I have a problem with a function that gets a x and an y position and converts it to a "Pos2D Structure"

Hmm... may be it's better to use a macro like


MakePos2D macro Addr_of_p2d, X, Y
mov eax, Addr_of_p2d
mov [eax].Pos2D.XPos, X
mov [eax].Pos2D.YPos, Y
endm
or more complex if you like save a few bytes sometimes


MakePos2D macro Addr_of_p2d, X, Y
mov eax, Addr_of_p2d
if X>0FFh
mov [eax].Pos2D.XPos, X
else
push X
pop [eax].Pos2D.XPos
endif
if Y>0FFh
mov [eax].Pos2D.YPos, Y
else
push Y
pop [eax].Pos2D.YPos
endif
endm
Posted on 2003-09-10 21:35:07 by S.T.A.S.
I have tried to change the math etc......
Here is my function:

***

MakePos2D proc X:DWORD, Y:DWORD

mov eax,OFFSET p2d
mov edx,X
mov .Pos2D.XPos, edx
mov edx,Y
mov .Pos2D.YPos, edx

MakePos2D endp

IsInTriangle proc PointX:DWORD, PointY:DWORD, TAX:DWORD, TAY:DWORD, TBX:DWORD, TBY:DWORD, TCX:DWORD, TCY:DWORD
LOCAL lbc :DWORD
LOCAL lca :DWORD
LOCAL lab :DWORD
LOCAL lap :DWORD
LOCAL lbp :DWORD
LOCAL lcp :DWORD
LOCAL abc :DWORD
LOCAL Temp :DWORD
LOCAL TM1 :DWORD
LOCAL TM2 :DWORD
LOCAL TM3 :DWORD
LOCAL TM4 :DWORD

mov Temp, TBX
mul Temp, PointY
mov lbp, TBY
mul lbp, PointX
sub lbp, Temp


mov Temp, TCX
mul Temp, PointY
mov lcp, TCY
mul lcp, PointX

sub lcp, Temp
add abc, lbc
add abc, lca
add abc, lab



mov TM1, 0
mov TM2, 0
mov TM3, 0
mov TM4, 0

mov TM1, abc
mov TM4, lbc
sub TM4, lbp
add TM4, lcp
mul TM1, TM4

mov TM2, abc
mov TM4, lca
sub TM4, lcp
add TM4, lap
mul TM2, TM4

mov TM3, abc
mov TM4, lab
sub TM4, lap
add TM4, lbp
mul TM3, TM4

cmp abc,0
push abc
pop abc
shl abc,24
sar abc,30
xor abc,1

.If TM1 > 0
.If TM2 > 0
.If TM3 > 0
mov eax, 1
.EndIf
.EndIf
.EndIf

ret

IsInTriangle endp

***


But it just won't work, I don't understand why.
Every time I try to assemble it says to almost all the lines that includes code:

"Invalid instruction oprands"


Why whats wrong.

Julian
Posted on 2003-09-11 03:58:24 by JulianS
mul lcp, PointX

becomes


mul [ebp-xx], [ebp+xx]


Of course you get invalid opcodes. Don't you remember that the cpu does not allow you to have both destination and source as memory? It is not in the instrcution set.
Posted on 2003-09-11 04:07:59 by roticv
roticv, isen't there any tutorials for well doing math operations?
If so, then please give me the link.
Cause that's the only way I am gonna make it.

Julian
Posted on 2003-09-11 05:42:54 by JulianS
The mul instruction only takes 1 operand, and the destination is always AX, DX:AX or EDX:EAX.
MOV memory,0 is dumb - use AND memory,0 instead since it takes less bytes.
CMP followed by SHL is useless since the cmp result is not used.
Also the ".if TM1>0" etc can be replaced with:


mov edx,TM1
or edx,TM2
or edx,TM3
js negative
xor eax,eax
inc eax
negative:

You really ought to read the processor manual :P

Donkey: Usage of GDI calls is slow! It involves translation of parameters and sometimes setting up an appropriate stack selector for 16-bit mode. Creation and deletion of GDI objects is also quite slow. And you can't escape the math :P You're just letting the system handle it.
Posted on 2003-09-11 14:42:02 by Sephiroth3
Donkey: Usage of GDI calls is slow! It involves translation of parameters and sometimes setting up an appropriate stack selector for 16-bit mode. Creation and deletion of GDI objects is also quite slow. And you can't escape the math :P You're just letting the system handle it.

Yes, I know, it was an example of how to do it with out math opcodes.
Posted on 2003-09-11 14:46:52 by donkey