运行结果:(参考书籍《LInux内核完全注释》,赵炯编著)
百度网盘下载地址:
https://pan.baidu.com/s/1_-IznMWL3z1CROziiD6mCw
仿照手中自己编写的代码:
#include <stdio.h>
#define mystrcpy(dest, source) \
({ \
register char *mydest __asm__("edi"); \
__asm__("cld; \
pushl %%edi; \
1: \
movsb; \
cmpb $0, (%%esi); \
jne 1b; \
popl %%edi" \
:"=D"(mydest) \
:"S"(source),"D"(dest)); \
mydest;})
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);
int mod(int x, int y);
int main(void) {
int x = 100, y = 33;
printf("%d + %d =%d\n%d - %d =%d\n%d * %d =%d\n\
%d / %d =%d\n%d %% %d =%d\n", x, y, add(x, y), x, y, sub(x, y), \
x, y, mul(x, y), x, y, div(x, y), x, y, mod(x, y));
char dest[0x100] = "NULL!";
char source[0x100] = "Hello, World!";
printf("%s\n\n%s\n\n", source, dest);
printf("%s\n\n", mystrcpy(dest, source));
printf("%s\n\n%s\n\n", source, dest);
}
int add(int x, int y) {
register int __result __asm__("eax");
__asm__("addl %1, %2"
:"=a"(__result):"b"(y),"0"(x));
return __result;
}
int sub(int x, int y) {
register int __result __asm__("eax");
__asm__("subl %1, %2"
:"=a"(__result):"b"(y),"0"(x));
return __result;
}
int mul(int x, int y) {
register int __result __asm__("eax");
__asm__("mull %1"
:"=a"(__result):"b"(y),"0"(x),"d"(0));
return __result;
}
int div(int x, int y) {
register int __result __asm__("eax");
__asm__("divl %1"
:"=a"(__result):"b"(y),"0"(x),"d"(0));
return __result;
}
int mod(int x, int y) {
register int __result __asm__("eax");
__asm__("divl %1\n"
"movl %3, %0"
:"=a"(__result):"b"(y),"0"(x),"d"(0));
return __result;
}