flat assembler
Message board for the users of flat assembler.
Index
> Heap > Euler challenge 2  Even Fibonacci numbers 
Author 

tthsqe
Did you get 4613732 as answer?
You code does seem a little verbose. I would start with the observation that only every third term is even. Thus: Code: xor ecx,ecx ; ecx=0 will contain our total xor eax,eax ; eax=0 is the 0^th fib number lea ebx,[eax+2] ; ebx=2 is the third fib number @@: add ecx,ebx push ebx lea ebx,[eax+4*ebx] pop eax cmp ebx,4000000 jbe @b ; ecx now contains answer Quote: ;Initialising ebx to 2 for division a much easier way of testing if eax is even is: Code: test eax,1 jz even odd: ... even: ... or Code: test eax,1 jnz odd even: ... odd: ... 

22 Feb 2014, 08:37 

watdapho
@tthsqe
Yeap 4613732 was my answer. Thanks for the input! very nice tips 

22 Feb 2014, 09:06 

tthsqe
Do you have a 64 bit windows system? If so, you can try to use the 64 bit calculator that I posted in the projects and ideas section. The straightforward code is
Code: n=0;total=0; While[Fibonacci[n]<=4000000, If[And[Fibonacci[n],1]==0, total=total+Fibonacci[n] ]; n=n+1 ]; total This also returns with 4613732, so at least all of the relevant circuits of my calculator are free of bugs. 

22 Feb 2014, 11:41 

Xorpd!
Instead of pushing ebx and popping eax you could just
lea eax, [eax+4*ebx] xchg eax, ebx xchg with eax is fast and also a 1byte instruction. 

25 Feb 2014, 16:52 

tthsqe
you are right. However, on amd, the xchg is slower than push/pop. How about you? Why isn't your name xorps? It is the same as xorpd but 1 byte shorter....


25 Feb 2014, 21:10 

Xorpd!
Actually, it's a past participle passive...
Also the extra byte may be useful in aligning subsequnt instructions. 

25 Feb 2014, 22:41 

< Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992020, Tomasz Grysztar.
Powered by rwasa.