i learned at school how to calculate factorials then when arrived at home i coded a small program, dunno if it'll work in all cases but here it goes :

xor ebx, ebx
xor eax, eax
xor edx, edx
mov eax, 5d ; number to calculate the factorial
mov ecx, eax
dec eax
cmp eax, 0
je end
imul ecx, eax
jmp fact
int 3 ; factorial in ecx

if anyone wants to optimize and\or do something. :grin:

The Keeper
Posted on 2002-02-21 09:51:38 by The Keeper
The decrement will set the zero flag, so the comparison after it is not needed.

Posted on 2002-02-21 10:04:42 by Mirno
The Keeper:
Do you have any idea how big 5D! is?
You would need a special proc and for culculating
and for displayng the value (it's bigger then number of atoms
in the universe)
and if you not speed up process Stirling formula
n!~ sqrt(2pi*n*n^n*e^-n),n-> ..
it would take a lot of time since you are going to work
with Veeeeeery big numbers in a circle.

I'd recommend you to read school textbook first ;)
0D! is max that signed 32 integer can hold (7328CC00h).
Of course for n where n! < 8000 0000h you don't need any
proc - just a table with 13 dwords wich holds values
from 1! to 13!
so if you know n you can get n! just by
mov eax,n
dec eax
mov eax,dword ptr
Posted on 2002-02-21 12:39:41 by The Svin

mov eax,n
dec eax
mov eax,dword ptr
mov eax,n

mov eax,dword ptr [-4]

Of course. :)
Posted on 2002-02-21 12:49:23 by bitRAKE
Right :)
Posted on 2002-02-21 12:59:57 by The Svin
uhm i need the factorial only of small numbers :P

thanks all

in the future i'll code something better.
Posted on 2002-02-21 23:21:03 by The Keeper
int factorial (int n)
int i, res = 1;

for ( i = 1; i <= n; i ++)
res *= i;
return res;

can it be compared to mine ? in terms of supporting big numbers?
Posted on 2002-02-22 12:00:39 by The Keeper