I came up with this, in which I have to call atoi() from C:
Code: Select all
;RDI, RSI, RDX, RCX, R8, R9, XMM0–7
%define SYS_EXIT 1
%define SYS_READ 3
%define SYS_WRITE 4
%define STDIN 0
%define STDOUT 1
%define STDERR 2
section .bss
inputnum resb 1
sum resb 2
section .data
prompt db 'Input some ****: '
section .text
extern _atoi
global _start
_exit:
mov rax, SYS_EXIT ; void sys_exit() from FreeBSD
mov rdi, 0
syscall
_start:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, prompt
mov rdx, 20 ; how many bytes
syscall
mov rax, 3 ; SYS_READ
mov rdi, 0 ; STDIN
mov rsi, inputnum
mov rdx, 1 ; how many bytes
syscall
push rsi
call _atoi ; after this the answer should be in rax
mov r10, rax
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, r10
mov rdx, 1
syscall
call _exit
NASM doesn't have a problem with this, but ld does. And I'm not sure what I have to do. Obviously I have to tell ld to "include" libc somehow, but how?
This is what I use for assembling and linking:
nasm -f elf64 helloworld.asm
ld -o hello helloworld.o