为什么参数的函数大小增加到字大小? [英] Why argument's size of function is increased to word size?

查看:34
本文介绍了为什么参数的函数大小增加到字大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了适用于 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屋!

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