views:

271

answers:

4

Hi,

I have question about inline assembler. It's possible to call another assembler subroutine from inline assembler within the same function? For example:

void FindValidPID(unsigned int &Pid) 
{
    __asm
    {
        sub esp, 20h
        mov eax, Pid
        add eax,eax
        call sub123 ; another assm subroutine
        mov Pid, eax
        add esp, 20h
    }
}

Where I should, and how, write subroutine sub123?

Cheers,
Thomas

+2  A: 

If you are writing an entire subroutine in assembly, you should look into using the file-level assembler rather than inline.

Potatoswatter
A: 

Suppose you have a subroutine _func2 written in assembler (I only know NASM syntax).

    global  _func2

    section .text
_func2:
    ret

You can call this subroutine from your C++ code like this

extern "C" {
void func2();
}

void func1()
{
    __asm {
        call func2
    }
}

int main()
{
    func1();
}

Of course func2 can also be a C/C++ function with an __asm block.

Fair Dinkum Thinkum
A: 

You could do it by creating the other function just as you did with the 1st one and then call it with its mangled name like call __GLOBAL__I_sub123;.

It might be a good idea to declare it as extern "C" to make things simpler.

In gcc, you can use gcc -S file.cpp to see what is the funciton's mangled name.

Gianni
+1  A: 

From the syntax, I assume the compiler being used is VC++? If so, a naked function (http://msdn.microsoft.com/en-us/library/5ekezyy2(VS.80).aspx) should let you define a function that can be easily called from assembly language code.

(Naked functions are also visible to C++, so you can call them from C++ as well and potentially get the wrong results... so you just have to be careful!)

brone