//------------------------------------------------------------------- // courseid.cpp // // This program employs inline assembly language to make direct // system-calls to the Linux kernel, and so it does not need to // be linked to any routines in the standard C runtime library. // // We will use this program in illustrating some concepts about // 'memory-mapping' of files (using the UNIX 'mmap()' function) // and about linking and loading of a binary-format executable. // // compile using: $ gcc -c courseid.cpp // // Then link the object-file (using our special linker-script): // // $ ld courseid.o -T ldscript -o courseid.b // // We have designed a special application (named 'launch.cpp') // that can 'map' the executable file 'courseid.b' into memory // and then execute it by making an indirect C function-call. // // NOTE: In the 'asm' constructs which are used below: // -- the "i" constraint denotes an immediate operand // -- the "m" constraint denotes a memory operand // // Reference: Richard M. Stallman, "Using and Porting GNU CC," // Boston: The Free Software Foundation (1999), pages 337-338. // // programmer: ALLAN CRUSE // written on: 19 OCT 2004 // revised on: 20 OCT 2004 //------------------------------------------------------------------- #define sys_write 4 // system-call ID-number #define dev_stdout 1 // device-file ID-number int main( void ) { char msg[] = "\nComputer Science 326: Operating Systems\n\n"; int len = sizeof( msg ) - 1; asm(" movl %0, %%eax " : : "i" (sys_write) ); asm(" movl %0, %%ebx " : : "i" (dev_stdout) ); asm(" leal %0, %%ecx " : : "m" (msg[0]) ); asm(" movl %0, %%edx " : : "m" (len) ); asm(" int $0x80 "); }