I have an assembly hello world program for Mac OS X that looks like this:
global _main
section .text
_main:
mov rax, 0x2000004
mov rdi, 1
lea rsi, [rel msg]
mov rdx, msg.len
syscall
mov rax, 0x2000001
mov rdi, 0
syscall
section .data
msg: db "Hello, World!", 10
.len: equ $ - msg
I was wondering about the line lea rsi, [rel msg]
. Why does NASM force me to do that? As I understand it, msg
is just a pointer to some data in the executable and doing mov rsi, msg
would put that address into rsi
. But if I replace the line lea rsi, [rel msg]
with , NASM throws this error (note: I am using the command nasm -f macho64 hello.asm
):
hello.asm:9: fatal: No section for index 2 offset 0 found
Why does this happen? What is so special about lea
that mov
can't do? How would I know when to use each one?
mov rsi, msg
uses an absolute address that would have to change depending on where the program is loaded, and Mach-O doesn't support that. – Ross Ridgemov rsi, msg
instruction it loads the register with the value encoded as an immediate operand. That immediate value needs to be the actual address ofmsg
. Mach-O doesn't support that. – Ross Ridgemsg
will be? – Jerfov2msg
will be located. By using RIP relative addressing it doesn't need to. – Ross Ridge