flat assembler
Message board for the users of flat assembler.
Index
> Windows > stack manipulation push vs mov[esp+xx] |
Author |
|
fpissarra 31 Jul 2019, 11:00
int0x50 wrote: in order to push values onto the stack, we can use push val1, push val2; etc. Consider this: Code: push rbx push rbp push r12 ... Each PUSH will subtract 8 from RSP and write a value to the stack. The code above is the same as: Code: sub rsp,8 mov [rsp],rbx sub rsp,8 mov [rsp],rbp sub rsp,8 mov [rsp],r12 ... Clearly this can be writen as: Code: sub rsp,24 mov [rsp+16],rbx mov [rsp+8],rbp mov [rsp],r12 ... |
|||
31 Jul 2019, 11:00 |
|
int0x50 31 Jul 2019, 11:04
Thanks for the response fpissarra. But why would compiler do that, instead of just pushing?
|
|||
31 Jul 2019, 11:04 |
|
edfed 31 Jul 2019, 11:06
variables on stack are manipulated like this in functions bodies.
it depends on calling convention used by the compiler. |
|||
31 Jul 2019, 11:06 |
|
revolution 31 Jul 2019, 11:23
It depends upon the compiler O setting, and maybe some other settings also. And there is the belief that using RISC type instructions (instead of CISC type) can equate to better performing code in some circumstances.
But like all optimisations it depends heavily upon which system the code is running on and the actual task it is performing. The extra code bytes can cause problems with cache thrashing, but the enhanced opportunities for the CPU to extract parallelism from the code stream may mitigate that. So there is a trade-off. If you really need that particular function to perform at it's best then you will need to measure the final performance with each option. See which works best for your usage case. |
|||
31 Jul 2019, 11:23 |
|
fpissarra 31 Jul 2019, 16:40
int0x50 wrote: Thanks for the response fpissarra. But why would compiler do that, instead of just pushing? I believe this was already explained by me: PUSH will do 2 things: SUB RSP,8/MOV [RSP],reg/mem/imm Multiple PUSHes will do these 2 things multiple times... |
|||
31 Jul 2019, 16:40 |
|
bitRAKE 31 Jul 2019, 20:37
This is such a common pattern that more modern processors have dedicated hardware to cope with stack changes.
https://stackoverflow.com/questions/36631576/what-is-the-stack-engine-in-the-sandybridge-microarchitecture There is a symbiotic relationship between compilers and processor evolution. Processors are not strictly designed to be as fast/featureful as possible. Rather they are designed to meet the use cases of industry. If the majority of code is compiled by a handful of compilers then that is the target. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
31 Jul 2019, 20:37 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.