C files with inline-asm into bmax?

BlitzMax Forums/BlitzMax Programming/C files with inline-asm into bmax?

Is is possible to import C files that use inline assembly into bmax?

Here is an example of what I am trying to do, but gives the error: cpuspeed.c:12: parse error before "_emit"

//---- file: cpuspeed.c ----

#include <windows.h>

int CpuSpeed(void)
{
    unsigned __int64 start, stop;
    unsigned __int64 nCtr, nFreq, nCtrStop;

    QueryPerformanceFrequency((LARGE_INTEGER *)&nFreq);

    __asm _emit 0x0F
    __asm _emit 0x31
    __asm mov DWORD PTR start, eax
    __asm mov DWORD PTR [start+4], edx

    QueryPerformanceCounter((LARGE_INTEGER *)&nCtrStop);
    nCtrStop += nFreq;
    do
    {
        QueryPerformanceCounter((LARGE_INTEGER *)&nCtr);
    } while (nCtr < nCtrStop);
    
    __asm _emit 0x0F
    __asm _emit 0x31
    __asm mov DWORD PTR stop, eax
    __asm mov DWORD PTR [stop+4], edx

    return ((stop-start)/1000000);
}


'---- file: test.bmx ----

Import "cpuspeed.c"

Extern
    Function CpuSpeed()
End Extern

Print "Cpu Speed: "+CpuSpeed()+" Mhz"


Am I doing something wrong or should I put the C function in a dll instead? (this is not possible in bmax)

Stick it in a dll, oh I did it already : http://www.blitzbasic.com/toolbox/toolbox.php?tool=122

I think you need to use "AT&T syntax" assembly in inline code with mingw.

Okay I *think* this is the correct AT&T conversion (could be wrong) but still does not compile.

cpuspeed.c:12: parse error before ':' token

//---- file: cpuspeed.c ----

#include <windows.h>

int CpuSpeed(void)
{
    unsigned __int64 start, stop;
    unsigned __int64 nCtr, nFreq, nCtrStop;

    QueryPerformanceFrequency((LARGE_INTEGER *)&nFreq);
	
    __asm: _emit $0x0F
    __asm: _emit $0x31
    __asm: movl %eax,$DWORD PTR start
    __asm: mov $edx,$DWORD PTR [start+4]
	
    QueryPerformanceCounter((LARGE_INTEGER *)&nCtrStop);
    nCtrStop += nFreq;
    do
	{
        QueryPerformanceCounter((LARGE_INTEGER *)&nCtr);
    } while (nCtr < nCtrStop);
    
    __asm: _emit $0x0F
    __asm: _emit $0x31
    __asm: movl	%eax, $DWORD PTR stop
    __asm: mov $ed, $DWORD PTR [stop+4]

    return ((stop-start)/1000000);
}


I'm no assembly programmer, but see if this helps at all:

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#s4

Alternatively, you could possibly create a ".a" library in Fasm directly and import/declare that... not something I can advise any further on though!

Stuart somthing like this, all though i could be wrong. but this does compile. im unsure on '_emit' convertion though.


#include <windows.h>

unsigned __int64 start, stop;
unsigned __int64 nCtr, nFreq, nCtrStop;

int CpuSpeed(void)
{
    QueryPerformanceFrequency((LARGE_INTEGER *)&nFreq);
	
//    __asm__("_emit $0x0F");
//    __asm__("_emit $0x31");

    __asm__("mov _start,%eax");
    __asm__("mov 4+(_start),%edx");
	
    QueryPerformanceCounter((LARGE_INTEGER *)&nCtrStop);
    nCtrStop += nFreq;
    do
	{
        QueryPerformanceCounter((LARGE_INTEGER *)&nCtr);
    } while (nCtr < nCtrStop);
    
//    __asm__("_emit $0x0F");
//    __asm__("_emit $0x31");

    __asm__("mov %eax,_stop");
    __asm__("mov %edx,4+(_stop)");


    return ((stop-start)/1000000);
}



kev

Thanks guys for your help, Ive got it working now.

This is what it should have been...

//---- file: cpuspeed.c ----

#include <windows.h>

unsigned __int64 start, stop;
unsigned __int64 nCtr, nFreq, nCtrStop;

int CpuSpeed(void)
{
    QueryPerformanceFrequency((LARGE_INTEGER *)&nFreq);

    __asm__(".byte 0x0F");
    __asm__(".byte 0x31");
    __asm__("mov %eax,_start");
    __asm__("mov %edx,4+(_start)");
	
    QueryPerformanceCounter((LARGE_INTEGER *)&nCtrStop);
    nCtrStop += nFreq;
    do
	{
        QueryPerformanceCounter((LARGE_INTEGER *)&nCtr);
    } while (nCtr < nCtrStop);

    __asm__(".byte 0x0F");
    __asm__(".byte 0x31");
    __asm__("mov %eax,_stop");
    __asm__("mov %edx,4+(_stop)");

    return ((stop-start)/1000000);
}