How to compile a att syntax .asm file to an executable on linux with nasm / gcc

Viewed 98

I have some asm code that I just want to compile. I have looked at some examples on stack and they always produce errors.

My code:

.data
hello:
        .string "Hello World!\n"

.text
.globl main
main:
        movl $4, %eax
        movl $1, %ebx
        movl $hello, %ecx
        movl $13, %edx
        int $0x80

        movl $1, %eax
        movl $0, %ebx
        int $0x80

What I tried:

nasm -f elf hello.asm

Error produced:

hello.asm:1: warning: label alone on a line without a colon might be in error [-w+label-orphan]
hello.asm:3: error: parser: instruction expected
hello.asm:5: warning: label alone on a line without a colon might be in error [-w+label-orphan]
hello.asm:6: error: parser: instruction expected
hello.asm:8: error: parser: instruction expected
hello.asm:9: error: parser: instruction expected
hello.asm:10: error: parser: instruction expected
hello.asm:11: error: parser: instruction expected
hello.asm:12: error: expression syntax error
hello.asm:14: error: parser: instruction expected
hello.asm:15: error: parser: instruction expected
hello.asm:16: error: expression syntax error
1 Answers

The example you have is for the GNU assembler, not for NASM. To assemble it, type cc -m32 -o hello hello.s after renaming the file to hello.s.

The same example in NASM syntax would be

        section .data
hello:  db      "Hello World!", 10, 0

        section .text
        global  main
main:   mov     eax, 4
        mov     ebx, 1
        mov     ecx, hello
        mov     edx, 13
        int     0x80

        mov     eax, 1
        mov     ebx, 0
        int     0x80

To assemble and link this NASM example, type

nasm -felf32 hello.asm
cc -m32 -o hello hello.o
Related