I have looked at similar questions, but cannot seem to find what is wrong with my code.
I am attempting to make the "write" syscall on MacOS to print a string to standard output.
I am able to do it with printf
perfectly, and am familiar with calling other functions in x64 assembly.
This is, however, my first attempt at a syscall
.
I am using GCC's GAS assembler.
This is my code:
.section __TEXT,__text
.globl _main
_main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movq $0x20000004, %rax
movq $1, %rdi
leaq syscall_str(%rip), %rsi
movq $25, %rdx
syscall
jc error
xorq %rax, %rax
leave
ret
error:
movq $1, %rax
leave
ret
.section __DATA,__data
syscall_str:
.asciz "Printed with a syscall.\n"
There does not seem to be any error; there is simply nothing written to stdout
.
I know that start
is usually used as the starting point for an executable on MacOS, but it does not compile with GCC.
stdout
– Dekastart
as the entry point, it is: "Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)" – Dekamovq $0x20000004, %rax
should bemovq $0x2000004, %rax
(one less 0) – Balcony-nostdlib -nostartfiles
. – Soradtruss
to trace the system calls of the program. – Sora2 << 24
) and not by 28 which is what I was effectively doing. If you make your comment into an answer I would happily accept it. Thanks again – Deka