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



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




.



Relevant Pages

  • One RosAsm Pre-parser
    ... cmp eax 0 | ja L0<< ... mov ecx, D ...
    (alt.lang.asm)
  • Re: Fastcode Library Design
    ... cmp ecx, SMALLMOVESIZE ... lea edx, ... fild qword ptr [eax] ... mov ecx, ...
    (borland.public.delphi.language.basm)
  • Re: Help understanding uops, etc...
    ... get the stream parameter into EAX to fill stack variables ... mov eax, DWORD PTR _pStream$ ... mov DWORD PTR _in$, ecx ...
    (comp.lang.asm.x86)
  • Re: Optimization Questions
    ... mov ecx,; ... mov eax, ebx ... instructions go through port 0 and port 1. ...
    (comp.lang.asm.x86)
  • Re: Optimization Questions
    ... instructions go through port 0 and port 1. ... Pre-read the value in EAX ... mov, ax ... mov, ecx ...
    (comp.lang.asm.x86)