is it certain in which register arguments and variables are stored?

时光怂恿深爱的人放手 提交于 2019-12-06 13:27:15

Is it certain in which register arguments and variables are stored?

Only at entry and exit of a function.

There is no guarantee as to what registers will be used within a function, even for variables which are parameters to the function. Compilers can (and often will) move variables around between registers to optimize register/stack usage, especially on register-starved architectures like x86.

In this case, a simple assignment operation like rdx = rdi may not compile to any assembly code at all, because the compiler will simply recognize that both values can now be found in the register %rdi. Even for a more complex operation like rdx = rdi + 1, the compiler has the freedom to store the value in any register, not specifically in %rdx. (It may even store the value back to %rdi, e.g. inc %rdi, if it recognizes that the original value is never used afterwards.)

No, it would be translated into:

mov %rdi, %rdx # move %rdi into %rdx
xor %eax, %eax # zero return value
ret # return

Of course, it's more than likely that rdx = rdi (and therefore mov %rdi, %rdx) will be removed by the compiler, because rdx is not used again.

Credit to @Jester for finding this out before me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!