源碼: f1.cjava
#include <stdio.h> #include <ucontext.h> #include <unistd.h> #include <sys/time.h> int main(int argc, const char *argv[]) { ucontext_t context; int count = 0; getcontext(&context); int value = 0; printf("Hello World! count = %d, value = %d\n", count++, value++); sleep(1); setcontext(&context); return 0; }
顯示完整編譯連接過程linux
user_00@(none):asm> gcc --verbose f1.c -m32 Using built-in specs. Target: x86_64-suse-linux Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.1.2 --enable-ssp --disable-libssp --disable-libgcj --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new --program-suffix= --enable-version-specific-runtime-libs --without-system-libunwind --with-cpu=generic --host=x86_64-suse-linux Thread model: posix gcc version 4.1.2 20070115 (prerelease) (SUSE Linux) /usr/lib64/gcc/x86_64-suse-linux/4.1.2/cc1 -quiet -v f1.c -quiet -dumpbase f1.c -m32 -m32 -mtune=generic -auxbase f1 -version -o /tmp/cckOI3iI.s #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/lib64/gcc/x86_64-suse-linux/4.1.2/include /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/include /usr/include End of search list. GNU C version 4.1.2 20070115 (prerelease) (SUSE Linux) (x86_64-suse-linux) compiled by GNU C version 4.1.2 20070115 (prerelease) (SUSE Linux). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 3417495ce02f6bde0aa6fd5f0bddeaf6 /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/bin/as -V -Qy --32 -o /tmp/ccAJKu3f.o /tmp/cckOI3iI.s GNU assembler version 2.16.91.0.5 (x86_64-suse-linux) using BFD version 2.16.91.0.5 20051219 (SUSE Linux) /usr/lib64/gcc/x86_64-suse-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../lib/crt1.o /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../lib/crti.o /usr/lib64/gcc/x86_64-suse-linux/4.1.2/32/crtbegin.o -L/usr/lib64/gcc/x86_64-suse-linux/4.1.2/32 -L/usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/lib/../lib -L/usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib64/gcc/x86_64-suse-linux/4.1.2 -L/usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/lib -L/usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../.. /tmp/ccAJKu3f.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib64/gcc/x86_64-suse-linux/4.1.2/32/crtend.o /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../lib/crtn.o
個人 gcc 是 x86_64 4.1.2 版本c++
在 x86_64 gcc 機器上寫 i386 32bit 的彙編編譯與運行:bash
彙編代碼 hello.sui
.data msg : .string "Hello World!\n" len = . - msg .text .global _start _start: movl $len, %edx movl $msg, %ecx movl $1, %ebx movl $4, %eax int $0x80 movl $0, %ebx movl $1, %eax int $0x80
編譯與運行:code
user_00@(none):asm> as -V -Qy --32 -o hello.o hello.s GNU assembler version 2.16.91.0.5 (x86_64-suse-linux) using BFD version 2.16.91.0.5 20051219 (SUSE Linux) user_00@(none):asm> ld -m elf_i386 -s -o hello hello.o user_00@(none):asm> ./hello Hello World!