Why this program does not work correctly:


;-- ------------------------------------------------------
.386
.model flat,stdcall
option casemap:none
include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
includelib c:\masm32\lib\kernel32.lib
.data
fstr db "string-%d",13,10,0
.data?
hOutput dd ?
buf db 11 dup(?)
n dd ?
.code
start:
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hOutput, eax
mov ecx,5
lbl:
invoke wsprintf, addr buf, addr fstr, ecx
invoke WriteConsole, hOutput, addr buf, 11, addr n, NULL
loop lbl
invoke ExitProcess, 0
end start
;--------------------------------------------------------

It should return something like this:
string-1
string-2
string-3
string-4
string-5

:stupid:
Posted on 2003-02-22 03:48:06 by drG4njubas
ECX gets modified by the WriteConsole call. So push/pop it before/after calling the function.
And you should replace the "mov ecx, 5" with

xor ecx, ecx
inc ecx

and do a "inc ecx" after the wsprintf call _before_ you push it :)
Oh, and dont use the "loop" instruction :/
Posted on 2003-02-22 03:56:41 by bazik
Oh, and dont use the "loop" instruction :/


The "loop" instruction is OK to use unless it would be part of a time-critical section of code and it would be imperative to get the fastest execution.

In code such as this thread's example, a few more nanosecs wouldn't be noticeable.

OTOH, the "loop" instruction MUST always be used whenever you would aim for the smallest code size possible.

Raymond
Posted on 2003-02-22 10:52:28 by Raymond
Bazik,

Could you add "Download a debugger" to your signature ?:rolleyes:
Posted on 2003-02-22 12:03:10 by Axial
wsprintf changes ecx too.
Posted on 2003-02-22 21:26:59 by roticv

Bazik,

Could you add "Download a debugger" to your signature ?:rolleyes:


Yes,nice remark from Axial.

Maybe, Bazik can add this one to his sig:"Beta testers,red alarm! New product from M$"

Regards,

Vortex
Posted on 2003-02-23 04:44:23 by Vortex