NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: dalfonso01 on September 07, 2014, 04:24:26 PM
-
Hi,
I found another post where there was a note to avoid _ before the procedure name with elf. I cannot find the reason this do not work.
Sorry if the question is trivial.
More I would know if I should expect some change using the g++ and generally speaking a native c++ instead than c during this operations.
Thanks
This is the c++
#include<cstdio>
int main(void) {
int x = 25, y = 70;
int value ;
extern int test1(int,int,int);
value = test1(x, y, 5);
printf("Result = %d\n", value);
return 0;
}
This is the nasm
section .text ; Section containing code
global test1
test1:
enter 0,0
mov eax,[ebp+8] ; get argument 1 (x)
add eax,[ebp+12] ; add argument 2 (y)
sub eax,[ebp+16] ; substract argument 3 (5)
leave
ret
This is the make result:
nasm -f elf -g -F dwarf hll1-asm.asm -l hll1-asm.lst
gcc -o hll1 hll1-cpp.o hll1-asm.o
hll1-cpp.o: In function `main':
hll1-cpp.cpp:(.text+0x31): undefined reference to `test1(int, int, int)'
collect2: ld returned 1 exit status
make: *** [hll1] Error 1
Thanks
Fabio D'Alfonso
-
Hi Fabio,
C++ is above my pay grade, but by flailing around with it, I got this to work:
#include<cstdio>
extern "C" int test1(int,int,int);
int main(void) {
int x = 25, y = 70;
int value ;
value = test1(x, y, 5);
printf("Result = %d\n", value);
return 0;
}
I think you have to tell g++ "-g" also to get debug info passed through.
Best,
Frank
-
Hi,
thanks.
I see now that with a .c extension it works (with <stdio.h>) without the "C" , while the compilation with cpp extension gives the error. It is related to the support to overloading support to function from c++ and produces some different internal name.
Thanks
Fabio D'Alfonso
-
When writing assembler which will be called from C or C++ you need to understand how the compiler is going to mangle the name of the method. To see what g++ is looking for you would add the -S switch on the command line and look at the output assembler generated. Then you would have to name your assembler method identically in order for it to link properly.
The resulting name is specific to GCC. If you were to use another compiler then the method signature will be mangled in a completely different manner. The C++ specification itself does not define how to mangle names.
Given that scenario most folks writing assembly which will be called by either C or C++ use the simple extern "C" specification to define the function interface. Now your method can be called by many different compilers. Note, however, than you still need to be aware of whether the C convention for you platform requires an underscore prepended to the name. For example, MS Windows does use the underscore whilst Linux GCC does not.