0

I am following this example. But I got this error. Does anybody know how to fix this problem? I am running on Mac OS X 10.14.1.

$ nasm -o hello_world.o hello_world.asm
hello_world.asm:8: error: instruction not supported in 16-bit mode
hello_world.asm:9: error: instruction not supported in 16-bit mode
hello_world.asm:10: error: instruction not supported in 16-bit mode
hello_world.asm:11: error: instruction not supported in 16-bit mode
hello_world.asm:15: error: instruction not supported in 16-bit mode
hello_world.asm:16: error: instruction not supported in 16-bit mode
$ nasm --version
NASM version 2.13.03 compiled on Feb  8 2018

How does C++ linking work in practice?

section .data
hello_world db "Hello world!", 10
section .text
global _start
_start:

; sys_write
mov rax, 1
mov rdi, 1
mov rsi, hello_world
mov rdx, 13
syscall

; sys_exit
mov rax, 60
mov rdi, 0
syscall
user1424739
  • 11,937
  • 17
  • 63
  • 152
  • Those are Linux system-call numbers. That won't work on OS X. – Peter Cordes Feb 23 '19 at 23:02
  • Try `-f macho64` to get nasm to generate a Mach-O object with 64 bit code. Still, this code won't work without changes due to it being written for Linux. – fuz Feb 23 '19 at 23:04
  • @Rietty The `BITS` directive changes the operation mode you assemble for, masking the error. It does not solve the underlying problem (wrong object file format) and is not needed to solve it. `BITS` is almost never the solution for this sort of issue. – fuz Feb 23 '19 at 23:05
  • 1
    I fixed [Ciro's answer](https://stackoverflow.com/questions/12122446/how-does-c-linking-work-in-practice/30507725#30507725) use `-felf64`, because the default `-fbin` doesn't work on Linux either. Maybe old versions of NASM defaulted to `-felf64` on Linux? Anyway, `-f macho64` is the output format you need on OS X for it to assemble + link, but then it will fail at runtime with the wrong call numbers. – Peter Cordes Feb 23 '19 at 23:09
  • How to make it work on Mac? – user1424739 Feb 23 '19 at 23:29
  • 1
    @user1424739 Fix the system call numbers. Check out `/usr/include/sys/syscall.h` for the system call numbers (I hope that's correct for macOS). Note that the error numbers are different, too. – fuz Feb 23 '19 at 23:32

1 Answers1

3

The problem is not in the code.

The complete command to compile is nasm -f elf64 -o hello.o hello.asm

You have to specify the format (elf64) to nasm compiler using the -f option.

Matthew
  • 1,905
  • 3
  • 19
  • 26
4s7r0n4u7
  • 56
  • 3
  • The answer to *this* question is actually `-f macho64`, because it's an OS X question. That will get it to assemble, at which point the OP will discover that OS X uses different system-call numbers than Linux. – Peter Cordes Jun 07 '19 at 15:08