Archived

This topic is now archived and is closed to further replies.

Help with assembler

This topic is 4947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well i need help I am begining to learn assembler (buth i don't think this is for the beginers forum) and I am focusing on the SIMD. so this function is writen in NASM segment .text global _Math_AMD_3DNOW_Vector3_ADD _Math_AMD_3DNOW_Vector3_ADD: femms lea eax, [ebp+8] lea ebx, [ebp] movd mm0,[eax] movd mm1,[ebx] pfadd mm0,mm1 movq mm1,[eax+8] movq mm2,[ebx+8] pfadd mm1,mm2 movq [eax] ,mm0 movd [eax+8] ,mm1 femms ret and its linked in the c troug .obj file format In c I call the "_Math_AMD_3DNOW_Vector3_ADD" and give it an pointer of 2 x 3D vector (X,Y,Z) The result is (at least shoud be) returnd in to first parameter (trough pointer). The code compiles ok , i dont get any errors but i don't get any results back !! I look at the vectors that hawe pased this function and they remain the same. Do you know why??? P.S. Sory about gramatical errors [edited by - Red Drake on May 31, 2004 1:49:12 PM] [edited by - Red Drake on May 31, 2004 2:06:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Red Drake
Is there some one that can code SIMD - 3DNOW , and can help me ???

It''s not even been 2 hours since your original post, and it seems to be a pretty specific topic. A response will come if someone knows the answer.

Share this post


Link to post
Share on other sites
Ok, ok
It''s yust that I hawe been trying to make this work for like ?? all day and its about 22:00 hear (I started at 12:00) so I am quite crazy right now (try this and try that,and again , and again, go to google search ...).
And I get a fealing that if nobody responds to your question in 2 h or so you probably wont even get an anwser
+ I hawe 56k dialup modem so my bill is pumping while I am waiting
I am so angry at my computer - compiler that I coud yust smash it - hopfuly it wont come to that - so if anybody knows an anwser to the question you bether write in or my computer may suffer som critical hits (on the monitor, keyboard ....)
I thing that game development is one of the most stresfull things to do :

Share this post


Link to post
Share on other sites
In vecadd.h write:


#ifndef VEC3DNOW
#define VEC3DNOW

struct Vec3D
{
float x;
float y;
float z;
};

extern "C" void Math_AMD_3DNOW_Vector3_ADD(Vec3D* a, Vec3D* b);

#endif



In vecadd.asm write:


segment .text

global _Math_AMD_3DNOW_Vector3_ADD

;
; void _Math_AMD_3DNOW_Vector3_ADD(Vec3D* a, Vec3D* b);
;
_Math_AMD_3DNOW_Vector3_ADD:

; create stack frame
push ebp
mov ebp, esp

; load pointers, eax = a, edx = b
mov eax, [ebp + 8]
mov edx, [ebp + 12]

; add x and y
movq mm0, [eax]
pfadd mm0, [edx]
movq [eax], mm0

; add z
movd mm0, [eax + 8]
pfadd mm0, [edx + 8]
movd [eax + 8], mm0

; make floating point save
femms

; destroy stack frame
pop ebp

; return
ret




[edited by - nmi on May 31, 2004 4:11:34 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Red Drake
I thing that game development is one of the most stresfull things to do :

It wouldn''t be special if it weren''t. And you wouldn''t want it any other way anyway, right?

Share this post


Link to post
Share on other sites
A question to nmi:
isn't pfadd used to add two mmx registers ??
And why do you create a stack ? (I mean I dont use it in this function).
I am going to test your code now :|
Thanks

and to Woodsman :
Whel any more stress to me today and I woud hawe an mayor brakedown

[edited by - Red Drake on May 31, 2004 4:20:18 PM]

Share this post


Link to post
Share on other sites
As the manual says:

PFADD mmreg1, mmreg2/mem64 0Fh 0Fh / 9Eh Packed, floating-point addition

So you could specify another register or a memory location.

The stack frame is created to access the pointers, a and b. If you like you may change the code to the following:


_Math_AMD_3DNOW_Vector3_ADD:

; get params
pop ecx
pop eax
pop edx

; add x and y
movq mm0, [eax]
pfadd mm0, [edx]
movq [eax], mm0

; add z
movd mm0, [eax + 8]
pfadd mm0, [edx + 8]
movd [eax + 8], mm0

; make floating point save
femms

; prepare return
push edx
push eax
push ecx

; return
ret


[edited by - nmi on May 31, 2004 4:31:40 PM]

Share this post


Link to post
Share on other sites
Your first code does not work !!

the debug from Visul c++ :

00401340 push ebp
00401341 mov ebp,esp
00401343 mov eax,dword ptr [ebp+8]
00401346 mov edx,dword ptr [ebp+0Ch]
-> 00401349 movq mm0,mmword ptr [eax] // Error
0040134C pfadd mm0,mmword ptr [edx]
00401350 movq mmword ptr [eax],mm0
00401353 movd mm0,dword ptr [eax+8]
00401357 pfadd mm0,mmword ptr [edx+8]
0040135C movd dword ptr [eax+8],mm0
00401360 femms

P.S. Do you know does creating a stack takes more thime (significantly) ??

Share this post


Link to post
Share on other sites
I think you are using NASM ? At least NASM assembles the file without any error. Maybe you should try that. Then insert the object file into your Visual C++ source tree.

The thing about stack frames is that every function saves the old base pointer and then loads the base pointer with the value for its own stack frame. The base pointer may be used to access parameters or local variables. When calling other functions from your assembler code, you should use stack frames. Here we don''t need it, but we may use it. Compared to the 3Dnow! addition it comes almost for free (consider caches etc.)

Share this post


Link to post
Share on other sites
I am using NSAM and GCC (dev-cpp)
The thing you saw was an debug of my app after it reprted an illegal operation ! It hapend to me a couple of times before !!
As I said : I am so angry at my computer - compiler that I coud yust smash it.
P.S.
I newer tryed code like yours buth it hapend in similar situations

[edited by - Red Drake on May 31, 2004 4:59:53 PM]

Share this post


Link to post
Share on other sites
It''s not the complier!
The app compiles corectly and I run it (on windows) and then get an mesage
"Ilegal operation in .... bla bla bla"
And after i press the debug buton I get the code

00401340 push ebp
00401341 mov ebp,esp
00401343 mov eax,dword ptr [ebp+8]
00401346 mov edx,dword ptr [ebp+0Ch]
-> 00401349 movq mm0,mmword ptr [eax] // Error
0040134C pfadd mm0,mmword ptr [edx]
00401350 movq mmword ptr [eax],mm0
00401353 movd mm0,dword ptr [eax+8]
00401357 pfadd mm0,mmword ptr [edx+8]
0040135C movd dword ptr [eax+8],mm0
00401360 femms

in my Visual Studio debuger
So any known reasons for that
P.S.
I hawe tried the second code buth it does not work eather

Share this post


Link to post
Share on other sites
Can you post the code where you call the function ?

Try this example:


#include "vecadd.h"

#include <iostream>
using namespace std;

int main()
{
Vec3D a = { 1.0, 2.0, 3.0 };
Vec3D b = { 4.0, 6.0, 8.0 };

Math_AMD_3DNOW_Vector3_ADD(&a, &b);

cout << "result: ("
cout << a.x << ", " << a.y << ", " << a.z;
cout << ")";
cout << endl;

return 0;
}




[edited by - nmi on May 31, 2004 5:21:17 PM]

Share this post


Link to post
Share on other sites
Hear it is in c

#include <stdio.h>
#include <stdlib.h>

struct Vec3
{
float X,Y,Z;
};

extern "C" {void Math_AMD_3DNOW_Vector3_ADD(Vec3 V1, Vec3 V2);}



int main (void)
{
Vec3 V,U;
V.X = 3.0f; V.Y = 0.5f; V.Z = 5.3f;
U.X = 2.30f; U.Y = 3.0f; U.Z = 0.2f;

Math_AMD_3DNOW_Vector3_ADD(V,V);
printf ("\n %f %f %f ",V.X , V.Y ,V.Z );

return 0;
}


and in asm


0x40128c : push ebp
0x40128d : mov ebp,esp
0x40128f : sub esp,0x38
0x401292 : and esp,0xfffffff0
0x401295 : mov eax,0x0
0x40129a : mov DWORD PTR [ebp-44],eax
0x40129d : mov eax,DWORD PTR [ebp-44]
0x4012a0 : call 0x4036f0
0x4012a5 : call 0x4020d0
0x4012aa : mov DWORD PTR [ebp-24],0x40400000
0x4012b1 : mov DWORD PTR [ebp-20],0x3f000000
0x4012b8 : mov DWORD PTR [ebp-16],0x40a9999a
0x4012bf : mov DWORD PTR [ebp-40],0x40133333
0x4012c6 : mov DWORD PTR [ebp-36],0x40400000
0x4012cd : mov DWORD PTR [ebp-32],0x3e4ccccd
0x4012d4 : sub esp,0x20
0x4012d7 : mov edx,esp
0x4012d9 : mov eax,DWORD PTR [ebp-24]
0x4012dc : mov DWORD PTR [edx+12],eax
0x4012df : mov eax,DWORD PTR [ebp-20]
0x4012e2 : mov DWORD PTR [edx+16],eax
0x4012e5 : mov eax,DWORD PTR [ebp-16]
0x4012e8 : mov DWORD PTR [edx+20],eax
0x4012eb : mov eax,DWORD PTR [ebp-24]
0x4012ee : mov DWORD PTR [edx],eax
0x4012f0 : mov eax,DWORD PTR [ebp-20]
0x4012f3 : mov DWORD PTR [edx+4],eax
0x4012f6 : mov eax,DWORD PTR [ebp-16]
0x4012f9 : mov DWORD PTR [edx+8],eax
0x4012fc : call 0x401340
0x401301 : add esp,0x20
0x401304 : sub esp,0x4
0x401307 : fld DWORD PTR [ebp-16]
0x40130a : lea esp,[esp*1-8]
0x40130e : fstp QWORD PTR [esp*1]
0x401311 : fld DWORD PTR [ebp-20]
0x401314 : lea esp,[esp*1-8]
0x401318 : fstp QWORD PTR [esp*1]
0x40131b : fld DWORD PTR [ebp-24]
0x40131e : lea esp,[esp*1-8]
0x401322 : fstp QWORD PTR [esp*1]
0x401325 : push 0x401280
0x40132a : call 0x405140
0x40132f : add esp,0x20
0x401332 : mov eax,0x0
0x401337 : leave
0x401338 : ret




[edited by - Red Drake on May 31, 2004 5:23:45 PM]

Share this post


Link to post
Share on other sites
The problem is here:

quote:

extern "C" {void Math_AMD_3DNOW_Vector3_ADD(Vec3 V1, Vec3 V2);}



You pass the parameters by value, so you just put a copy onto the stack when calling the function. Whether the function changes the values or not, the copy won't affect the original.

Please use the other variant:


extern "C" void Math_AMD_3DNOW_Vector3_ADD(Vec3* a, Vec3* b);


By passing pointers to the data, the function is able to modify its contents. Now you should see how the values are changing.

[edited by - nmi on May 31, 2004 5:32:51 PM]

Share this post


Link to post
Share on other sites
You are right
I totaly fuc*d my self.
I did not think for a single moment that this is it
Thank''s man !
Now instead of "I am so angry at my computer - compiler that I coud yust smash it" it''s "I am so angry at my self that I coud start hiting my head of the wall.)
And the funiest part is that when I started to learn SIMD this morning up uuntil now I was doing hundreds of stuff that didn''t even mather !!!!!!!!!!!!!!!
As I said "I am goin to start hiting my head of the wall"
And sory for the time that I hawe taken you

Share this post


Link to post
Share on other sites
quote:

Your first code does not work !!



It does work for me, so either your processor is too old, or probably your data is not aligned properly


My Site

[edited by - Quasar3D on May 31, 2004 5:36:58 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Red Drake
You are right
I totaly fuc*d my self.
I did not think for a single moment that this is it
Thank''s man !
Now instead of "I am so angry at my computer - compiler that I coud yust smash it" it''s "I am so angry at my self that I coud start hiting my head of the wall.)
And the funiest part is that when I started to learn SIMD this morning up uuntil now I was doing hundreds of stuff that didn''t even mather !!!!!!!!!!!!!!!
As I said "I am goin to start hiting my head of the wall"
And sory for the time that I hawe taken you


No problem. Maybe it''s already somewhat late... ;-)

However learning SIMD is just as easy as taking the manual. Teaching SIMD to your compiler is somewhat harder...

Share this post


Link to post
Share on other sites