1

How to programming amd64-based assembler with 64-bit FreeBSD?

like this i386-style code:

;hello world FreeBSD-i386 can run.

SYSCALL_EXIT  EQU 1
SYSCALL_WRITE EQU 4
STDOUT        EQU 1
section .data
    hello   db 'Hello, world!', 0Ah
    hbytes  equ $-hello

section .text
global  _start
_start:
    push dword hbytes
    push dword hello
    push dword STDOUT
    mov eax,SYSCALL_WRITE
    call kernel

.exit:
    push dword 0
    mov eax,SYSCALL_EXIT
    call kernel

kernel:
    int 80h
    ret

Here is how I run it:

sh:nasm -f elf hello.asm -o hello.o
sh:ld -melf_i386_fbsd -s -o hello hello.o
sh:./hello
hello world1

The result is correct

sh:nasm -f elf64 hello.asm -o hello64.o
sh:ld hello64.o -o hello64
sh:./hello64

tty stdout failed! no output!

fuz
  • 88,405
  • 25
  • 200
  • 352
ZiYi.Xiang
  • 11
  • 2
  • The duplicate I linked to refers to Linux, but FreeBSD uses the same ABI for system calls on amd64. Take the system call numbers out of `/usr/include/sys/syscall.h` – fuz Jan 05 '19 at 13:34
  • 1
    The exact reason why your code does not work is that nasm silently translates all your pushes into 64 bit pushes as 32 bit pushes are not available in 64 bit code. Also, since the return address is 8 bytes in 64 bit code, the stack layout doesn't fit anyway. It is generally a bad idea to do `int 80h` system calls in 64 bit code. – fuz Jan 05 '19 at 13:36
  • Thanks,for your help. (^_^) – ZiYi.Xiang Jan 05 '19 at 13:48
  • If you have further questions, feel free to ask. – fuz Jan 05 '19 at 13:51
  • My English is poor. It's not my native language. But I know what you mean. I'll go to your link to figure out the call convention. – ZiYi.Xiang Jan 05 '19 at 13:54

0 Answers0