Hi all!

I'm new here, and I come with a quick question. If I have a loop, it counts down. Is it possible to make it count up, from 0-25 instead of 25-0, or at least getting the value of cx that way. It would be possible if you could get the absolute (positive) value of cx-(cx+1). Can you get the absolute value from a register in ASM? "ABS cx" doesn't seem to work.

Thanks,
Martin
Posted on 2007-08-18 17:32:59 by Heptagonal

xor cx,cx
.my_loop:
;... loop stuff...
inc cx
cmp cx,25
jb .my_loop
Posted on 2007-08-18 18:51:38 by SpooK
	mov edx,-25
my_loop:
lea ecx,[25+edx]; ECX := 0,1,2,3,...
add edx,1
jnz my_loop

abs(eax)

@1: neg eax
js @1

abs(eax)

cdq
xor eax,edx
sub eax,edx


that is
	mov dx,-25
@@:
mov cx,25
add cx,dx; CX := 0,1,2,3,...
add dx,1
jnz @b
abs(ax)
@@:	neg ax
js @B

abs(ax)
	cwd
xor ax,dx
sub ax,dx


Posted on 2007-08-18 20:50:53 by drizz
Just to clear up one detail, when you go from 25 to 0, it would normally mean that the "25" value would be processed and the "0" value would NOT be processed.

If you use CX strictly as a counter, it does not matter if the "25 is not processed but the "0" gets processed. However, if you use the actual value of CX within your loop, then it would matter how you handle the counting. SpooK's code does not process the "25 but does process the "0" value. If you want the opposite, shift the inc cx to the start of the loop (and if CX gets modified within the loop, save it on the stack and retrieve it at the end of the loop):

xor cx,cx
.my_loop:
inc cx
push cx    ;if required
;... loop stuff...
pop cx  ;if required
cmp cx,25
jb .my_loop

Raymond
Posted on 2007-08-18 21:02:37 by Raymond