0

I have been following this tutorial and have got stuck at the following code: leaq str(%rip), %rdi.

My full assembly code is the following:

.data
.text
.globl _main
_main:
  pushq %rbp
  movq %rsp, %rbp
  subq $32, %rsp

  leaq str(%rip), %rdi
  callq _printf

And my Makefile:

build:
  as main.s -o main.o
  ld main.o -e _main -lc -macosx_version_min 10.13 -arch x86_64  -o main

run: build
  ./main

And finally the output of the command make build:

as main.s -o main.o
ld main.o -e _main -lc -macosx_version_min 10.13 -arch x86_64  -o main
Undefined symbols for architecture x86_64:
  "str", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
make: *** [build] Error 1

How am I able to link the str command for use in macOS assembly?

iProgram
  • 6,057
  • 9
  • 39
  • 80
  • `str()` isn't a function, operator, or "command". `constant(%register)` is AT&T addressing mode syntax, and your code doesn't define that label. Put a `str: .asciz "Hello World\n"` in the `.rodata` section of your program. Exactly like the tutorial shows... (Actually they put it in `.data`, which is silly because you don't want to modify it.) – Peter Cordes Sep 25 '18 at 10:41
  • @PeterCordes Thanks for that. I miss understood it first of all. I was thinking it would have printed out the first command line argument. I shall try later when I get Home. – iProgram Sep 25 '18 at 10:49
  • Maybe you should find a tutorial that understands what's going on. Not one that gets to a section like "Third, why are we copying address to %rdi register. I don’t have proper explanation". Uh, because that's how the calling convention works. [What are the calling conventions for UNIX & Linux system calls on i386 and x86-64](https://stackoverflow.com/q/2535989). Also [Understanding %rip register in intel assembly](https://stackoverflow.com/a/50414538) and [How to use RIP Relative Addressing in a 64-bit assembly program?](https://stackoverflow.com/a/13356588) for RIP-relative. – Peter Cordes Sep 25 '18 at 10:49
  • 1
    There are some good links in https://stackoverflow.com/tags/x86/info to good guides and reference material, and most of the highly-voted stuff on stack overflow is good. But on sites where there's no mechanism for downvoting wrong or confusing stuff, it's pure luck whether you get something good. At least a tutorial that *admits* they don't know what's going on is better than one claiming to be authoritative and actually being wrong. – Peter Cordes Sep 25 '18 at 10:51

0 Answers0