I have a simple assembly function called from a c program and I have to use a instruction (FIDIV
) that needs a memory operand.
Is it safe to move the value to [esp - 2]
and use it in the next instruction or is it never safe to use the stack that way?
I know there are many workarounds, and I really don't need this anymore, so now it's just curiosity.
Using an offset like that will definately expose the data to corruption any time any action on the thread needs to touch the stack again. This can occur during interrupts, APCs, context switches, exceptions, etc. What you'll want to do instead is to actually reserve space on the stack and save a pointer to it.
sub esp, 4 ; Allways move it 4 byte increments. x64 may need 8 bytes
mov eax, esp ; EAX points to your 4 byte buffer
add esp, 4 ; Restore allocation.
Of course if you only need a few bytes, the push instruction is much faster
push eax
mov eax, esp ; EAX points to a buffer properly alligned to system
It's not safe - that part of the stack may be used for context switches, interrupts and possibly other things that your thread has little or no knowlege of or control over.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With