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

Started by
2 comments, last by Paradigm Shifter 19 years, 7 months ago
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
Advertisement
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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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
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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement