As part of a class, I need to write assembly code with specific format. The teacher refers to it as "Intel syntax x86-64". I tried to compile it with the given command (gcc -O3 -o op sc.s
) and despite working great on a linux PC it fails on a Mac. After some researches I found Mac OS doesn't support this syntax and following the advices from the teacher I tried to run Linux from a bootable USB key and on a Virtual Machine (using VirtualBox) but both fail due to keyboard compatibility issues. I searched a few online solution but none of them could compile the source code without problem. How can I run it without buying a new computer ? I would like to run this exact syntax if it's possible.
Thank you for reading me !
Here a exemple of code he gave us :
.intel_syntax noprefix
.data
msg: .asciz "Hello, world!\n"
.text
.global main
.type main, @function
main: PUSH RBP
MOV RBP, RSP
MOV RDI, offset flat:msg
CALL printf
MOV EAX, 0
POP RBP
RET
With can be compiled with gcc (shown in live) yet compiling it with gcc -o hw1 hw.s
or gcc -o hw1 -masm=intel hw.s
give me the following error :
hw.s:6:9: error: unknown directive
.type main, @function
^
hw.s:9:29: error: unknown token in expression
MOV RDI, offset flat:msg
Edit: Setting up an ssh server on the VM and connecting to the VM via the host terminal made things much easier with VirtualBox, see https://stackoverflow.com/a/10532299/5770818
I don't think there's a good solution on macOS. As far as I know, GNU binutils don't support the Mach binary format on macOS, which means you can't just use the same assembler as on Linux (gas).
The issue isn't only the syntax, in fact, the syntax as such is partially supported. You will also run into other platform-related differences. For example, the ".type" directive wouldn't be used on macOS, and symbol names are prefixed with an underscore.
If you have keyboard issues with VMs, I'd recommend setting up some server Linux distribution in the VM and then run it in headless mode. Access it by logging in with ssh. This way you interact with it through Terminal, and shouldn't have keyboard problems.
Docker might also be an option, since it actually runs Linux in a VM on macOS, but might be more work to figure out.
Anyway, if you're interested in a version of the code that works on macOS:
.intel_syntax noprefix
.data
msg: .asciz "Hello, world!\n"
.text
.global _main
_main: PUSH RBP
MOV RBP, RSP
LEA RDI, [RIP + msg]
CALL _printf
MOV EAX, 0
POP RBP
RET
I removed the ".type" line, added underscores to main and printf, and changed "MOV RDI, offset flat:msg" to "LEA RDI, [RIP + msg]".
Build with "clang -o hw1 hw1.s", no reason to pretend we're running gcc ;)