为什么参数的函数大小增加到字大小?

编程入门 行业动态 更新时间:2024-10-28 16:18:39
本文介绍了为什么参数的函数大小增加到字大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我阅读了适用于 i386 和 AMD64 的 System V ABI.他们告诉参数必须四舍五入为字长的倍数.我不明白为什么.

I read System V ABI for i386 and AMD64. They are telling that arguments must be rounded to multiple of word size. And i dont understand why.

这是情况.如果您将 4 个字符参数传递给 i386 架构上的函数,它将占用 16 个字节(每个字符参数为 4 个字节).像局部变量那样为所有 4 个参数分配 4 个字节不是更有效吗?

Here is situation. If you pass 4 char arguments to a function on i386 architecture it will take 16 bytes (4 bytes for each char argument). Isn't it more efficient to allocate only 4 bytes for all 4 arguments like it should be with local variables?

对齐不是答案.因为在这两种情况下,16 字节堆栈对齐都可能需要 4-12 字节的填充.

Alignment is not the answer. Because it could take 4-12 bytes padding for 16 byte stack alignment in both situiation.

推荐答案

这样做不是更有效率吗……

Isn't it more efficient to ...

你总是必须说出你想要优化的内容:

You always have to say what you want to optimize:

执行速度快小程序大小更少的堆栈使用更简单的编译器...

如果您想优化以减少堆栈使用,将字节传递给函数确实会更有效.

If you want to optimize for less stack usage, passing bytes to the function really would be more efficient.

但是,通常您希望针对快速执行速度或小程序大小进行优化.

However, normally you want to optimize for fast execution speed or small program size.

与现代编译器(mov 堆栈的参数)不同,大多数 1990 年代编写的编译器我都知道 push 堆栈的参数.如果编译器使用 push 操作,将字节放入堆栈会相当复杂 - 它会使程序变慢和变长.

Unlike modern compilers (that mov the arguments to the stack) most compilers written in the 1990s I know push the arguments to the stack. If a compiler uses push operations, putting bytes to the stack would be rather complex - it would make the program slow and long.

(请注意,我从未见过对参数执行 pop 操作.)

(Note that I have never seen that a pop operation is done on a parameter.)

这篇关于为什么参数的函数大小增加到字大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 20:14:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1071082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   函数   参数   增加到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!