Me again.
I'm having a lot of "add esp, 4" in my program and I'm trying to reduce its size. Is there any smaller instruction that can replace "add esp, 4" ?
Me again.
I'm having a lot of "add esp, 4" in my program and I'm trying to reduce its size. Is there any smaller instruction that can replace "add esp, 4" ?
If you're managing a stack(I assume you are), you can use push eax
and pop eax
to add values to the stack and maintain the esp
. You can also use instructions such as pusha/popa
to push/pop all GPRs on/off the stack and pushf/popf
to push/pop the EFLAGS registers on/off the stack.
A better question might be: "why do you have so many add esp, 4
instructions, and what can you do to have less of them?" It's somewhat unusual to be doing lots of small increments to the stack pointer like this.
Are you moving things to/from the stack at the same time? Could you use push
/pop
instead?
Alternatively, do you really need to update the stack pointer so frequently, or could you get away with moving it once at the beginning of a block of code to make some space on the stack, then restoring it once at the end of the routine?
What are you really trying to do?
One way to do it if you have multiple function calls:
sub esp, 4
mov 0(esp), param
call ...
...
mov 0(esp), param2
call ...
...
add esp, 4
That is, reuse the stack allocated for the first parameter over several function calls.
Sorry if this will sound trivial... but if you manage to reorder your code so that several add esp, 4
instructions are consecutive, you can of course simplify them to e.g.:
add esp, 8
or even:
add esp, 12
Just make sure that moved instructions don't reference esp
or the stack; or if they do reference something on the stack, they do only via the ebp
register.
on some assemblers, add esp, 4 takes up 3 or 5 bytes. Try add esp, byte 4 for a two byte instruction.
popfd
will add 4 to esp
in just one byte, with the side effect of randomizing your flags. It might be slow to execute; I don't know.
Of course it would help to see code or know what your requirements really are.