Inline Assembly
Inline Assembly
• 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.
• Inline Assembly language appears in two flavors:
• Intel Style &
• AT&T style.
Basic Inline Code
• asm("assembly code");
• or
• __asm__ ("assembly code");
#include <stdio.h>
int gcd( int a, int b ) { /* Compute Greatest Common Divisor using Euclid's Algorithm */
int result ;
/ __asm__ __volatile__ (
"movl %1, %%eax;"
"movl %2, %%ebx;"
"CONTD: cmpl $0, %%ebx;"
"je DONE;"
"xorl %%edx, %%edx;"
"idivl %%ebx;"
"movl %%ebx, %%eax;"
"movl %%edx, %%ebx;"
"jmp CONTD;"
"DONE: movl %%eax, %0;" : "=g" (result) : "g" (a), "g" (b)
);
return result ;
}
int fact(int n) { /* find factorial of integer */
int res ;
__asm__ __volatile__ (
"movl %1, %%ecx;"
"xorl %%edx, %%edx;"
"movl $1, %%eax;"
"cmpl $0, %%ecx;"
"jg factloop;"
"jmp factDONE;"
"factloop: xorl %%edx, %%edx;"
"imull %%ecx;"
"loopnz factloop;"
"factDONE: movl %%eax, %0;" : "=g" (res) : "g" (n)
);
return res ;
}
int main() {
int first, second ;
printf( "Enter two integers : " ) ;
scanf( "%d%d", &first, &second );
printf( "GCD of %d & %d is %d\n %d!= %d\n", first, second, gcd(first, second), first, fact(first));
return 0 ;
}
#include <stdio.h>
int a, b, addv, subv, mulv, quo, rem;
int main(void) {
printf("Please enter two integers? ");
scanf("%d%d", &a, &b); printf("%d + %d = %d\n", a, b, addv);
asm (".intel_syntax noprefix"); printf("%d - %d = %d\n", a, b, subv);
asm ("mov eax, [a]"); printf("%d * %d = %d\n", a, b, mulv);
asm ("add eax, [b]"); printf("%d / %d = %d\n", a, b, quo);
asm ("mov [addv], eax"); printf("%d %% %d = %d\n", a, b, rem);
asm ("mov eax, [a]"); return (0);
asm ("sub eax, [b]"); }
asm ("mov [subv], eax");
asm ("mov eax, [a]");
asm ("xor edx, edx");
asm ("mov ebx, [b]");
asm ("imul ebx");
asm ("mov [mulv], eax");
asm ("mov eax, [a]");
asm ("mov ebx, [b]");
asm ("xor edx, edx");
asm ("idiv ebx");
asm ("mov [quo], eax");
asm ("mov [rem], edx");
asm (".att_syntax noprefix");
// Quadratic Equation - Intel Inline Assembly
#include <stdio.h>
int a, b, c, d;
int main(void) {
printf("Please enter the Coefficients a, b and c of the Quadratic Equation ax^2 + bx + c = 0 ? ");
scanf("%d%d%d", &a, &b, &c);
asm (".intel_syntax noprefix");
asm ("mov eax, [a]");
asm ("add eax, eax");
asm ("add eax, eax");
asm ("xor edx, edx");
asm ("mov ebx, [c]");
asm ("imul ebx"); if (d > 0)
asm ("mov [d], eax"); printf("Equation has Real Roots\n");
else if (d < 0)
asm ("mov eax, [b]");
printf("Equation has Complex Roots\n");
asm ("mov ebx, eax");
asm ("xor edx, edx"); else
asm ("imul ebx"); printf("Equation has Real Equal Roots");
asm ("mov ebx, [d]"); return (0);
asm ("sub eax, ebx"); }
asm ("mov [d], eax");
asm (".att_syntax noprefix");
int a, b, c, d;
int main(void) {
printf("Please enter the Coefficients a, b and c of the Quadratic Equation ax^2 + bx + c = 0 ? ");
scanf("%d%d%d", &a, &b, &c);
// AT&T Inline Assembly V2
asm (
"movl (a), %eax;"
"addl %eax, %eax;"
"addl %eax, %eax;"
"xorl %edx, %edx;"
"movl (c), %ebx;"
"imull %ebx;"
"movl %eax, (d);" if (d > 0)
"movl (b), %eax;" printf("Equation has Real Roots\n");
"movl %eax, %ebx;" else if (d < 0)
"xorl %edx, %edx;" printf("Equation has Complex Roots\n");
"imull %ebx;" else
"movl (d), %ebx;" printf("Equation has Real Equal Roots");
"subl %ebx, %eax;" return (0);
}
"movl %eax,(d);"
);