11

I'm working through some of the tutorials on http://www.ibm.com/developerworks/linux/library/l-gas-nasm/index.html to familiarize myself with x86/x64. This tutorial code compiles and runs without a hiccup using the provided code, which uses AT&T syntax:

.global main
.text
main:                               # This is called by C library's startup code
    mov     $message, %rdi          # First integer (or pointer) parameter in %edi
    call    puts                    # puts("Hello, World")
    ret                             # Return to C library code
message:
    .asciz "Hello, World"           # asciz puts a 0x00 byte at the end

However, when I convert this code to Intel syntax, I get a "Segmentation fault" error.

.intel_syntax noprefix
.global main
.text
main:                               # This is called by C library's startup code
    mov     rdi, message            # First integer (or pointer) parameter in %edi
    call    puts                    # puts("Hello, World")
    ret                             # Return to C library code
message:
    .asciz "Hello, World"           # asciz puts a 0x00 byte at the end

I'm not familiar with x86, so perhaps I'm missing something. Any ideas?

Alex
  • 599
  • 1
  • 4
  • 16
  • Did you compare the binary outputs? – Jens Björnhager Apr 20 '13 at 14:07
  • @JensBjörnhager, I did not compare the binary outputs. I'm a novice in assembly, and I know even less binary. Is there something specific I can get from comparing the binaries without being a low-level guru? – Alex Apr 20 '13 at 14:11
  • Probably, `mov rdi, offset message` was mentioned? – Egor Skriptunoff Apr 20 '13 at 14:11
  • 1
    @EgorSkriptunoff, changing `mov rdi, message` to `mov rdi, offset message` works. Would you be able to point me in the right direction as to why this works? I assume it has something to do with pointing `rdi` to the location in memory right before `message` starts? Why is an offset necessary in Intel's syntax but not AT&T's? – Alex Apr 20 '13 at 14:14

1 Answers1

15

In AT&T syntax, mov $message, %rdi, the $ means immediate, meaning the address of message.

In GAS's Intel syntax, mov rdi, message means absolute addressing, meaning the content at message. To get the actual address of message, you need to supply the offset keyword: mov rdi, offset message.

Disassebly of the two binaries shows the difference:

AT&T:

0000000000000000 <main>:
0:   48 c7 c7 00 00 00 00    mov    $0x0,%rdi

Intel:

0000000000000000 <main>:
0:   48 8b 3c 25 00 00 00 00    mov    0x0,%rdi
     
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47