LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2005, 08:22 PM   #1
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Problem linking assembly program with C lib.


I am trying to link my simple program to the C library, here is the code:

example.asm
Code:
segment .data
        asdf db "Hello, world!",0
        format db "%s\n",0

segment .text
    global _start
    extern printf

_start:
    mov eax,asdf
    push eax
    mov eax,format
    push eax
    call printf

    mov ebx,0
    mov eax,1
    int 0x80
Then I compile and link with:

Code:
nasm -f elf example.asm
ld -s -o example example.o -lc
Then when I try to run it:
Code:
./example
It tells me:

bash: ./example: No such file or directory

If I omit the printf call, and don't link with the C library (don't include the -lc) then it runs it fine. Can anyone tell me what is wrong?
 
Old 03-08-2005, 09:44 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Original Poster
Rep: Reputation: 32
Nevermind, I got it. The C library uses it's own entry point for your program. I guess the C library has to do some stuff first (and some stuff afterwards). It then calls your programs main. So, instead of _start, it's main. And I shouldn't have used linux's exit system call, but just return instead. The C library flushes the buffers and frees the files and whatnot. Example:
new_example.asm
Code:
extern printf
section .data
        asdf db "Hello, world!",0
        format db "%s",0xa,0

section .text
    global main

main:
    push dword asdf
    push dword format
    call printf
    add esp,8

    xor eax,eax
    ret
Then instead of using ld, I just used gcc..

gcc -o new_example new_example.o

Last edited by 95se; 03-08-2005 at 09:47 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash routine into assembly program rblampain Linux - Security 3 08-06-2005 01:49 AM
Please help me with my assembly program flamesrock Programming 2 01-30-2005 10:27 PM
puzzle about g++ compile options for inline assembly program markbeth Programming 2 09-20-2004 04:30 AM
Linking whole archive into shared lib mj_ Programming 0 07-23-2004 06:00 AM
Linking program in g++? Eits0 Programming 6 06-27-2002 08:02 AM


All times are GMT -5. The time now is 11:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration