Leahcim
Leahcim

Reputation: 41929

Use NASM to compile 32-bit assembly for x86_64 environment

I'm reading a book where all the assembly code examples are written for 32-bit Linux environment, and I'm using a 64-bit Mac. I was able to compile the following program with NASM after changing _start to start. However, when I run the executable it doesn't print hello world as I would expect it to. Is there an option to pass to NASM to compile this in a way that will run on a 64-bit Mac?

I tried:

nasm -f macho32 helloworld.asm

and

nasm -f macho helloworld.asm

followed by:

ld helloworld.o -o helloworld

My code is:

section .data       ; data segment
msg     db      "Hello, world!", 0x0a   ; the string and newline char

section .text       ; text segment
global start       ; Default entry point for ELF linking

start:
  ; SYSCALL: write(1, msg, 14) 
  mov eax, 4        ; put 4 into eax, since write is syscall #4
  mov ebx, 1        ; put 1 into ebx, since stdout is 1
  mov ecx, msg      ; put the address of the string into ecx
  mov edx, 14       ; put 14 into edx, since our string is 14 bytes
  int 0x80          ; Call the kernel to make the system call happen

  ; SYSCALL: exit(0)
  mov eax, 1        ; put 1 into eax, since exit is syscall #1
  mov ebx, 0        ; exit with success
  int 0x80          ; do the syscall

Upvotes: 3

Views: 2891

Answers (1)

Joshua
Joshua

Reputation: 43278

It's simply not going to work like that. Get a VM and install 32 bit linux in the VM. The problem is not running x86_32 code on x64. The problem is trying the Linux syscall gate on MAC. There's no reason to believe that would work.

Upvotes: 3

Related Questions