News:

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

why does INC ECX not work like I expect?

Started by georgek01, September 01, 2005, 03:03:08 PM

Previous topic - Next topic

georgek01

If someone will be so kind as to tell this 'half-brain' what he's doing wrong....

Why does the following only loop through once?


mov ecx,6d

.while ecx < 12d
; do something...
inc ecx
.endw
What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

Tedd

At a guess, your "do something" is rewriting the value of ecx.
For example calling another function will almost always change the value of ecx (aswell as eax, and edx) so if you're relying on its value then you should save it first.

mov ecx,6d

.while ecx < 12d
        PUSH ECX
        ; do something...
        POP ECX

        inc ecx
.endw


Alternatively, you could use ebx, which isn't so willingly corrupted (convention is to preserve the values of ebx, edi and esi {and esp and ebp})
No snowflake in an avalanche feels responsible.

georgek01

What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

Mark Jones

Isn't there some funky rules with .while-.endw? I think I've seen it written as While-ENDM? in any case, try this instead:


.data?
    iNum DD ?
.code
    mov iNum,6
@@:
    ; do whatever here
    cmp iNum,12    ; is iNum 12?
    jz @F          ; if so, break out
    inc iNum       ; else increment
    jmp @B         ; and loop
@@:


CMP tests the two values by subtracting them and affecting the zero-flag. JZ jumps only when the zero-flag is set (meaning only when the numbers match.) @@: are jump "labels" and @B means the previous label while @F means the next label.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

georgek01

Thanks Mark!

Apologies for this elementary questions...

Is @F an actual label, or just an instruction to jump to the next label? Same for @B - does it instruct to jump to the previous label? Could you be using label names instead?
What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

Tedd

Mark> isn't ENDM is end-macro?


George> there is a 'special' label called "@@" which you can use when you don't want to think of a label name (for small loops, etc.)
So to reference those labels you can say "jump to the next @@ label" (jmp @F) or "jump to the previous @@ label" (jmp @B) {F for FORWARD, and B for BACKWARD.}
You could just use 'normal' label names if you want. But these are useful so you don't have to keep writing loop1, loop2, bigloop, innerloop, etc..
No snowflake in an avalanche feels responsible.

georgek01

What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

Mark Jones

Quote from: Tedd on September 01, 2005, 03:38:21 PM
Mark> isn't ENDM is end-macro?

Yes it should be. But I recall seeing some weird code using .ENDM to terminate conditionals. But maybe I'm just losing my mind again... senile decay, ya know. :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

PBrennick

#8
Mark,
A lot of people will post 'untested' code to help others.  Errors tend to creep in.  I think this is the case here.

EDIT:  I just remembered that a FOR loop uses ENDM.  Maybe that was what you were thinking of?

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Mark Jones

Aaaha, that's it Paul. Why does it use ENDM instead of, say, NEXT/ENDF/ ENDFOR? :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

PBrennick

Mark,
I am glad I figured it out.  I was searching everywhere because I new I had experienced something similar.  Someday I need to straighten out my archives but I have, literally, tens of thousands of examples. <bangs head againtst the wall>

Anyway, I gave up, years ago, trying to crawl into the minds of the people who created these assemblers.  It is an exercise in futility.

... or maybe FEND, anything to prevent people from saying WHATFOR!

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Jeff

as for the "ENDM" stuff, the FOR (and FORC and REPEAT) macros are closed off by "ENDM" because they are well... ("preprocessor") macros.  ;)

PBrennick

Jeff,
So is WHILE so I don't get your point.  ;)

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Jeff

oh yeah and while.  :)

FOR, FORC, REPEAT (and WHILE) as i understand em are what i call "preprocessor" macros.  these macros are used to replicate or replace code (for different conditions) before any machine code is created (like other "normal" macros).  on the other hand, the .IF or .WHILE doesnt actually replicate code in that sense, it just generates code to simulate that logic (at runtime).  i hope that made sense.

PBrennick

Jeff,
You are missing the point, WHILE does not use ENDM, it uses ENDW which is what the whole sub discussion in this topic is about.  Please reread the posts s-l-o-w-l-y.

Paul
The GeneSys Project is available from:
The Repository or My crappy website