c - 基于汇编32位(x86)编写C代码

标签 c assembly x86

我有这段代码,我必须将其从汇编转换为c。我必须将函数的汇编代码转换为该函数的 C 代码。

function :
  1.pushl %ebp           
  2.movl %esp, %ebp      
  3.movl 12(%ebp), %eax                               
  4.movl 8(%ebp), %edx                          
  5.addl %edx, %eax                             
  6.addl 16(%ebp), %eax                          
  7.popl %ebp                
  8.ret               

我对行进行编号只是为了解释目的。这是我认为每一行正在做的事情:

1.Save frame pointer by moving it onto the stack. 
2. Create new frame pointer
3. Assigns value of the second parameter, which is 12 bytes above the base pointer, to the register eax
4. Assigns value of first parameter which is 8 bytes above base pointer to the register edx
5. Adds edx to the value in eax and stores the value in eax
6. Adds the value in third parameter which is 16 bytes about the base pointer, to the register eax
7.pops base pointer off of stack
8. returns the calling function

现在我很难真正理解如何将其转换为 C 代码。我想代码看起来有点像这样:

function (int *x, int *y, int *z){
*y = x + y;
*y = z + y; 
return y;  
}

我不确定它是否完全像这样,但这就是我想出的。我很难真正理解如何理解汇编。这个翻译有意义吗?如有任何帮助,我们将不胜感激。

最佳答案

您对汇编代码的解释是正确的。我可能要补充的一件事是,当函数返回一个值时,该值将在寄存器 A 中返回。所以 C 函数看起来像这样。

int function( int x, int y, int z )
{
    return x + y + z;
}

关于c - 基于汇编32位(x86)编写C代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22698173/

相关文章:

assembly - 如何用汇编语言编写 "Hello World"应用程序?

windows - 检测另一个进程的位数(在 Windows 中)

c - Malloc 返回相同的值 - 没有双重释放错误

c - 如何使用指针仅指向数组的一半

windows - C++ 如何导出地址表 Hook (EAT)?

linux - 我可以通过X86_64机器码获取内存访问信息吗?

assembly - x86 NASM 'org' 指令含义

c - 停止宏扩展

c - R 中负二项式密度 (dnbinom) 的参数化

linux - 汇编简单的I/O代码。 Int 转字符串错误