Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AT&T syntax hello world works but intel syntax does not

I am writing a hello world program for linux x86_64 using GAS assembler and here is my code in AT&T syntax.

#.intel_syntax noprefix

.section .data
    msg:
        .ascii "hello world\n"
.section .text
.globl _start
_start:
    movq  $1, %rax
    movq  $1, %rdi
    movq  $msg, %rsi
    movq  $12, %rdx
    syscall

    movq  $60, %rax
    movq  $0, %rdi
    syscall

This works and prints "hello world". Here is intel syntax:

.intel_syntax noprefix

.section .data
    msg:
        .ascii "hello world\n"
.section .text
.globl _start
_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, 12
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

This compiles and runs fine but does not print "hello world". I am assuming the mistake is in mov rsi, msg? If so, what is the correct syntax?

like image 527
wingerse Avatar asked Aug 31 '25 17:08

wingerse


1 Answers

Try mov rsi, offset msg. gas uses a masm-like syntax where mov rsi, msg moves the content of msg to rsi instead of moving the address. The offset keyword has to be used to avoid this issue.

If you want to program in Intel syntax, I advise you to pick a better assembler like nasm.

like image 107
fuz Avatar answered Sep 02 '25 14:09

fuz