asm volatile ("asm routine" : output : input : modify);
#define switch_to(prev,next,last) do {
asm volatile("pushl %%esi "
"pushl %%edi "
"pushl %%ebp "
"movl %%esp,%0 " /* save ESP */
"movl %3,%%esp " /* restore ESP */
"movl $1f,%1 " /* save EIP */
"pushl %4 " /* restore EIP */
"jmp __switch_to "
"1: "
"popl %%ebp "
"popl %%edi "
"popl %%esi "
:"=m" (prev->thread.esp),"=m" (prev->thread.eip),
"=b" (last)
:"m" (next->thread.esp),"m" (next->thread.eip),
"a" (prev), "d" (next),
"b" (prev));
} while (0)
asm volatile("pushl %%esi "
"pushl %%edi "
"pushl %%ebp "
"movl %%esp,%0 " /* save ESP */
"movl %3,%%esp " /* restore ESP */
"movl $1f,%1 " /* save EIP */
"pushl %4 " /* restore EIP */
"jmp __switch_to "
"1: "
"popl %%ebp "
"popl %%edi "
"popl %%esi "
:"=m" (prev->thread.esp),"=m" (prev->thread.eip),
"=b" (last)
:"m" (next->thread.esp),"m" (next->thread.eip),
"a" (prev), "d" (next),
"b" (prev));
} while (0)
上面的代码汇编之后大致如下:
#define switch_to(prev,next,last) do {
asm volatile("pushl %%esi "
"pushl %%edi "
"pushl %%ebp "
"movl %%esp,(prev->thread.esp) "/* save ESP */
"movl (next->thread.esp),%%esp "/* restore ESP */
"movl $1f,(prev->thread.eip) " /* save EIP */
"pushl (next->thread.eip) " /* restore EIP */
"jmp __switch_to "
"1: "
"popl %%ebp "
"popl %%edi "
"popl %%esi "
:"=m" (prev->thread.esp),"=m" (prev->thread.eip),
"=b" (last)
:"m" (next->thread.esp),"m" (next->thread.eip),
"a" (prev), "d" (next),
"b" (prev));
} while (0)
asm volatile("pushl %%esi "
"pushl %%edi "
"pushl %%ebp "
"movl %%esp,(prev->thread.esp) "/* save ESP */
"movl (next->thread.esp),%%esp "/* restore ESP */
"movl $1f,(prev->thread.eip) " /* save EIP */
"pushl (next->thread.eip) " /* restore EIP */
"jmp __switch_to "
"1: "
"popl %%ebp "
"popl %%edi "
"popl %%esi "
:"=m" (prev->thread.esp),"=m" (prev->thread.eip),
"=b" (last)
:"m" (next->thread.esp),"m" (next->thread.eip),
"a" (prev), "d" (next),
"b" (prev));
} while (0)
m = memory
b = ebx
a = eax
d = edx
f = forward