Page 1 of 1
c++, calling asm functions
Posted: Tue Jun 28, 2005 6:46 am
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?
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 7:28 am
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???
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 7:29 am
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
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]
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 7:34 am
by guest
im linking 3 files with the linker
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 7:54 am
by Pype.Clicker
iirc, you should tell C++ that your asm function is
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).
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 7:55 am
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 ??
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 8:01 am
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.
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 8:08 am
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
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 8:12 am
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
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 8:38 am
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.
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 10:22 am
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.
Re:c++, calling asm functions
Posted: Tue Jun 28, 2005 1:15 pm
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