This is an issue that Microsoft doesn't seem to document very well.
I've got a small 100% asm app called EVE, that is unbreakable on
Win 9x, but there are stability issues on 2000/XP.
A little while ago, EVE was crashing just after startup, as soon as
someone tried "File/Open..." -- the process just disappeared, no
error message at all. Funny thing, this was on some 2000/XP systems,
but not all -- I have at home two installations, NT5/beta1 and XP, and EVE
worked fine, which was enormously frustrating.

Anyway, I eventually found, by a process of exhaustive elimination
and enormous help from various testers, that by moving a single
string from .DATA to .CODE (the string is read only), it worked on
This lead me to think that maybe data has to be aligned, which I had
never bothered about before. So, I've made sure all 32-bit data is
preceded with "ALIGN 4".

However, some people are still reporting crashes. These are happening
outside EVE, in an API func, don't yet know which one/s, but I've just
stuck a heap of extra code into EVE that tracks the last API call and I have
an auto "talkback" feature in EVE so on a crash people can send me a
email bug report (v3.23). So, I hope to find out which API func is being naughty.

But, I wonder. I recall a thread in this forum, someone mentioned that
*all* addresses passed as parameters to API funcs should be DWORD
aligned ... is this true? This would mean that I would even have to align
all strings to start on DWORD alignment. Anyone got any thoughts on this?

Barry Kauler
Posted on 2003-02-17 17:06:44 by bkauler

I have heard comments of this type before but I have yet to see code that fails through not having aligned string data. Almost exclusively the reason why you get some code that fails in 2k/xp that runs on 9x has to do with register preservation.

There are a few instances where return values are important where in 9x you can ignore them but in 2k/xp you will get a failure.


Posted on 2003-02-17 18:02:42 by hutch--
This is an extract from a thread earlier this year:

things to remembre on NT: register preservation is even more important than on 9x (though you wont get BSODs from not following it). stack must be 4-byte aligned (not a problem unless you're *messing* with the stack). and you must return 0 ("xor eax, eax") in dlgproc if you don't handle the message. none of these should cause a "invalid win32 app" peldr messagebox though.



Right. Additionally, an important thing I've not read anywhere is that on NT systems all data pointers you pass to the API must be DWORD aligned. This maybe wasn't documented explicitly because is no problem in C/C++, since the compiler takes care of it, but in asm it's an issue.

Generally, the problem happens when one defines some structures in his/her source, and those structures aren't made DWORD aligned. A more common problem than it may seem.


Okay, can we say that the advice to align pointers passed to APIs is NOT necessary?

Leave that one. What about the stack...
In a procedure, if I have local data:

Isn't that going to place a single byte on the stack, thus misaligning it?
...or, maybe it doesn't matter within a procedure?

It would be interesting to examine asm list file output of Win32 C/C++ or other high level language compiler, to see what they are doing.

In my case, I have always known the rules for register preservation, so that's not the problem. I seem to have an alignment problem. I've been going thru my app
DWORD aligning stuff, ... no crash reports from users this week!
Posted on 2003-02-28 03:18:36 by bkauler
Remember esp should be aligned to 4
Posted on 2003-02-28 05:52:38 by roticv
Aah, yes, I have just checked something.
I've got this func with a single local byte variable:


I used Jeremy Gordon's excellent debugger and found that it assembled as:

push ebp
mov ebp,esp
add esp,-4

Which means that MASM is intelligent and actually allocated 4 bytes on the
stack for the byte data.
Posted on 2003-03-01 06:32:15 by bkauler