I\'m trying to learn how to write assembly code and I\'m doing it with the help of http://gnu.mirrors.pair.com/savannah/savannah//pgubook/ProgrammingGroundUp-0-8.pdf. It\'s
Mach-O 64-bit does not support 32-bit absolute addressing because the image base is greater than 2^32.
Normally you should use RIP relative addressing for accessing a single memory element. In your case however you're accessing a static array (arrays allocated in the data section/bss section) and
as explained in the the section Addressing static arrays in 64 bit mode in Agner Fog's Optimizing Assembly manual.
It is not possible to access static arrays with RIP-relative addressing and an index register.
So when NASM processes your code
mov rax, [data_items+rbx*4]
it can't do RIP relative addressing so it tries to 32-bit absolute + index address which is not allow with Mach-O 64-bit which causes NASM to report the error.
Exampels 3.11b-3.11d In Agner's manual presents three ways to access static arrays. However, since 64-bit OSX does not allow 32bit absolute addressing (though it's possible in Linux) the first example 3.11b is not possible.
Example 3.11c uses the image base reference point __mh_execute_header
. I have not looked into this but 3.11d is easy to understand. Use lea
to load the RIP+offset into a register like this:
lea rsi, [rel data_items]
And then change your code using mov rax, [data_items+rbx*4]
to
mov rax, [rsi+rbx*4]
Since you have delcared DEFAULT REL
you should be able to ommit the rel in [rel data_items]
.