C 函数调用约定:为什么使用 movl 而不是 pushl? [英] C Function Call Convention: Why movl instead of pushl?

查看:36
本文介绍了C 函数调用约定:为什么使用 movl 而不是 pushl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么以下几行使用 movl 将数据推送到堆栈指针下方是 GCC 生成的.

I don't understand why the following lines are using movl to push data below the stack pointer are produced by GCC.

movl    -4(%ebp), %eax      # -4(%ebp) <- local variable 1
movl    8(%ebp), %edx       # 8(%ebp)  <- first parameter
movl    %edx, 8(%esp)       # ??? WHY NOT:   pushl %edx
movl    %eax, 4(%esp)       # ??? WHY NOT:   pushl %eax
movl    -8(%ebp), %eax      # ??? WHY NOT:   pushl -8(%ebp)
movl    %eax, (%esp)
call    athena
movl    %eax, f

(完整代码)

我猜这段代码试图为函数调用推送 3 个参数.但是为什么不使用 pushl.这段代码的用途是什么,它是如何工作的?

I guess this code tries to push 3 parameters for the function call. But why isn't it using pushl. What is the usage of this code and how does this work?

推荐答案

Hans Passant 回答正确.push/pop 操作码可以分解为两个微操作,它们执行内存移动和堆栈指针的递增/递减.如果堆栈指针 - 或任何指针 - 被更新,然后立即在下一个操作码中使用,通常会发生执行停顿.通过堆栈指针访问各个内存位置(如您的示例中所示),不会出现停顿,并且操作可以配对,允许同时执行.

Hans Passant answered correctly. The push/pop opcodes can be broken down into two micro-ops which do a memory move and an increment/decrement of the stack pointer. If the stack pointer - or any pointer - is updated and then immediately used in the next opcode, an execution stall generally occurs. By accessing the individual memory locations through the stack pointer - as in your example - there would be no stall and the operations could be paired allowing them to be executed simultaneously.

如果它们的结果/来源彼此无关,任何超标量 CPU 类型都将尝试在单个周期中执行多个操作码.编译器正在为您做一些事情来加速执行,而手动操作是相当费力的.操作码可能比推送占用更多空间,但它们的执行速度大约是推送的两倍 - 所有其他事情都相同.

Any superscalar CPU type will attempt to execute multiple opcodes in a single cycle if their results/sources have nothing to do with one another. The compiler is doing something for you to speed up execution that would be fairly laborious to do by hand. The opcodes may occupy more space than pushes, but they will execute roughly twice as fast - all other things being the same.

这篇关于C 函数调用约定:为什么使用 movl 而不是 pushl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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