problem interfacing GCC and NASM

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
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

problem interfacing GCC and NASM

Post 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.
Only Human
HOS

Re:problem interfacing GCC and NASM

Post by HOS »

at the end of your asm function, do not do

Code: Select all

mov esp, ebp
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:problem interfacing GCC and NASM

Post 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.
BI lazy

Re:problem interfacing GCC and NASM

Post 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?
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:problem interfacing GCC and NASM

Post 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.
BI lazy

Re:problem interfacing GCC and NASM

Post 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?
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:problem interfacing GCC and NASM

Post 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 ...
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:problem interfacing GCC and NASM

Post by Neo »

Where can i get GDB?
Only Human
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:problem interfacing GCC and NASM

Post 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>
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:problem interfacing GCC and NASM

Post 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. ;)
Every good solution is obvious once you've found it.
Post Reply