xor ebx, ebx //Start the cycle counter at 0
mov ecx, 3 //Put 3 into ecx for multiplication
Parity:
cmp eax, 1 //Are we done yet?
je Done //If so, go to the end
mov edx, eax //edx = eax
and edx, 1 //Get parity bit
cmp edx, 0 //Is it even?
je ParEven //If so, skip straight to that part
//eax is odd here
mul ecx //treble eax (ecx is just 3)
add eax, 1 //and +1
add ebx, 1 //increment cycle counter
//eax is now even, so flow straight into even handler
ParEven:
shr eax, 1 //divide by two
add ebx, 1 //increment cycle counter
jmp Parity //time to test parity again
:GJS1M 67dcbdbce4a0b67c4b48e86a6ae29205a95e4b83024a9d947213d1231800e8d9
:84 ef3afb18090d2fbf01bb368da3a79301
:1309304705 1309372649 >>10 <-- that's cool and all, but check my doubles over there
>>18
Why don't you go back to ``translating'', Cudder?
Name:
Anonymous2011-06-30 6:33
.loop:
test eax,1
jz .even
.odd:
mul dword 3
inc eax
.even:
shr eax,1
test eax,~1
jnz .loop
Might be better to use lea eax,[eax * 3 + 1], but I don't know if that's faster, smaller, or possible. I'm not really experienced in x86 assembly.
[The Collatz problem or 3n+1 problem is as follows. Take a natural number n.
If it's even, halve it; if odd, triple it and add one. Repeat the process with
the resulting number, and continue indefinitely. If n is 0, the resulting
sequence is 0, 0, 0, 0... It is conjectured but not proven that for any
positive integer n, the resulting sequence will end in 1, 4, 2, 1...
See also http://www.research.att.com/projects/OEIS?Anum=A006577
This program takes a series of decimal numbers, followed by linefeeds (10).
The entire series is terminated by an EOF (0 or "no change"). For each number
input, the program outputs, in decimal, the number of steps from that number
to zero or one, when following the rule above. It's quite fast; on a Sun
machine, it took three seconds for a random 640-digit number.
One more note. This program was originally written for Tristan Parker's
Brainfuck Texas Holdem contest, and won by default (it was the only entry);
the version I submitted before the contest deadline is at http://www.hevanet.com/cristofd/brainfuck/oldcollatz.b
Please, just leave, your replies are not going to be appreciated here whilst they might be appreciated on an imageboard, therefore it's more likely to be a worthwhile endeavor for you to post on one.