views:

399

answers:

8

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" ?

+3  A: 
pop edx  maybe ?

if you're implementing a stack

jspcal
Can be done with no other register involved? Like "pop <nothing>"
DavidH
no, you need it to pop somewhere
jn_
@DavidH: No such instruction in the x86 family. A side effect of such a non-orthogonal structure is that every instruction is assumed to have a particular purpose. Fancy tricks like the DEC processors or 68K with `CMP (SP)+,(SP)+` to pop two values off with only minor side effects (setting condition codes) aren't possible because the x86 stack pointer is not a general use register.
wallyk
+2  A: 

Try using pop eax

alemjerus
You mean "pop eax" ?
DavidH
Agreed. `push` on x86 platforms actually *decreases* the stack pointer (`esp`), since the stack grows towards lower memory addresses, which may seem a little counter-intuitive, given the name "stack".
stakx
Yeap, sorry. My bad.
alemjerus
A: 

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.

Mike
+3  A: 

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?

Stephen Canon
They are from external function calls
DavidH
External function calls don't, on their own, require lots of small adjustments to the stack pointer. Are you adjusting the stack pointer to satisfy an ABI alignment requirement? As part of popping arguments off the stack after the call?
Stephen Canon
+1  A: 

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.

Richard Pennington
+3  A: 

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.

stakx
+1  A: 

on some assemblers, add esp, 4 takes up 3 or 5 bytes. Try add esp, byte 4 for a two byte instruction.

Joshua
+1  A: 

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.

Potatoswatter