Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: //\\\\o//\\\\annabee <Wannabee@xxxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 05:05:36 -0000
På Tue, 27 Feb 2007 05:04:00 -0000, skrev //\\o//\\annabee <Wannabee@xxxxxxxxxxxx>:
På Tue, 27 Feb 2007 04:59:15 -0000, skrev //\\o//\\annabee <Wannabee@xxxxxxxxxxxx>:
På Mon, 26 Feb 2007 21:34:20 -0000, skrev Frank Kotler <fbkotler@xxxxxxxxxxx>:
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"...
Think I did this error once or twice as well.
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"...
Rereading his post, if N is assumed 32 bit, and inputted from the commandline he could add the pairs together in one loop, and the nonpairs in another.
like this for instance
mov edx 0
mov ecx 2
While ecx <= N
add edx ecx
add edx ecx
add ecx 2
End_While
mov ecx 1
While ecx <= N
add edx ecx
add ecx 2
End_While
zum in edx
or in one loop like maybe this
mov edx 0
mov ecx 2
mov eax 1
While ecx <= N
add edx ecx
add edx ecx
add ecx 2
add edx eax
add eax 2
End_While
Sorry. I am drunk. Now I go kill myself :(
Best,
Frank
.
- Follow-Ups:
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: Frank Kotler
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- References:
- Read N from keybord. Double even numbers. Using LOOP instruction
- From: aeter
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: SpooK
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: Rod Pemberton
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: SpooK
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: aeter
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: SpooK
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: Frank Kotler
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: //\\\\o//\\\\annabee
- Re: Read N from keybord. Double even numbers. Using LOOP instruction
- From: //\\\\o//\\\\annabee
- Read N from keybord. Double even numbers. Using LOOP instruction
- Prev by Date: Re: Read N from keybord. Double even numbers. Using LOOP instruction
- Next by Date: 64 bit Menuet 0.59 released
- Previous by thread: Re: Read N from keybord. Double even numbers. Using LOOP instruction
- Next by thread: Re: Read N from keybord. Double even numbers. Using LOOP instruction
- Index(es):
Relevant Pages
|