0

My goal is to use Xcode (10.2-beta) to write C and Assembly for MacOS (14.1); I want to use the NASM instead of the default GNU compiler, the GAS syntax is quite horrible.

BTW, although I configured Xcode to use NASM, as you will see below, I suspect it is not using it!

I've built my project using the GNU compiler. It runs fine.

// main.c
# include <stdio.h>

int myOperation(int a, int b);

int main(int argc, char * argv[]) {
    // insert logic here
    int x = 10;
    int y = 12;
    int z = myOperation(x, y);
    printf("The sum of %d and %d is %d\n", x, y, z);
    return 0;
}

//  assembly.s
.text
.globl _myOperation

_myOperation:
    add %esi, %edi
    mov %edi, %eax
    ret

I installed the XCode command line tools and changed the build rules to compile NASM files. The C file is the same:

//  assembly.s
.text
.globl _myOperation

_myOperation:
    add edi, esi
    mov eax, edi
    ret

Building rules Xcode build rule

I get the following errors:

/Users/rodrigomattososilveira/projects/asm/NASM Tutorial/Tutorial_01AA/Tutorial_01AA/assembly.s:12:5: error: unknown use of instruction mnemonic without a size suffix
    add edi, esi
    ^
/Users/rodrigomattososilveira/projects/asm/NASM Tutorial/Tutorial_01AA/Tutorial_01AA/assembly.s:13:5: error: unknown use of instruction mnemonic without a size suffix
    mov eax, edi
    ^
Command CompileC failed with a nonzero exit code

Some of the links I've checked out:

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rodrigo Silveira
  • 141
  • 2
  • 15

1 Answers1

1

I found a hint at this Reddit posting:

Looks like gas can take Intel syntax, but you have to specify it with the .intel_syntax flag.

Now, this code compiles:

.intel_syntax // THIS DID THE TRICK

.text
.global _myOperation

_myOperation:
    add edi, esi
    mov eax, edi
    ret
Rodrigo Silveira
  • 141
  • 2
  • 15
  • That's still not NASM syntax; note the `.text` directive, and `.global` instead of `global`. More importantly, `mov eax, foobar` is a load from `[foobar]` in GAS's `.intel-syntax`, unlike NASM where it's a `mov reg, imm32`. GNU `.intel_syntax` is MASM-like. See https://stackoverflow.com/tags/intel-syntax/info – Peter Cordes Feb 17 '19 at 21:54
  • Correct, I jumped the gun. Somehow Xcode seems to have abandoned NASM, so I'll have to abandon attempting to write NASM using Xcode – Rodrigo Silveira Feb 17 '19 at 22:57
  • I'd assume you can configure Xcode with custom build rules to manually tell it how to run NASM, even if you're right that it doesn't come with a built-in NASM mode anymore. But actually, avoiding NASM for OS X would probably be a good idea; there have been multiple serious bugs in NASM's `macho64` support over the years. [Successive sys\_write syscalls not working as expected, NASM bug on OS X?](//stackoverflow.com/q/49224997) [Mach-O 64-bit format does not support 32-bit absolute addresses. NASM Accessing Array](//stackoverflow.com/q/47300844) – Peter Cordes Feb 17 '19 at 23:07
  • @PeterCodes, wise suggestion regarding not using NASM. Is there anything other than GAS you would recommend – Rodrigo Silveira Feb 24 '19 at 16:41
  • YASM is nasm-compatible. I don't know how good its OS X support is, and it's not well maintained (still lacking AVX512 support), but it's otherwise very good on Linux. – Peter Cordes Feb 24 '19 at 18:26