2
votes

I have written a small piece of assembly with AT&T syntax and have currently declared three variables in the .data section. However, when I attempt to move any of those variables to a register, such as %eax, an error from gcc is raised. The code and error message is below:

.data
  x:.int 14
  y:.int 4
  str: .string "some string\n"

.globl _main

_main:
  pushq %rbp
  movq %rsp, %rbp
  subq $16, %rsp
  movl x, %eax; #attempting to move the value of x to %eax;
  leave
  ret

The error raised is:

call_function.s:14:3: error: 32-bit absolute addressing is not supported in 64-bit mode

movl x, %eax;

^

I have also tried moving the value by first adding the $ character in front of x, however, a clang error is raised:

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Does anyone know how the value stored in x can be successfully moved to %eax? I am using x86 assembly on Mac OSX and compiling with gcc.

1
Use movl x(%rip), %eax.Jester
@Jester Thank you, x(%rip) works! If you could write an answer with an explanation, I will accept it.Ajax1234
The error message is slightly dishonest. Yes, movl x, %eax is supported by the architecture, but you are not supposed to use it.fuz
@fuz: In AT&T syntax, the moffs forms of mov (with a 64-bit absolute address) use the movabs mnemonic, so plain mov implies you want the mov r32, r/m32 opcode with a ModR/M byte. The MachO64 output format doesn't support 32-bit absolute relocations at all, and the image base address is above 2^32 on OS X so a [disp32] addressing mode can't work. Thus for mov, RIP-relative is the only option.Peter Cordes

1 Answers

6
votes

A RIP-relative addressing mode is the only good option for addressing static data on MacOS; the image base address is above 2^32 so 32-bit absolute addresses aren't usable even in position-dependent code (unlike x86-64 Linux). RIP-relative addressing of static data is position-independent, so it works even in position-independent executables (ASLR) and libraries.

movl x(%rip), %eax is the AT&T syntax for RIP-relative.

mov eax, dword ptr [rip+x] in GAS .intel_syntax noprefix.

Or, to get the address of a symbol into a register, lea x(%rip), %rdi


NASM syntax: mov eax, [rel x], or use default rel so [x] is RIP-relative.

See Mach-O 64-bit format does not support 32-bit absolute addresses. NASM Accessing Array for more background on what you can do on OS X, e.g. movabs x, %eax would be possible because the destination register is AL/AX/EAX/RAX. (64-bit absolute address, but don't do that because it's larger and not faster than a RIP-relative load.)

See also http://felixcloutier.com/x86/MOV.html.