c++, calling asm functions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
guest

c++, calling asm functions

Post by guest »

how would i call external asm functions that i have written in another file. i need to declare these external functions some how so i dont get a compile error and i was wondering how do i do this and how do i actually call them?
guest

Re:c++, calling asm functions

Post by guest »

im using:
in my asm file(nasm):
global gdtload

in my cpp file:
extern void gdtload();

but when i call it in my cpp file:
gdtload();

the linker gives me an error saying:
undefined reference to 'gdtload()'

i think it might think the parentheses are part of the function name but i cant use parentheses in nasm???
Endar

Re:c++, calling asm functions

Post by Endar »

Code: Select all

; Assembly file
function:
     ; code
     ret

Code: Select all

// C++ file

// declare function that is present in another file for linking
extern void function(); // or whatever it returns

int main()
{
     function(); // call the function


return 0;
}

[edit]
Okay, so I wasn't completely right the first time :D
You said that you declare it as "global gdtload" inside the asm file.

This is a silly question I know, but, then again, I've missed simpler and stupider things, but are you actually linking the two (or more) things together after compiling them?

Apart from that, I have no other useful advice.
[/edit]
guest

Re:c++, calling asm functions

Post by guest »

im linking 3 files with the linker
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c++, calling asm functions

Post by Pype.Clicker »

iirc, you should tell C++ that your asm function is

Code: Select all

extern "C" gdtLoad();
or it will be looking for the mangled name instead of a flat name (e.g. something like __7gdtLoad1vv. Don't rely on me to mangle it like GCC).
guest

Re:c++, calling asm functions

Post by guest »

ok i found the problem but i dont know how to fix it

gpp trys calling the function __Z7gdtloadv instead of gdtload.
so gpp looks for that function name in my asm file. when i change it to that name in my asm file, then it links fine. But how do i get gpp to look for gdtload instead of __Z7gdtloadv in the asm file ??
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c++, calling asm functions

Post by Pype.Clicker »

btw, if you're aiming at C++, i suggest you got a look at the tutors/forums at www.invalidsoftware.net. They might help you too.
proxy

Re:c++, calling asm functions

Post by proxy »

pypeclicker's extern "C" advice should do the trick. Also keep in mind that g++ may or may not need a leading underscore in functions that come from asm.

so for example:

Code: Select all

.global function

; Assembly file
function:
    ; code
    ret

// C++ file

// declare function that is present in another file for linking
extern "C" void function(); // or whatever it returns

int main()
{
    function(); // call the function


return 0;
}

hope this helps

proxy
guest

Re:c++, calling asm functions

Post by guest »

when i do extern "C", it looks for the functions in the cpp file and now i get a compile error:

gdload was not declared in the scope
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c++, calling asm functions

Post by Pype.Clicker »

hmm ... you might wish to pick up your reference C++ book for that one. I can't remember if a specific namespace needs to be used for C externs or not.
mystran

Re:c++, calling asm functions

Post by mystran »

I'd check for typos first.

You should declare 'extern "C"' normally in the global namespace.

The only thing it does is specify linkage, which in this case means disabling name-mangling.
proxy

Re:c++, calling asm functions

Post by proxy »

well, the extern "C" is definitely mandatory unless he wants to specify the function by it's mangled name in the asm file (which would be nothing but annoying).

What compiler error do you get when you use extern "C"? did you try adding the leading underscore the asm declaration as i advised?

proxy
Post Reply