I used GetSystemTime to get system time,format year with wsprintf,
when use MessageBox , the window quit without rhyme or reason.

_WndProc Proc uses ebx edi esi , hWnd , uMsg , wParam , lParam
LOCAL lpTime[255]:byte

              .elseif ( uMsg == WM_COMMAND )
mov eax , hButtonOK
.if ( lParam == eax )
invoke GetSystemTime,addr lpTime
mov ax,lpTime.wYear
mov year,ax
invoke wsprintf,addr mess,CTXT("year :%d"),year
invoke MessageBox, NULL , addr mess,NULL,MB_OK ; as soon as run to this sentence,the window close
                .endif ......

can anybody figure out the question??
Posted on 2005-11-13 03:09:18 by czg1997
Don't use word-sized variables or registers as arguments to API calls :)

movzx eax, lpTime.wYear
invoke wsprintf,addr mess,CTXT("year :%d"), eax

try that to start with. How is "mess" defined, btw?
Posted on 2005-11-13 03:17:21 by f0dder
I've changed to this way:

xor eax,eax
mov ax,lpTime.wYear

invoke wsprintf,addr mess,CTXT("year :%d"),eax

and it goes well.

i made a mistake,the lpTime is a SYSTEMTIME structure,and mess is a 255 bytes buffer. :(
Posted on 2005-11-13 03:22:34 by czg1997
You should use movzx instead of the xor+mov, it's shorter and faster... not that it matters much in this code, but keep it in mind.
Posted on 2005-11-13 03:28:37 by f0dder
the important thing to notice here is that this is a masm bug
and not that you can't pass word/byte-sized arguments
(if dont believe me, try writing "LOCAL year : SWORD" and it will work)

czg1997, you didn't make any mistake.
Posted on 2005-11-13 06:07:26 by drizz

czg1997, you didn't make any mistake.

Actually, wsprintf expected a dword parameter (%d) but was passed a word parameter. It's not MASM's bug.
Posted on 2005-11-14 15:40:23 by QvasiModo

czg1997, you didn't make any mistake.

Actually, wsprintf expected a dword parameter (%d) but was passed a word parameter. It's not MASM's bug.

Actually, it IS a masm bug - for some reason, masm does

.text:00000000                push    0
.text:00000002                push    small wordparm

when it should simply do

.text:00000000                push    small wordparm

probably a size-extension leftover from 16bit code.
Posted on 2005-11-14 15:47:16 by f0dder
Oh, I didn't know that. I just saw the format string error. :(
Posted on 2005-11-14 15:56:00 by QvasiModo
Well, iirc when you push a 16bit variable in 32bit mode, it's automatically zero-extended... so it shouldn't cause problems. It's been quite a while since I looked into this though, so I *could* be wrong.
Posted on 2005-11-14 15:57:38 by f0dder
MASM probably does, but pushing a word into the stack seems to work. Then again, unaligned ESP values seem to work too until you call some API functions...
Posted on 2005-11-14 16:02:49 by QvasiModo
I was wrong, the CPU doesn't do this automatically (nor should it) - dunno where I had that idea from.

Anyway, the bug is in MASM - it should "push word ptr 0" rather than "push dword ptr 0" (as it does now) when trying to do size-extension. Just one of the many quirks :)
Posted on 2005-11-14 16:12:41 by f0dder