函数调用和汇编 [英] Function Calls and Assembly

查看:67
本文介绍了函数调用和汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解程序如何使用汇编x86代码进行函数调用(使用C语义).任何帮助将不胜感激.

Im trying to understand how a program makes a function call (using C semantics) with assembly x86 code. Any help would be greatly appreciated.

我找不到任何来源专门回答这个问题.

I could not find any sources to specifically answer this question.

推荐答案

在x86中,有称为 call ret 的指令来执行此操作. call 将当前地址存储在堆栈上,并将jmp存储到作为参数传递的标签上.然后,名为 ret 的指令会弹出该地址,并在向该地址添加一个字节后跳转到该地址.

In x86, there are the instructions called call and ret to do this. call store the current address on stack and jmp to a label passed as argument. And the instruction called ret pop this address and jump to it after add one byte to that address.

代码示例:

C

int sum(int a, int b)
{
  return a + b;
}

void f(void)
{
  sum(2, 2);
  g();
}

编译器可能会生成(类似x86的汇编示例):

A compiler might generate(x86-assembly-like example):

f:
  push 2
  push 2
  call sum
  call g
  ret

sum:
   pop eax
   pop ebx
   add eax, ebx
   ret

我希望对您有帮助

这篇关于函数调用和汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆