Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comment syntax ambiguity under GAS ARM target

The GNU GAS documentation appears to fail to properly define certain syntax features for the ARM target, so I'm having difficulty writing even simple assembly.

Two such examples:

  • The ARM Compiler Migration and Compatibility Guide Version 6.02 states that, as well as multi-line comments, under GNU syntax:

    The // marker identifies the remainder of a line as a comment:

    MOV R0,#16 // Load R0 with 16

    which appears to conflict with the Using as guide, which states:

    The presence of a ‘@’ anywhere on a line indicates the start of a comment that extends to the end of that line.

  • The issue of implicit shifts in instructions is only addressed in passing in the Arm documentation, and only gives the failing case:

    GNU syntax assembly expects immediate values to be presented as encoded. The instruction MOVK x1, #0x40000 results in the following message: error: immediate must be an integer in range [0, 65535].

    What exactly is meant by "encoded"? Am I to assume that the LSL syntax is supported identically under GNU? It doesn't appear to be addressed in GAS documentation.

Is there somewhere I can find concrete and complete syntax definition for GAS + ARM target? Thanks in advance.

like image 601
Benjamin Crawford Ctrl-Alt-Tut Avatar asked Oct 17 '25 18:10

Benjamin Crawford Ctrl-Alt-Tut


1 Answers

GAS supports both // comments and @ comments itself, even without using the C preprocessor to handle C/C++-style comments.

(Call your file foo.S with a capital S and gcc -c foo.S will run it through CPP first, so you can use #define and #if 0 and stuff like that.)

"Encoded" means representable in machine code. e.g. MOVK x1, #0x40000 isn't a syntax error, it's an error at a later stage than parsing because there's no way to represent it that specific number in machine code with that opcode. But mov x1, #0x40000 is fine because the assembler can pick a bit-range encoding for orr-immediate with xzr. Note that that's AArch64, which uses different machine code than ARM.

GAS ARM syntax is the same as what official ARM manuals document. Directives are different than Keil's ARMasm, but the syntax of instruction lines is (mostly?) identical. @old_timer says there are or were some differences with mrc and mcr syntax, but for the most part GAS aims to be compatible with standard ARM syntax.

See the GAS manual's ARM Dependent Features chapter, including a section on Syntax.

(Note that ARM has a couple different syntax modes, "unified" vs. the older one that used different syntax for ARM vs. Thumb mode. Usually you want .syntax unified)

You can always look at GCC C compiler output for examples (e.g. on https://godbolt.org/z/Tk_jrD), although for a specific case you'd have to write a C function where GCC will use the instruction or addressing mode you're interested in. GCC uses GAS directives.

like image 85
Peter Cordes Avatar answered Oct 21 '25 04:10

Peter Cordes