1

First I'm on OS X(x86-64 set) and I use nasm. I'm following a tutorial to asm, and I'm writing a function to reverse a string. My problem is that I can't put byte in my resb buffer. I have Mach-O 64-bit format does not support 32-bit absolute addresses error. I did some research, tried a lot of things(like lea, or mov into a register first) nothing seems to solve my problem.

Below is my code and my problem is on: mov [output + rdi], eax(I'm trying to write the reverse string of input db buffer to output resb 12 buffer) in reverseStr label

section .data
    nl db 0xa
    input db "Hello world!"

section .bss
    output resb 12

section .text
    global start
    global _main

start:
    jmp _main
    ret

_main:
    push rbp
    mov rbp, rsp
    mov rsi, input
    xor rcx, rcx
    cld
    mov rdi, $ + 15
    call calculateStrLength
    xor rax, rax
    xor rdi, rdi
    jmp reverseStr

calculateStrLength:
    cmp byte [rsi], 0
    je exit
    lodsb
    push rax
    inc rcx
    jmp calculateStrLength

reverseStr:
    cmp rcx, 0
    ;je printResult
    pop rax
    mov [output + rdi], eax
    dec rcx
    inc rdi
    jmp reverseStr
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
renan
  • 167
  • 2
  • 9
  • 4
    You can always get the 64-bit address of `output` and put it in a register (like _RDX_) and then do the memory access with _RDX_. You can do `lea rdx, [rel output]` (the `rel` forces it to use RIP-relative addressing) to load and then do the MOV this way `mov [rdx + rdi], eax` . Since `output` never changes you can do the `lea` and load _RDX_ once outside the loop. – Michael Petch Jun 25 '17 at 22:21
  • Yes you are right, it worked well. Thanks. – renan Jun 26 '17 at 16:06
  • You should probably use `default rel` at the top of your file. It's unfortunately not enabled by default in NASM/YASM. – Peter Cordes Jul 01 '17 at 22:28

0 Answers0