C/C++中嵌入汇编的方法是使用__asm{},在花括号中就可以写汇编代码了。然后,之前可见的变量在汇编中均可见。
以下是一个简单的例子,将HELLO WORLD中的每个字符加上0x20后再输出:
#include<stdio.h>
#include<string.h>
int main()
{
char *s="HELLO WORLD";
char d0[256];
char *d=d0;
int count=strlen(s);
__asm
{
xor eax, eax;
mov ecx, count;
mov esi, s;
mov edi, d;
NEXT:
mov al, byte ptr[esi];
add al, 0x20;
mov byte ptr[edi], al;
add esi, 0x1;
add edi, 0x1;
sub ecx, 0x1;
jz DONE;
jmp NEXT;
DONE:
}
d[count]='\0';
printf("%s\n",d);
return 0;
}
运行:
hello@world
Press any key to continue . . .