MisterHazza
MisterHazza

Reputation: 198

How do I run an assembly program on mac?

I have recently been teaching myself ARM assembly on a raspberry PI, and I am now teaching myself x86 assembly on a MacOS X using the NASM compiler. I have written a small program with the file name 'program1.s'. The code is as follows:

global start

section .text
start:
    mov rax, 0xA

Firstly, I'd like to ask, is this the correct syntax for NASM on MacOS X? But my main question is, what NASM command do I use to run this?

Any help on either of these questions would be greatly appreciated.

Upvotes: 2

Views: 6871

Answers (1)

dannyadam
dannyadam

Reputation: 4170

The following commands can be used to assemble, link, and then run an assembly code program on macOS.

$ nasm -fmacho64 program1.s         # assemble
$ ld -static program1.o -o program1 # link
$ ./program1                        # run

For the preceding to work, I added instructions for an exit system call (otherwise there is a segmentation fault when running, presumably from the subsequent data in memory being interpreted as instructions to execute).

global start

section .text
start:
    mov rax, 0xA
    mov rax, 0x02000001  ; System call for exit.
    mov rdi, 0x0         ; An exit code of 0.
    syscall

Upvotes: 4

Related Questions