Chameera Dedduwage
Chameera Dedduwage

Reputation: 537

Error calling standard C library functions from NASM?

I'm quite new to assembly programming. I use NASM 2.11.05 on a Windows 7 (64-bit) platform to run some sample code. The problem arises when I try to call standard C functions from my assembly code. This is my assembly source:

global  main
        extern  puts

        section .text
main:                                   
        push    message            
        call    puts                    
        ret                             
message:
        db      "Hola, mundo", 0 

When I compile with NASM, I use this command line: nasm -fwin32 file.asm which produces file.obj. Now, when I try to link it with ld or gcc, I keep getting errors. Some things I tried:

  1. gcc -m32 -nostartfiles file.obj (gives the error that i386:x86-64 architecture of input file is not compatible with i386 output).

  2. ld file.obj (gives the error undefined reference to puts).

Can anyone please guide me on how to resolve this?

Upvotes: 1

Views: 490

Answers (2)

user6829088
user6829088

Reputation:

You can just compile in a different way, like:

  • Create an object of your .asm file with:

    1. GCC: nasm -f elf file.asm
      or
    2. LD: ld -m elf_i386 file.o -o file

  • Link object file created with gcc -m32 -o file file.o
  • Run with ./file

    Upvotes: 0

  • Chameera Dedduwage
    Chameera Dedduwage

    Reputation: 537

    In the end, one line at the top of my ASM file settled it. This is that line.

    [BITS 32]
    

    However, the output file still keeps crashing: anyone who can explain that is welcome!

    Upvotes: 1

    Related Questions