This article describes using inline assembly code in your C/C++ program... was it ever difficult for you, will never be any more!
Introduction
First of all, what does term "inline" mean?
Generally the inline term is used to instruct the compiler to insert the code of a function
into the code of its caller at the point where the actual call is made. Such functions are
called "inline functions". The benefit of inlining is that it reduces function-call overhead.
Now, it's easier to guess about inline assembly. It is just a set of assembly instructons
writen as inline functions. Inline assembly is used for speed, and you ought to believe me that
it is frequently used in system programming.
We can mix the assembly statements within C/C++ programs using keyword "asm".
Inline assembly is important because of its ability to operate and make its output visible
on C/C++ variables.
GCC Inline Assembly Syntax
Assembly language appears in two flavors : Intel Style & AT&T style.
GNU C compiler i.e. GCC uses AT&T syntax and this is what we would use.
Let us look at some of the major differences of this style as against the Intel Style.
If you are wondering how you can use GCC on Windows, you can just download Cygwin from
www.cygwin.com.
- Register Naming :
Register names are prefixed with %, so that registers are
%eax, %cl etc,
instead of just eax, cl.
- Ordering of operands :
Unlike to Intel convention (first operand is destination),
the order of operands is source(s) first, and destination last.
For example, Intel syntax "
mov eax, edx" will look like
"mov %edx, %eax" in AT&T assembly.
- Operand Size :
In AT&T syntax the size of memory operands is determined from the
last character of the op-code name. The suffix is
b for (8-bit) byte,
w for (16-bit) word, and l for (32-bit) long.
For example, the correct syntax for the above instruction would have been
"movl %edx, %eax".
- Immediate Operand :
Immediate operands are marked with a
$ prefix, as in
"addl $5, %eax", which means add immediate long value 5 to register %eax).
- Memory Operands :
Missing operand prefix indicates it is a memory-address; hence
"
movl $bar, %ebx" puts the address of variable bar into register
%ebx, but "movl bar, %ebx" puts the contents of variable
bar into register %ebx.
- Indexing :
Indexing or indirection is done by enclosing the index register or indirection memory cell
address in parentheses. For example, "
movl 8(%ebp), %eax"
(moves the contents at offset 8 from the cell pointed to by %ebp into register %eax).
Basic Inline Code
We can use either of the following formats for basic inline assembly.
asm("assembly code");
or
__asm__ ("assembly code");
Example:
asm("movl %ebx, %eax"); /* moves the contents of ebx register to eax */
__asm__("movb %ch, (%ebx)"); /* moves the byte from ch to the memory pointed by ebx */
Just in case if we have more than one assembly instructions,
use semicolon at the end of each instruction.
Please refer to the example available in basic_arithmetic.c in downloads.
Compile the program using "-g" option of GNU C compiler "gcc" to keep debugging
information with the executable and then using GNU Debugger "gdb" to inspect the contents of CPU registers.
Extended Assembly
In extended assembly, we can also specify the operands.
It allows us to specify the input registers, output registers and a list of clobbered registers.
asm ( "assembly code"
: output operands /* optional */
: input operands /* optional */
: list of clobbered registers /* optional */
);
If there are no output operands but there are input operands,
we must place two consecutive colons surrounding the place where
the output operands would go.
It is not mandatory to specify the list of clobbered registers to use,
we can leave that to GCC and GCC’s optimization scheme do the needful.
Example (1)
asm ("movl %%eax, %0;" : "=r" ( val ));
In this example, the variable "val" is kept in a register,
the value in register eax is copied onto that register,
and the value of "val" is updated into the memory from this register.
When the "r" constraint is specified, gcc may keep the variable
in any of the available General Purpose Registers.
We can also specify the register names directly by using specific register constraints.
The register constraints are as follows :
+---+--------------------+
| r | Register(s) |
+---+--------------------+
| a | %eax, %ax, %al |
| b | %ebx, %bx, %bl |
| c | %ecx, %cx, %cl |
| d | %edx, %dx, %dl |
| S | %esi, %si |
| D | %edi, %di |
+---+--------------------+
Example (2)
int no = 100, val ;
asm ("movl %1, %%ebx;"
"movl %%ebx, %0;"
: "=r" ( val ) /* output */
: "r" ( no ) /* input */
: "%ebx" /* clobbered register */
);
In the above example, "val" is the output operand, referred to by %0 and "no"
is the input operand, referred to by %1.
"r" is a constraint on the operands, which says to GCC to use any register for storing the operands.
Output operand constraint should have a constraint modifier "=" to specify the
output operand in write-only mode. There are two %’s prefixed to the register name,
which helps GCC to distinguish between the operands and registers. operands have a single % as prefix.
The clobbered register %ebx after the third colon informs the GCC that the value
of %ebx is to be modified inside "asm", so GCC won’t use this register to store any other value.
Example (3)
int arg1, arg2, add ;
__asm__ ( "addl %%ebx, %%eax;"
: "=a" (add)
: "a" (arg1), "b" (arg2) );
Here "add" is the output operand referred to by register eax.
And arg1 and arg2 are input operands referred to by registers eax and ebx respectively.
See the file arithmetic.c for extended inline assembly statements.
It performs simple arithmetic operations on integer operands and displays the result.
Volatile
If our assembly statement must execute where we put it,
(i.e. must not be moved out of a loop as an optimization),
put the keyword "volatile" or "__volatile__" after "asm" or "__asm__" and before the ()’s.
asm volatile ( "...;"
"...;" : ... );
or
__asm__ __volatile__ ( "...;"
"...;" : ... );
Refer to the example in gcd.c, which computes the Greatest Common Divisor
using well known Euclid's Algorithm ( honoured as first algorithm ).
Here are some more examples which uses FPU (Floating Point Unit) Instruction Set.
- Example program to perform simple floating point arithmetic is available in
float.c
- Example program to compute trigonometrical functions like sin and cos is available in
maths.c.
Summary
GCC uses AT&T style assembly statements and we can use asm keyword to specify basic as well as extended assembly instructions.
Using inline assembly can reduce the number of instructions required to be executed by the processor.
In our example of GCD, if we implement using inline assembly, the number of intructions required for calculation
would be much less
as compared to normal C code using Euclid's Algorithm.
Downloads
The code can be downloaded from the downloads section here.