0

working on a school project, I'm trying to figure out how to compile a simple c program with assembly code on Xcode running the latest Xcode update (12.0.1). We are supposed to use AT&T syntax witch is what I found the syntax to be used in Xcode.

Here is the c code:

#include <stdio.h>
extern int MaxOfThree(int, int, int);
int main()
{
    printf("The largest value among 1, -4 och -7 is %d\n", MaxOfThree(1,-4,-7));
    return 0;
}

Assembly code:

.text
.global MaxOfThree 
MaxOfThree:
    cmpl %esi, %edi
    cmovl %esi, %edi
    cmpl %edx, %edi
    cmovl %edx, %edi
    movl %edi, %eax
ret

The error I'm getting is:

error: unexpected token in directive
.global MaxOfThree MaxOfThree:
                   ^

I also got another error with another program saying absolute addressing is not allowed with x86_64 syntax. what is wrong?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    It seems like the file you're trying to assemble has improper line endings for the platform you're using. – Carcigenicate Sep 27 '20 at 16:13
  • Do you have any suggestions on how that can be fixed? – Adam Alrefai Sep 27 '20 at 16:23
  • Some editors like Notepad++ have options to fix things like that; if that's what the issue is. I guessed that that was the problem because it doesn't seem to recognize the newline. – Carcigenicate Sep 27 '20 at 16:24
  • 1
    Use `hexdump -C foo.S` to see the actual line endings. Probably the assemblers expects Unix line endings (`0x10` LF), not classic Mac `0x13` CR or whatever you might have. Use an editor that can make Unix files. re: absolute addressing: [Mach-O 64-bit format does not support 32-bit absolute addresses](https://stackoverflow.com/q/47300844) explains the problem, but uses NASM syntax. I'm not seeing a good macos AT&T syntax Q&A about that, but the AT&T syntax is `lea symbol(%rip), %rsi` or whatever. [How to load address of function or label into register](//stackoverflow.com/q/57212012) – Peter Cordes Sep 27 '20 at 17:04
  • 1
    In Xcode "Preferences...", at the bottom of the "Text Editing" tab, there are settings to trim whitespace, and set the line endings. There's also an option to "Convert existing files on save". After choosing the correct settings, you should be able to fix the file by opening it with Xcode, making a trivial edit, and then closing the file. – user3386109 Sep 27 '20 at 17:05

0 Answers0