Inline Asm
Notes on writing inline assembly in C/C++ with GCC and Clang: AT&T (GAS) syntax vs Intel syntax, addressing modes and extended asm.
In this post I wish to give some information about inline assembly.
What is Assembly?
In short, Assembly is the human-readable form of the CPU’s machine language. Each instruction you write is translated more or less directly into one processor instruction.
Why are we using it?
Writing a complete application in ASM is really hard. But unlike compiled languages like C or C++, it lets you send commands directly to the processor.
Writing short pieces of ASM code brings you some fantastic capabilities, such as:
- You can write your own low-level hardware interface for an operating system.
- You can use special CPU instructions which the compiler doesn’t generate for you.
- You can use MMX, SSE or AVX registers for high-speed computing (SIMD).
What is GAS syntax?
There are two common ASM syntaxes, AT&T and Intel. If you know how to write your code in MASM, NASM or even FASM, you are familiar with Intel syntax. Visual Studio on Windows shows disassembly in Intel syntax and its compiler accepts Intel syntax in inline asm. On the other side, you have to learn AT&T syntax, which is the default of GAS, the famous GNU assembler.
GCC (g++) and Clang understand this GAS syntax in the asm() statement. The simple differences are:
- Names of the registers start with the % character. Basically
eaxin NASM,%eaxin GAS. - The operand order is reversed in GAS. For example
mov eax, edx(move the value of the edx register into eax) in Intel syntax ismovl %edx, %eaxin GAS. - Instructions take a size suffix in AT&T notation. b: byte 8 bit, w: word 16 bit, l: long 32 bit, q: quad 64 bit, t: ten bytes 80 bit (x87).
- When one of the operands is a register, GAS can figure out the size by itself and the suffix is optional. But using suffixes is still the best option.
- Immediate operands are marked with a $ prefix, as in
addl $5, %eax(add the immediate long value 5 to register %eax). movl $foo, %eaxloads the address of foo into eax. In Intel syntax this ismov eax, OFFSET fooin MASM (ormov eax, fooin NASM).movl foo, %eaxloads the value of foo into eax. In Intel syntax this ismov eax, DWORD PTR [foo].
AT&T has a little bit complicated addressing syntax with registers. Here is an example of it:
movl 3(%ebp, %edx, 2), %eax
It calculates (3 + ebp + (edx * 2)) as a memory address and moves the value at that address into the eax register. The general form is displacement(base, index, scale).
movl -8(%ebp, %ebx, 4), %eax
# address is -8 + ebp + (4 * ebx)
movl (%ebp, %ebx, 4), %eax
# address is ebp + (4 * ebx)
movl -8(%ebp), %eax
# address is ebp - 8
movl (%ebp), %eax
# address is ebp
movl (%ebp, %ebx), %eax
# address is ebp + ebx
movl -8(, %ebx, 4), %eax
# address is (4 * ebx) - 8

Intel x86 registers
If you really don’t like AT&T, GCC can also work with Intel syntax. Compile with -masm=intel, or switch inside the block with .intel_syntax noprefix and switch back with .att_syntax prefix at the end.
How can I write it inline?
To write a basic asm code in your C/C++ environment you only need the asm directive. But keep in mind you have to add new lines for every instruction.
#include <cstdlib>
int main(int argc, char const *argv[])
{
asm("movl %ebx, %eax \n\t" // using \n\t to add a new line
"movl %edx, %eax"); // it allows you to add multiple lines
return EXIT_SUCCESS;
}
This is only a demo. It changes eax behind the compiler’s back, and basic asm has no way to tell the compiler about that. For real code use the extended version below.
You can use multiple lines of assembly in the asm directive, but keep these rules in your mind:
- Protecting data and managing the flow of the code is your responsibility. Changing segment registers or writing to a wrong address may crash your program, even though modern operating systems use a flat memory model.
- Keep the blocks short. The compiler can’t optimize around your asm block and long blocks are hard to maintain.
- You have to provide new line characters (generally
\n\t) where necessary. On x86 GAS also accepts;as a separator. - In Visual Studio (MSVC) you write
__asm { ... }blocks in Intel syntax, and only for 32-bit x86. MSVC doesn’t support inline asm on x64 at all, so there you have to use intrinsics. On GCC and Clangasmand__asm__are fine (__asm__also works in strict C modes).
There is also an extended version of the asm command.
#include <cstdlib>
#include <iostream>
int main(int argc, char const *argv[])
{
int inp{11};
int out{12};
std::cout << "inp is " << inp << "\n";
std::cout << "out is " << out << "\n";
__asm__("movl %1, %0;"
: "=r" (out) // out is now %0
: "r" (inp)); // inp is now %1
std::cout << "inp is " << inp << "\n";
std::cout << "out is " << out << "\n";
return EXIT_SUCCESS;
}
The extended version uses variables as parameters. The form is asm(template : outputs : inputs : clobbers). "=r" means “write it to a register”, "r" means “read it from a register”, and the compiler picks the registers for you. In the clobber list you tell the compiler which registers or memory your code changes, for example : "eax", "memory". Note that the registers in extended asm are written with %%, like %%eax, because a single % is used for the operands.
If your asm block has no outputs, or the compiler thinks the outputs are not used, it may remove the block or move it out of a loop. To prevent this, add the __volatile__, __volatile or volatile keyword after the __asm__, __asm or asm directive.
Update (2026): For SIMD work, intrinsics are a much better choice than inline asm today. They work on GCC, Clang and MSVC, and the compiler can still optimize the code around them. See my SIMD post.