//------------------------------------------------------------------- // useasm.cpp // // This program illustrates the use of GCC's 'asm' construct to // insert inline assembly language statements into your program // (in cases where you need to reference your local variables). // // asm( instruction-template // : output-operand // : input-operand // : clobber-list ); // // To see the assembly-language generated by the asm construct, // you can use this command-sequence: // // $ gcc -S useasm.cpp // $ cat useasm.s // // programmer: ALLAN CRUSE // written on: 18 OCT 2004 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), write(), close() int c = 10; // static variable (global) int main( int argc, char **argv ) { int x = 25; // automatic variable (local) int y; // automatic variable (local) // using several single-instruction constructs asm(" movl %0, %%eax " : : "m" (x) ); asm(" mull c " : : : "edx" ); asm(" shl $1, %eax "); asm(" movl %%eax, %0 " : "=m" (y) ); printf( "x=%d y=%d \n", x, y ); // using just one multi-instruction construct asm(" movl %1, %%eax \n \ mull c \n \ shl $1, %%eax \n \ movl %%eax, %0 " : "=m" (y) : "m" (x) : "edx" ); printf( "x=%d y=%d \n", x, y ); }