The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Kyoy on July 14, 2005, 03:46:44 AM

Title: StdIn StdOut whats wrong with my code
Post by: Kyoy on July 14, 2005, 03:46:44 AM
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
Title: Re: StdIn StdOut whats wrong with my code
Post by: MichaelW on July 14, 2005, 04:10:40 AM
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.

Title: Re: StdIn StdOut whats wrong with my code
Post by: Kyoy on July 14, 2005, 04:34:33 AM
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?
Title: Re: StdIn StdOut whats wrong with my code
Post by: Mark Jones on July 14, 2005, 04:37:45 AM

    invoke Sleep,1000


? :bg
Title: Re: StdIn StdOut whats wrong with my code
Post by: Kyoy on July 14, 2005, 05:40:46 AM
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?
Title: Re: StdIn StdOut whats wrong with my code
Post by: MichaelW on July 14, 2005, 06:37:35 AM
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.