0

I want to start learning assembly language, but all the googling didn't make any sense. I got some Exec format error and even used wine which is not good for understanding. So I wonder if anyone can tell what command line assembler will do on x86-64 architecture and probably some hello world example for Linux?

5

There are quite a few assemblers available, including:

  • gas (part of binutils, and supported by GCC) — this is available everywhere, and uses AT&T style;
  • NASM (look for a nasm package in your distribution) — this supports Intel-style mnemonics;
  • Yasm which is a rewrite of NASM (look for a yasm package).

Here's a “Hello world” for gas:

    .global _start

    .text
_start:
    mov     $1, %rax
    mov     $1, %rdi
    mov     $hello, %rsi
    mov     $13, %rdx
    syscall

    mov     $60, %rax
    xor     %rdi, %rdi
    syscall

hello:
    .ascii  "Hello, world\n"

Save this to hello.S, and build it using gcc -c hello.S && ld -o hello hello.o.

The equivalent for NASM is:

    section .text
    global  _start

_start:
    mov     rax, 1
    mov     rdi, 1
    mov     rsi, hello
    mov     rdx, len
    syscall

    mov     rax, 60
    xor     rdi, rdi
    syscall

    hello   db "Hello, world",0x0A
    len     equ $ - hello

Save this as hello.asm, and build it using nasm -felf64 hello.asm && ld -o hello hello.o.

1

NASM is a good assembler for Linux/Unix. You can find plenty of examples from google for NASM 64-bit code.

For Centos you can install NASM with:

yum install nasm -y

For debian/ubuntu it's probably:

apt-get install nasm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.