Sign in to follow this  

Creating a stackframe with size of local variables not divisible by 4 [asm]

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing some code in assembly and somehow I managed to write a standard function header which creates 18 bytes of space on the stack
push	ebp
mov	ebp, esp
sub	esp, 18
The code freezes when this function is called. When I change the size of locals to 20, it runs fine. The function is win32 window procedure and basically just calls DefWindowProc (unless its WM_DESTROY). I know, I shouldn't use unaligned space, but somehow I didn't pay attention to it and realized this doesn't work... So - at least I would like to know why? Oxyd

Share this post


Link to post
Share on other sites
In 32-bit stack mode (which Win32 uses), push and pop don't take any size operands, they automatically push or pop 4 bytes at a time. On 16-bit OS's such as DOS, they push/pop 2 at a time. There is some flag on the CPU to set this (from kernel mode).

Since push/pops work in this way, its a fatal error to have esp not divisible by the push/pop frame size.

I don't know exactly why the PC just freezes though.

Share this post


Link to post
Share on other sites
Enlightment [smile]

Probably this part of documentation confused me:
PUSH imm8                     ; 6A ib                [186] 
PUSH imm16 ; o16 68 iw [186]
PUSH imm32 ; o32 68 id [386]


My theory for the freezing up, is bad return address when ret'ing window procedure...

And also: thanks for the reply [smile]

Oxyd

Share this post


Link to post
Share on other sites
No probs.
I'm a bit unsure what happens when you push a byte register onto the stack, presumably it zero extends or something like that.

This is also the reason that PC's need to boot up in real-mode (16-bit), I think you can only set the stack word size in this mode.

Share this post


Link to post
Share on other sites
Sign in to follow this