Reputation: 158
Hello I would like to begin coding a little in assembly.
I have installed the latest nasm version (2.15). I followed all the steps and I set up nasm correctly.
Now I wanted to run my first hello world program using:
nasm -f macho64 hello.asm
or also
nasm -fmacho64 hello.asm && ld hello.o && ./a.out
or also
nasm -f macho64 hello.asm -o hell.o && ld -e _main -macosx_version_min 10.8 -arch x86_64 hello.o -lSystem
and none of these work. Actually nasm -f macho64 hello.asm
does not throw errors but does not print to the screen either. This is the code I copied and pasted to see if everything worked:
; Writes "Hello, World" to the console using only system calls. Runs on 64-b
; To assemble and run:
;
; nasm -fmacho64 hello.asm && ld hello.o && ./a.out
; --------------------------------------------------------------------------
global start
section .text
start: mov rax, 0x02000004 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do
mov rax, 0x02000001 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exi
section .data
message: db "Hello, World", 10 ; note the newline at the end
and this is the error that I get from running the other two commands:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
Could someone help me and tell me how to set up everything correctly. As I said the command nasm -f macho64 hello.asm
does not give any error but it does not print anything out but it should, so either it is a problem with the actual code or it is a problem with nasm itself. Could you also give me a brief explanation of why the other two commands fail. Thanks in advance :)
Upvotes: 0
Views: 507
Reputation: 168967
You'll just need to adjust your code a little and link it with the correct switches (see _main
instead of start
):
global _main
section .text
_main: mov rax, 0x02000004 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do
mov rax, 0x02000001 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exi
section .data
message: db "Hello, World", 10 ; note the newline at the end
$ nasm -fmacho64 hello.asm && ld -e _main hello.o -lSystem && ./a.out
Hello, World
$
Upvotes: 1