Click here to Skip to main content
12,354,630 members (61,361 online)
Click here to Skip to main content

Stats

404.2K views
2.3K downloads
65 bookmarked
Posted

Using Inline Assembly in C/C++

, 14 Oct 2006 CPOL
This article describes using inline assembly code in your C/C++ program... was it ever difficult for you, it will never be any more!
#include <stdio.h>

int main() {

	int arg1, arg2, add, sub, mul, quo, rem ;

	printf( "Enter two integer numbers : " );
	scanf( "%d%d", &arg1, &arg2 );

	__asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
	__asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );
	__asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );

	__asm__ ( "movl $0x0, %%edx;"
	          "movl %2, %%eax;"
	          "movl %3, %%ebx;"
	 		  "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );

	printf( "%d + %d = %d\n", arg1, arg2, add );
	printf( "%d - %d = %d\n", arg1, arg2, sub );
	printf( "%d * %d = %d\n", arg1, arg2, mul );
	printf( "%d / %d = %d\n", arg1, arg2, quo );
	printf( "%d %% %d = %d\n", arg1, arg2, rem );

	return 0 ;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jain.pk
India India
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 15 Oct 2006
Article Copyright 2006 by jain.pk
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid