What's wrong with this code:


               invoke GetDlgItemInt,hWnd,IDC_EDIT,IntValue,NULL

mov ECX,EAX
Go_on: push ecx
invoke SetDlgItemInt,hWnd,IDC_EDIT,ECX,0
pop ecx
.IF (ecx > 0)
dec ecx
jmp Go_on
.ENDIF



I mean, in the good old DOS days, you could make a LOOP, simply by making the
code loop to (in this case) Go_on:, and then ECX (CX) would be decremented,
and when CX got to zero, the loop ended.
But now I have to make a loop to Go_on, but in Win32Asm you obviously can't
do it quite the same way.

So my question:

How do I make a loop to Go_on, and then continue with the code after that loop ?:)
Posted on 2002-12-07 05:22:58 by The SharK
dec does not affect the flags. You could use sub.


invoke GetDlgItemInt,hWnd,IDC_EDIT,IntValue,NULL
mov ECX,EAX
Go_on: push ecx
invoke SetDlgItemInt,hWnd,IDC_EDIT,ECX,0
pop ecx
.IF (ecx > 0)
sub ecx,1
jnz Go_on
.ENDIF

That might work. Still seems a little weird what you are doing.

Maybe you want this?:


invoke GetDlgItemInt,hWnd,IDC_EDIT,IntValue,NULL
mov ECX,EAX
Go_on: push ecx
invoke SetDlgItemInt,hWnd,IDC_EDIT,ECX,0
pop ecx
sub ecx,1
.IF (ecx < 0)
jnz Go_on
.ENDIF

Im not really sure itf that is right either. I dont use the high level stuff. How about:


invoke GetDlgItemInt,hWnd,IDC_EDIT,IntValue,NULL
mov ECX,EAX
Go_on: push ecx
invoke SetDlgItemInt,hWnd,IDC_EDIT,ECX,0
pop ecx
dec ecx
test ecx,ecx
jnz Go_on
.ENDIF

I'm not sure if you want the ecx=0 loop to run, or stop after the ecx=1 loop.
Posted on 2002-12-07 06:17:30 by ThoughtCriminal
You need the opcode jecxnz, which means jump if ecx is not zero. The only trouble you may run into with this opcode is for loops where the count is zero based (i.e. indexes). So the code:


dec ecx
jecxnz Go_on

will replace your .IF statement.
Posted on 2002-12-07 06:18:34 by sluggy
"I dont use the high level stuff. How about "

Hi ThoughtCriminal

Well, actually I'm making a counter, just to practice...

When you say this, do you mean that you don't use .while .if and so on.
You just write the stuff manually with cmp and such...


Thanks sluggy I'll try that.
Posted on 2002-12-07 06:37:45 by The SharK
Hi sluggy

When I do what you say, it loops okay,
but it should also DISPLAY the value, AFTER
the increment of ECX, to make it count down.... :(
Posted on 2002-12-07 06:52:05 by The SharK
Hi,

that's wrong, dec does affect the flags. in the agner.hlp file it recommends to use dec ecx / jnz label instead of loop label

because of dec and jnz pair.

also jecxz is bad because it's unpairable but test ecx,ecx and jz pair.

for such a simple loop it isn't worth using the high-level things, i think because to loop n times just do



mov ecx,n
L1:
<code>
dec ecx
jnz L1


i hope this helps :)

-stormix
Posted on 2002-12-07 06:57:42 by stormix
When I do what you say, it loops okay,
but it should also DISPLAY the value, AFTER
the increment of ECX, to make it count down.... :(
You should check where your loop label is, you are obviously jumping back to the wrong spot.
Posted on 2002-12-07 06:59:00 by sluggy

jecxnz


Hmmm forgot about that one.
Posted on 2002-12-07 07:28:48 by ThoughtCriminal
the opcode loop still works.. but the loop must be smaller than 128 , IIRC.



loop1:
loop loop1


dec affects zero flag if i am not wrong, the other flags I am not so sure



loop1:
dec ecx
jnz loop1
Posted on 2002-12-07 08:56:19 by roticv



Hmmm forgot about that one.



I don't think there IS any JECXNZ, but there IS a JECXZ:tongue:
Posted on 2002-12-07 10:18:14 by The SharK
Hi Shark...
the .if .while .repeat loops used in masm are unsigned by default. to let masm know you want signed comparison you need to "cast" ecx so this should do the trick...


invoke GetDlgItemInt,hWnd,IDC_EDIT,IntValue,NULL
mov ECX,EAX
Go_on:
push ecx
invoke SetDlgItemInt,hWnd,IDC_EDIT,ECX,0
pop ecx
.IF ([B]sdword ptr[/B] ecx > 0)
dec ecx
jmp Go_on
.ENDIF


you also have sbyte sword

IMO though the loop you have is very inefficient due to the minimal code inside it. Always try to write your loops so that you don't have to do unecessary cmps. this usually means updating the loop control variable at either the start or end of the loop and followed by the appropriate branching opcode. In this case I would use:


invoke GetDlgItemInt,hWnd,IDC_EDIT,IntValue,NULL
mov ECX,EAX
Go_on:
push ecx
invoke SetDlgItemInt,hWnd,IDC_EDIT,ECX,0
pop ecx
dec ecx ;updatde loop control variable
jg Go_on ;signed jump ,continue looping if ecx is greater than zero


contrary to what ThoughtCriminal said dec DOES affect the same flags as sub otherwise it would be a less than useful instruction...
Posted on 2002-12-07 10:53:37 by MArtial_Code

contrary to what ThoughtCriminal said dec DOES affect the same flags as sub otherwise it would be a less than useful instruction...


Thats what I get for not touching this stuff for a couple of weeks.....

There was some thread about something with dec not doing what someone wanted it to do or some such... and I am just remebering incorectly.......

Wish I could find that thread.....
Posted on 2002-12-07 11:42:42 by ThoughtCriminal
Thanks all - it works well now :alright:
Posted on 2002-12-07 16:21:31 by The SharK
MArtial_Code,
INC/DEC do NOT affect the CF, but ADD/ADC/SUB/SBB do. Ratch
Posted on 2002-12-08 16:56:25 by Ratch