繁体   English   中英

交流程序中的汇编代码

[英]assembler code in a c program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
 typedef int (FuncPtr)();
 char asmFunc[] = {0x90, 0xB8, 0x10, 0x00, 0x00, 0x00, 0xC3};
 FuncPtr *cFunc = malloc(7);
 memmove(cFunc, asmFunc, 7);
 int result = cFunc();
 printf("result = %d\n", result);
}

如果有人可以在intel i7 pc上修复汇编器部件,那将很棒,因为这会导致我的ubuntu出现segfault :)

这是将汇编代码放入ac程序的最佳方法吗?

将汇编代码放在C源文件中的最佳方法是使用内联汇编。 这是一个很好的起点 例:

int main(void)
{
 int x = 10, y;

 asm ("movl %1, %%eax;"
      "movl %%eax, %0;"
  :"=r"(y) /* y is output operand */
  :"r"(x)  /* x is input operand */
  :"%eax"); /* %eax is clobbered register */
}

可以不使用typedef来编写它,但是在不使用typedef的情况下将其强制转换为函数指针非常难看。

int (*testFuncPtr)(void);

强制转换为此函数指针将使用

(int (*)(void))

好吧,这当然不是将机器代码包含在C程序中的最佳方法。 使用内联汇编。 自从您遇到Ubuntu之后,我会提到gcc完全可以做到这一点。

有关开始,请参见http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM