The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: georgek01 on September 01, 2005, 03:03:08 PM

Title: why does INC ECX not work like I expect?
Post by: georgek01 on September 01, 2005, 03:03:08 PM
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
Title: Re: why does INC ECX not work like I expect?
Post by: Tedd on September 01, 2005, 03:11:56 PM
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})
Title: Re: why does INC ECX not work like I expect?
Post by: georgek01 on September 01, 2005, 03:19:07 PM
Thanks Tedd!  :U
Title: Re: why does INC ECX not work like I expect?
Post by: Mark Jones on September 01, 2005, 03:29:31 PM
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.
Title: Re: why does INC ECX not work like I expect?
Post by: georgek01 on September 01, 2005, 03:33:10 PM
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?
Title: Re: why does INC ECX not work like I expect?
Post by: Tedd on September 01, 2005, 03:38:21 PM
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..
Title: Re: why does INC ECX not work like I expect?
Post by: georgek01 on September 01, 2005, 03:40:47 PM
Once again... thanks Tedd.
Title: Re: why does INC ECX not work like I expect?
Post by: Mark Jones on September 01, 2005, 03:47:34 PM
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
Title: Re: why does INC ECX not work like I expect?
Post by: PBrennick on September 01, 2005, 04:23:07 PM
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
Title: Re: why does INC ECX not work like I expect?
Post by: Mark Jones on September 02, 2005, 12:51:43 PM
Aaaha, that's it Paul. Why does it use ENDM instead of, say, NEXT/ENDF/ ENDFOR? :)
Title: Re: why does INC ECX not work like I expect?
Post by: PBrennick on September 02, 2005, 04:28:11 PM
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
Title: Re: why does INC ECX not work like I expect?
Post by: Jeff on September 02, 2005, 08:18:06 PM
as for the "ENDM" stuff, the FOR (and FORC and REPEAT) macros are closed off by "ENDM" because they are well... ("preprocessor") macros.  ;)
Title: Re: why does INC ECX not work like I expect?
Post by: PBrennick on September 02, 2005, 09:29:50 PM
Jeff,
So is WHILE so I don't get your point.  ;)

Paul
Title: Re: why does INC ECX not work like I expect?
Post by: Jeff on September 03, 2005, 12:30:38 AM
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.
Title: Re: why does INC ECX not work like I expect?
Post by: PBrennick on September 03, 2005, 06:29:41 AM
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
Title: Re: why does INC ECX not work like I expect?
Post by: Jeff on September 03, 2005, 09:05:28 AM
well i saw mark's post and tried to justify the use of ENDM.

PS:  .WHILE and WHILE are two completely different things.  .WHILE-.ENDW vs WHILE-ENDM  :(
Title: Re: why does INC ECX not work like I expect?
Post by: PBrennick on September 03, 2005, 10:46:29 AM
This is going nowhere.  Say whatever you please.
Paul