Re: Read N from keybord. Double even numbers. Using LOOP instruction



On Feb 26, 3:34 pm, Frank Kotler <fbkot...@xxxxxxxxxxx> wrote:
SpooK wrote:
On Feb 26, 10:58 am, "aeter" <A.ele...@xxxxxxxxx> wrote:

Thank you All..u guys are the best..im going to work on it..concerning
doubling the even numbers is like: S = 1 + (2+2) + 3 + (4+4) + 5 +
(6+6) +.....N

Ah! Yeah, what SpooK said.



In that case, this should work

;##### DOUBLE ALL THE EVEN NUMBERS IN A GIVEN RANGE #####

;Double all the even numbers
xor eax,eax ;Sum Holder
mov ecx,10 ;Counter and Number, all in one!!!

DOUBLE_LOOP:
test ecx,1
jnz ODD

;Process an "Even" Number
mov edx,ecx
shl edx,1
add eax,edx
loop DOUBLE_LOOP
jmp END

;Process an "Odd" Number
ODD:
add eax,ecx
loop DOUBLE_LOOP

;Continue on with the rest of the code...
END:
;##### WASN'T THAT FUN??? #####

I would double-check the use of that "test" instruction, I'm kind of
rusty at it ;)

Looks right to me. There's a potential "gotcha" here... As we know, "je"
is an alias for "jz"...

test eax, 1
je EVEN

This is *not* a test for equality! The "test" instruction does an "and"
operation, sets the flags accordingly, but doesn't store the result. If
more than one bit set in the second operand, the zero flag will be
cleared (NZ) if *any* of those bits is set in the first operand. Testing
multiple bits can be useful, but confusing. I've seen confused newbies
try to use "test" as if it were a replacement for "cmp"...

test eax, 7
je someplace

will jump if *none* of the bits in 7 are set, not if it's "equal".
Definitely the instruction to use to determine odd or even, but you have
to be careful with it. Remember it's a "forgetful and"...

Best,
Frank

Yeah, using "test" is the only time I use JZ/JNZ (I think), otherwise
I always use the alias JE/JNE... just keeps things clear and
consistent in my code :)

.



Relevant Pages