News:

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

StdIn StdOut whats wrong with my code

Started by Kyoy, July 14, 2005, 03:46:44 AM

Previous topic - Next topic

Kyoy

I am testing a printconsole function and i want to print "." for 10 times but instead i get an infinite loop. Can someone tell me whats wrong with my code



.386
.model flat, stdcall  ;32 bit memory model
option casemap :none  ;case sensitive


include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib

.data
CStdIn dd 0
CStdOut dd 0
COutStr db "."
ClnpStr db 255 dup (0),0
.code

start:
call OpenConsole
mov eax,0

loopness:
inc eax

call PrintConsole

cmp eax,10
je returnness
jmp loopness

returnness:

call CloseConsole
ret
OpenConsole:
  call AllocConsole
  push STD_INPUT_HANDLE
  call GetStdHandle
  mov CStdIn, eax

  push STD_OUTPUT_HANDLE
  call GetStdHandle
  mov CStdOut, eax
  ret


CloseConsole:
  call FreeConsole
  mov CStdIn, 0
  mov CStdOut, 0
  ret
PrintConsole:
push 0
push 0
push sizeof COutStr
push offset COutStr
push CStdOut
call WriteConsole
  ret 

end start

MichaelW

The API functions, like the MASM32 procedures, do not preserve EAX, ECX, or EDX. Possible solutions are:

Store ther loop counter in one of the registers that are preserved: EBX, ESI, or EDI.

Store the loop counter in a variable.

Store the loop counter on the stack before the call and recover it after the call returns.

eschew obfuscation

Kyoy

Thanks, i switched it to ebx and it works. But the problem is, the program activates so fast that i cannot tell if it is actually 10 of "." drawn. What are some possible methods to slow down every "." drawn. GetTickCount or something?

Mark Jones

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Kyoy

Thanks everyone, it all works. But just a question. MichaelW, what's wrong with using ecx? I thought that register is what loop counters use anyway?

MichaelW

I don't know any of the reasoning behind the decision to do so, but (E)CX has always been used as a scratch register in the Microsoft high-level languages. The LOOP instruction is hard-coded to use ECX, but these days most programmers tend to avoid using LOOP because it's relatively slow.
eschew obfuscation