1

Could someone explain to me what leaq str(%rip), %rsi does and why there isn't a simple mov operation to move the value of the string in a register? This is a hello world script for macOS x86-64.

I understand that it's setting the rsi register with the address of the string + the address of rip register. But why have to offset from the address of the rip register?

.section __DATA,__data
str:
  .ascii "Hello world!\n"
  len = . - str

.section __TEXT,__text
.globl start
start:

movl $0x2000004, %eax        
movl $1, %edi

leaq str(%rip), %rsi  

movq $len, %rdx                
syscall

movl $0x2000001, %eax 
movl $0, %ebx         
syscall
Jester
  • 56,577
  • 4
  • 81
  • 125
  • It's done that way to make it position independent which is required on macos nowadays. Note that it's not actually adding the address of `str` to `rip`, the assembler/linker will encode the correct offset into the instruction such that the end result is the address of `str`. – Jester Jun 27 '19 at 00:34
  • `str(%rip)` means symbol address *with respect to* RIP, not absolute address + RIP. The duplicate asks about the Intel syntax equivalent, `[rip + str]`, but my answer covers both Intel and AT&T syntax. – Peter Cordes Jun 27 '19 at 00:44
  • Maybe a duplicate - but upvoted for a good first question. – Brett Hale Jul 01 '19 at 13:06

0 Answers0