0

I am trying to learn assembly language and am trying to do a basic "Hello, World" output. The error I am getting is:

ld: warning: -arch not specified
ld: warning: -macosx_version_min not specified, assuming 10.10
ld: warning: ignoring file hw.o, file was built for unsupported file format 
( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) 
which is not the architecture being linked (x86_64): hw.o
Undefined symbols for architecture x86_64:
  "start", referenced from:
    implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64

My .asm file:

1 section .data
msg db "hello, assembly!"

section .text
global _start
_start:
   mov rax, 1      ; rax: temp register; when we call syscall, rax must contain syscall number
                   ; 1 means that we will use sys_write system call
   mov rdi, 1      ; rdi: used to pass 1st argument to function
                   ; it will be the first argument of sys_write
   mov rsi, msg    ; rsi: pointer used to pass 2nd argument to functions
                   ; msg will be second buf argument for sys_write
   mov rdx, 13     ; rdx: used to pass 3rd argument to function
                   ; third paramater, length of the string. 3rd argument of sys_write
   syscall         ; we have all arguments now we call sys_write with syscall
   mov rax, 60     ; pass 60 to rax for syscall exit
   mov rdi, 0      ; error code, so with 0 are program must exit succesfully
   syscall         ; execute exit code

What I am entering in the command prompt:

$ nasm -f elf64 -g -F dwarf hw.asm
$ ld *.o

After I execute the second line on the command prompt, I get the error above.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Nick
  • 1
  • 2
  • 2
    You're running this on OSX..? Then shouldn't you use `-f macho64` instead? – Michael Aug 29 '15 at 06:41
  • @Michael Yes, I am on OSX. I tried the macho64 command and it left the error, `fatal: No section for index 2 offset 0 found` – Nick Aug 29 '15 at 06:51
  • @Michael: Note that nasm has a bug with macho64 format, see http://stackoverflow.com/a/31640622/224132. Nick: Also see that question for how to assemble and link on OS X. Your `ld` fails because, as it says, `hw.o` isn't an object file in the native format. – Peter Cordes Aug 29 '15 at 08:08

0 Answers0