首先,给自己一个 x86 操作码列表,应该很容易在网上找到。
该asm()
函数遵循以下顺序:
asm ( "assembly code"
: output operands /* optional */
: input operands /* optional */
: list of clobbered registers /* optional */
);
其次,您遇到的一个主要问题是您不能“跳转”到 C 标签,您需要将标签设置为“汇编”标签才能跳转到它。前任:
int main()
{
asm("jmp .end"); // make a call to jmp there
printf("Hello ");
asm(".end:"); //make a "jumpable" label
printf("World\n");
return 0;
}
当我们跳过“Hello”时,该程序的输出只是“World”。这是相同的示例,但有一个比较跳跃:
int main()
{
int x = 5, i = 0;
asm(".start:");
asm("cmp %0, %1;" // compare input 1 to 2
"jge .end;" // if i >= x, jump to .end
: // no output from this code
: "r" (x), "r" (i)); // input's are var x and i
printf("Hello ");
i++;
asm("jmp .start;");
asm(".end:");
printf("World\n");
return 0;
}