Page 1 of 1

problem interfacing GCC and NASM

Posted: Mon Oct 13, 2003 12:25 pm
by Neo
I have a problem interfacing GCC and NASM code.(I'm a beginner at this)
I first created a NASM FILE with this function as shown below

Code: Select all

[bits 32]
[section text]
x_Four   equ   8
[global _func1]
_func1:
   push   ebp
   mov   ebp,esp
   
   mov   eax,[ebp+x_Four]
   add   eax,4

   mov   esp,ebp
   pop   ebp   
   ret
and then a GCC file as shown

Code: Select all

extern unsigned int func1(unsigned int);
int main(void)
{
   printf("Result = %i\n",func1(10));
   return 0;
}
i then compiled them using
nasm -f coff test.asm
gcc -o test.exe testc.c test.o
and obtained an executable file test.exe(I'm using windows)on running this file i get the following
[tt]
Exiting due to signal SIGSEGV
Bounds Check at eip=000101c2
eax=00000000 ebx=0000834b ecx=00000000 edx=00000340 esi=0000005c edi=00000000
ebp=0008fb40 esp=0008fb28 program=C:\NASM\TEST.EXE
cs: sel=00a7 base=834bd000 limit=0009ffff
ds: sel=00af base=834bd000 limit=0009ffff
es: sel=00af base=834bd000 limit=0009ffff
fs: sel=0087 base=00011090 limit=0000ffff
gs: sel=00bf base=00000000 limit=0010ffff
ss: sel=00af base=834bd000 limit=0009ffff
App stack: [0008fb60..0000fb60] Exceptn stack: [0000fac0..0000db80]

Call frame traceback EIPs:
0x000101c2
0x00002e78
[/tt]
Pls help me out as i cant figure out whats wrong and I want to interface NASM and GCC.Thanx in advance.

Re:problem interfacing GCC and NASM

Posted: Mon Oct 13, 2003 12:47 pm
by HOS
at the end of your asm function, do not do

Code: Select all

mov esp, ebp

Re:problem interfacing GCC and NASM

Posted: Mon Oct 13, 2003 3:20 pm
by Pype.Clicker
HOS wrote: at the end of your asm function, do not do

Code: Select all

mov esp, ebp
i don't see a good reason why he couldn't do that, as it will just restore the clean stack pointer saved by "mov ebp,esp".

Imho, the best for you to discover the problem will be to run your program in GDB or some equivalent debugger and see where exactly your error occurs.

Re:problem interfacing GCC and NASM

Posted: Tue Oct 14, 2003 3:25 am
by BI lazy
what's this %i option in his printf? 've never seen this one. 'd expect %d at such a place. Or has printf changed over the years?

Re:problem interfacing GCC and NASM

Posted: Tue Oct 14, 2003 3:42 am
by Pype.Clicker
%i and %d are synonyms. they're both used for "signed native-sized integer" ... now, many home-made implementation of printf tends to have only "%d" or only "%i" depending on the homekeeper's mood.

Re:problem interfacing GCC and NASM

Posted: Tue Oct 14, 2003 3:43 am
by BI lazy
after having done some googling, I've found out. it exists. Hm...

the only thing I do different is at the end: I don't issue
mov esp,ebp. That's all. But I canna imagine this to do is an error causing the program to crap out.

Sorry, that I can't help more. either the %i is not implemented in gcc, or ... but windows requires underscore (name mangling), doesn't it?

Re:problem interfacing GCC and NASM

Posted: Tue Oct 14, 2003 3:55 am
by Pype.Clicker
if GDB sounds too complicated, you can also just compile your code with debugging informations (-g) and do an objdump -drS test.exe, then look for the crash address 101c2 ...

Re:problem interfacing GCC and NASM

Posted: Wed Oct 15, 2003 1:20 pm
by Neo
Where can i get GDB?

Re:problem interfacing GCC and NASM

Posted: Thu Oct 16, 2003 1:11 am
by Pype.Clicker
it should be made available with GCC ... wherever you got GCC you should get GDB aswell (either on www.djdelorie.com or some alternate location depending on your 'gcc provider')

<offtopic severity="slightly"> and for the happy Linux ownerz, don't forget the graphical front-end DDD for GDB. It's nice and may change the way you debug stuff :) </offtopic>

Re:problem interfacing GCC and NASM

Posted: Thu Oct 16, 2003 1:24 am
by Solar
Hehe... if you have to ask where to get it, I think that qualifies as "gdb sounds too complicated". 8) Try Pype's suggestion regarding objdump instead.

Unless, of course, you think this is a good time to learn what there is to learn about gdb. Sooner or later, you'll need it anyway. ;)