News:

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

Help with Loops

Started by marcd, November 11, 2010, 10:53:51 PM

Previous topic - Next topic

marcd

Hi, being new to assembler, I have the following code, which is intended to make a spinner.  Unfortunalely I cannot seem to get jnz to work properly.  The loop either runs once or more often, forevver.  My code seems to exactly follow the examples in the tutorials I have read.  What am I doing wrong? (Using MASM32 on Win XP Pro) -  Thank you!:
.386
.model flat, stdcall
option casemap :none

include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\masm32.inc

includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\masm32.lib

.data
    v db "|",0
    s db "/",0
    h db "-",0
    b db "\",0
    p db " ",0
    cr sdword 0Dh
   

.code
start:
        mov ecx, 10
                 
spin: 
        invoke StdOut, addr v
        invoke StdOut, addr cr
        invoke StdOut, addr s
        invoke StdOut, addr cr
        invoke StdOut, addr h
        invoke StdOut, addr cr
        invoke StdOut, addr b
        invoke StdOut, addr cr
        invoke StdOut, addr p
        invoke StdOut, addr cr
        dec ecx
        jnz spin

        invoke ExitProcess, 0

end start


KeepingRealBusy

ecx is not protected by the ABI so your call to StdOut wipes it out. Use EBX instead, or push ecx as you enter the loop, pop it before decrementing and push it again.

MichaelW

Quote from: KeepingRealBusy on November 11, 2010, 11:07:56 PM
ecx is not protected by the ABI so your call to StdOut wipes it out. Use EBX instead, or push ecx as you enter the loop, pop it before decrementing and push it again.

An example to make that clear:

    spin:
        push ecx
        invoke StdOut, addr v
        . . .
        pop ecx
        dec ecx
        jnz spin


eschew obfuscation

marcd

Thank you!!  Is there a good reference for that sort of information (ecx not being protected, but ebx is)?  Or is it because StdOut uses ecx and I should look at how those calls work before using them?  Thanks again..

Marc

dedndave

well - it is documented - but it isn't that difficult

EAX, ECX, EDX may be trashed across API calls, and are often used to return result values
(quite often, but not always: EAX = status, ECX = count, EDX = address)
EBX, ESI, EDI, EBP are preserved across API calls
DF - the direction flag should always return cleared if set inside a function

routines that you may write should generally follow the same guidelines

marcd

Thank you!  I understand.

Slugsnack

It can also vary across systems. What you are interested in is the calling convention. In this case it is stdcall. You can look this up and research other calling conventions and their usages.