News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

A hard-learned lesson in ASM programming

Started by Larry Hammick, November 01, 2009, 12:57:54 AM

Previous topic - Next topic

Larry Hammick

Make damn sure the stack pointer ESP is a multiple of 4 at all times! :green This sort of thing:


GetSizeFromFilespec:              ;input on the stack is the lp of a filespec
enter sizeof(WIN32_FIND_DATA),0
mov eax,esp
push eax
push [ebp+8]
call FindFirstFileA
...

will bring forth inexplicable error codes from deep inside the Windows kernel. Likewise Winsock and User will misfire in the strangest ways if they are called when ESP is not a multiple of 4. The problem in the above example is that sizeof(WIN32_FIND_DATA) is 318 and that is not a multiple of 4.

MichaelW

The Microsoft compilers would pad the last member to a multiple of 4 bytes, the largest alignment required by any of the members, so the length would be 320 bytes. For MASM, the problem can be corrected by specifying a 4-byte alignment:

WIN32_FIND_DATA STRUCT DWORD
  dwFileAttributes      DWORD      ?
  ftCreationTime        FILETIME <>
  ftLastAccessTime      FILETIME <>
  ftLastWriteTime       FILETIME <>
  nFileSizeHigh         DWORD      ?
  nFileSizeLow          DWORD      ?
  dwReserved0           DWORD      ?
  dwReserved1           DWORD      ?
  cFileName             BYTE MAX_PATH dup(?)
  cAlternate            BYTE 14 dup(?)
WIN32_FIND_DATA ENDS

eschew obfuscation

sinsi

heh, you are going to love 64-bit windows programming then...
Light travels faster than sound, that's why some people seem bright until you hear them.

BlackVortex

Jesus Christ. Did I see an enter command there ?!   :green