LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem linking assembly program with C lib. (https://www.linuxquestions.org/questions/programming-9/problem-linking-assembly-program-with-c-lib-299373/)

95se 03-08-2005 08:22 PM

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?

95se 03-08-2005 09:44 PM

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


All times are GMT -5. The time now is 06:05 AM.