0

I am trying to teach myself NASM on my 64-bit MacBook Pro. I've got the typical Hello World program compiling and running correctly.

I'm trying to do some simple arithmetic by following the code here... http://www.csee.umbc.edu/portal/help/nasm/intarith_64.asm

However I cannot get a simple assignment to work. When I try and compile this using nasm -f macho64 filename.asm I get the following message: error: Mach-O 64-bit format does not support 32-bit absolute addresses referring to the line mov [b], rax. Does anyone have any suggestions on how to overcome this?

global start

section .data
a:      dq      1

section .bss

b:      resq    1

section .text

    start:
        mov rax, [a]
        mov [b], rax
soarjay
  • 641
  • 4
  • 17
  • 1
    You could use the `default rel` directive or use `rel` inside the square brackets like `mov rax, [rel a]` – Michael Petch Jun 19 '16 at 02:05
  • `default rel` did the trick. Thanks :) Why does the example code (from the link in the question) not need to do this? – soarjay Jun 19 '16 at 02:14
  • Because it appears the code at that link was being used to compile on 64-bit Linux using the ELF64 format and not OS/X MACHO64 format. OS/X requires executables to use Position Independent Code (PIC), unlike Linux. – Michael Petch Jun 19 '16 at 02:20

1 Answers1

0

From the NASM manual:

The only instructions which take a full 64-bit displacement is loading or storing, using MOV AL, AX, EAX or RAX (but no other registers) to an absolute 64-bit address. Since this is a relatively rarely used instruction (64-bit code generally uses relative addressing), the programmer has to explicitly declare the displacement size as QWORD:

 default abs 

 mov eax,[foo]           ; 32-bit absolute disp, sign-extended 
 mov eax,[a32 foo]       ; 32-bit absolute disp, zero-extended 
 mov eax,[qword foo]     ; 64-bit absolute disp 

 default rel 

 mov eax,[foo]           ; 32-bit relative disp 
 mov eax,[a32 foo]       ; d:o, address truncated to 32 bits(!) 
 mov eax,[qword foo]     ; error 
 mov eax,[abs qword foo] ; 64-bit absolute disp
John Burger
  • 3,662
  • 1
  • 13
  • 23
  • That's true, but it's not the answer to this question. Addresses that fit in the low32 can use a normal `mod/rm`-encoded address with the absolute address as the disp32. OS X mach-o64 object files / executables don't support absolute addressing at all; i.e. even executables must use position-independent code. I assume this is to enable ASLR for executables, not just libraries. I went into a bit of detail about this on [x86 addressing modes answer](http://stackoverflow.com/a/34058400/224132). I think I actually left out `movabs`, though! – Peter Cordes Jun 19 '16 at 08:27
  • Oh, I didn't realize NASM has an `a32` keyword. YASM doesn't support it, but time to update my answer on [this question about the mov eax, moffs32 encoding in 64bit mode](https://stackoverflow.com/questions/37665819/sign-or-zero-extension-of-address-in-64bit-mode-for-mov-moffs32). – Peter Cordes Jun 19 '16 at 08:31