user1343318
user1343318

Reputation: 2141

Does gcc support embedded assembler syntax?

William Hohl's ARM Assembly Language book talks about Embedded assembler syntax. An example of it can be found on here on ARM reference manual:

#include <stdio.h>
__asm void my_strcpy(const char *src, char *dst)
{
loop
      LDRB  r2, [r0], #1
      STRB  r2, [r1], #1
      CMP   r2, #0
      BNE   loop
      BX    lr
}

When I google 'gcc embedded assembler' syntax I only get the following results. Does gcc only support what it calls “extended assembly syntax?”

 asm [volatile] ( AssemblerTemplate
                  : OutputOperands
                  [ : InputOperands
                  [ : Clobbers ] ])

Upvotes: 3

Views: 511

Answers (1)

Damon
Damon

Reputation: 70206

No. GCC does not support embedded assembly functions like the one shown in your example. You would have to use e.g. a KEIL compiler for that.

The two forms of inline assembly that are supported by GCC are the "simple" (which is just one of the variants of the asm keyword followed by curly braces) and the much more useful "extended" form which allows you to map variables for input and output to registers or classes of registers in a very flexible and efficient way. You can write almost register-agnostic assembly that works and fully uses the compiler's capacity for low-level optimizations (it obviously cannot do high-level optimizations, that would be asked a bit too much!).
Unluckily, the syntax is a bit... unintuitive at first.

In order to get the same effect as in your embedded assembly my_strcpy function, you would have to write an ordinary function that contains an extended assembly block, and map the function's arguments to inputs (and don't forget to clobber memory).

Note that GCC inline assembly has a few additional quirks which may come as a surprise. For one, it uses AT&T notation, and then you have to explicitly add \n to every line to prevent the assembler from complaining (or \n\t if you intend to check intermediate files and care about readable output).
The first quirk (AT&T notation) is actually a benefit since it is a lot more intuitive than Intel notation, you only have to know about it in the first place. On the other hand, the need to explicitly add escaped newlines is... bleh. Luckily, you don't really need to use assembly often (if ever).

Upvotes: 1

Related Questions