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
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})
Thanks Tedd! :U
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.
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?
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..
Once again... thanks Tedd.
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
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
Aaaha, that's it Paul. Why does it use ENDM instead of, say, NEXT/ENDF/ ENDFOR? :)
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
as for the "ENDM" stuff, the FOR (and FORC and REPEAT) macros are closed off by "ENDM" because they are well... ("preprocessor") macros. ;)
Jeff,
So is WHILE so I don't get your point. ;)
Paul
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.
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
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 :(
This is going nowhere. Say whatever you please.
Paul