๐งพ What is Inline Assembly?
Inline Assembly (or Embedded Assembly) is a way to embed low-level Assembly language instructions directly inside a C program.
It gives you direct access to the CPU, allowing you to:
Optimize performance-critical parts
Access hardware-specific features
Use processor instructions not available in C
๐ง Why Use Inline Assembly?
๐ Need | ๐ก Why Inline Assembly Helps |
|---|---|
Maximum speed | Fine-tuned control over CPU instructions |
Direct hardware access | Communicate with hardware at register level |
Use of special CPU instructions | SIMD, SSE, AVX instructions not available in C |
Embedded Systems / OS development | Critical low-level boot/load/interrupt operations |
โ ๏ธ When Not to Use Inline Assembly
General application programming (C is already portable and efficient)
When compiler optimization is more than enough
If your goal is portability across processors/platforms
๐ Inline assembly breaks portability โ code may work only on specific compilers (e.g., GCC) or processors (e.g., x86).
๐ง Syntax of Inline Assembly in GCC
__asm__("assembly_instruction");or
asm("assembly_instruction");โ๏ธ Both __asm__ and asm are GNU extensions.
๐ Example 1: Simple NOP (No Operation)
#include <stdio.h>
int main() {
printf("Before NOP\\n");
asm("nop"); // Do nothing
printf("After NOP\\n");
return 0;
}๐ง nop is a processor instruction that literally does nothing for one CPU cycle โ useful in delay loops, debugging, etc.
๐ Example 2: Move a Value into a Register
int val;
asm("movl $42, %0" : "=r"(val));
printf("Value is: %d\\n", val); // Output: 42๐ง Explanation:
movl $42, %0โ move 42 into the variable mapped to register%0=r(val)โ output operand stored in register
๐งฑ GCC Extended Syntax (More Powerful)
__asm__ (
"assembly_code"
: output_operands
: input_operands
: clobbered_registers
);โ Meaning of Components
Component | Description |
|---|---|
| Your actual assembly instructions |
| Where to store results (using |
| Inputs passed from C variables to registers |
| Registers modified or affected by this code |
๐ Example 3: Add Two Numbers Using Assembly
int a = 10, b = 20, result;
__asm__ (
"addl %%ebx, %%eax;"
: "=a"(result)
: "a"(a), "b"(b)
);
printf("Sum = %d\\n", result); // Output: 30๐ง Explanation:
"addl %%ebx, %%eax;"โ Addb(in EBX) toa(in EAX)=a(result)โ store result from EAX into C variable"a"(a), "b"(b)โ assign a and b to registers
๐ Use Cases in Real Life
Field | Use Case |
|---|---|
Embedded Systems | Bit-level manipulation, device driver communication |
Cryptography | Optimized encryption routines (e.g., bitwise ops) |
Operating Systems | Context switching, interrupt handling |
Game Engines | Math-heavy routines (matrix ops, SIMD) |
๐ก Inline Assembly vs External Assembly
Feature | Inline Assembly | External Assembly File ( |
|---|---|---|
Portability | Lower | Lower |
Flexibility | Medium | High |
Debugging Ease | Medium | Low |
Performance | High (when done right) | Highest |
Code Integration | Tight with C | Separate file linkage needed |
โ ๏ธ Things to Keep in Mind
Inline Assembly is architecture-dependent (mostly x86 or ARM).
Itโs compiler-specific (mainly supported in GCC or Clang).
Mistakes can lead to crashes or undefined behavior.
๐ Inline Assembly is Useful If Youโre:
Working in Embedded C, OS Dev, or Firmware
Preparing for low-level systems interviews
Writing performance-critical algorithms
Learning how CPUs work at a deeper level