Click here to Skip to main content
11,429,382 members (65,156 online)
Click here to Skip to main content
Add your own
alternative version

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!
edujini_inline_asm_src.zip
#include <stdio.h>

float sinx( float degree ) {
	float result, two_right_angles = 180.0f ;
	__asm__ __volatile__ ( "fld %1;"
							"fld %2;"
							"fldpi;"
							"fmul;"
							"fdiv;"
							"fsin;"
							"fstp %0;" : "=g" (result) : "g"(two_right_angles), "g" (degree)
	) ;
	return result ;
}

float cosx( float degree ) {
	float result, two_right_angles = 180.0f, radians ;
	__asm__ __volatile__ ( "fld %1;"
							"fld %2;"
							"fldpi;"
							"fmul;"
							"fdiv;"
							"fstp %0;" : "=g" (radians) : "g"(two_right_angles), "g" (degree)
	) ;
	__asm__ __volatile__ ( "fld %1;"
							"fcos;"
							"fstp %0;" : "=g" (result) : "g" (radians)
	) ;
	return result ;
}

float square_root( float val ) {
	float result ;
	__asm__ __volatile__ ( "fld %1;"
							"fsqrt;"
							"fstp %0;" : "=g" (result) : "g" (val)
	) ;
	return result ;
}

int main() {
	float theta ;
	printf( "Enter theta in degrees : " ) ;
	scanf( "%f", &theta ) ;

	printf( "sinx(%f) = %f\n", theta, sinx( theta ) );
	printf( "cosx(%f) = %f\n", theta, cosx( theta ) );
	printf( "square_root(%f) = %f\n", theta, square_root( theta ) ) ;

	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

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