Contribute  :  Web Resources  :  Past Polls  :  Site Statistics  :  Downloads  :  Forum  
    BiW ReversingThe challenge is yours    
 Welcome to BiW Reversing
 Saturday, December 03 2022 @ 12:48 AM CET
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Factorize()

 
Post new topic   Reply to topic    www.reversing.be Forum Index -> Coding Corner
View previous topic :: View next topic  
Author Message
BoR0
Regular
Regular


Joined: 28 Feb 2005
Posts: 105
Location: Europe

PostPosted: Fri Dec 02, 2005 11:08 pm    Post subject: Factorize() Reply with quote

formula: n! = (n)(n-1)(n-2)(n-3)...3*2*1

Code:
.data
prefix db "%d", 0
buffer db 32 dup(0)

.code
Factorize PROC n:DWORD
mov eax, n
mov ecx, eax

@@:
dec ecx

mul ecx

cmp ecx, 1
jne @B

ret
Factorize ENDP

start:
invoke Factorize, 5

; eax = 5 * 4 * 3 * 2 * 1
; eax = 120

invoke wsprintf, ADDR buffer, ADDR prefix, eax
invoke MessageBox, 0, ADDR buffer, 0, 0
invoke ExitProcess, 0

end start


thats all Wink
Back to top
View user's profile Send private message Visit poster's website
moniker
Regular
Regular


Joined: 05 Sep 2005
Posts: 123
Location: lage lande

PostPosted: Sat Dec 03, 2005 4:09 am    Post subject: Reply with quote

indeed a nice recursive function. However it isn't really the optimal way to do it imo.

(since a for loop will do to)

and that your processor probably has faster ways of doing this:
if it's are only ints, MMX instructions
otherwise 3DNOW! or SSE instructions ..

anybody got any experience with these?

i am trying to learn how to use them as efficiently as possible
any pointers to docs/democode would be appreciated Wink


ps: hmz come to think of it, if your only going to use int's you are probably better of with a look up table. since 50! is already way beyond int bounds
Back to top
View user's profile Send private message
parabytes
Frequent poster
Frequent poster


Joined: 14 Apr 2005
Posts: 92
Location: Israel

PostPosted: Sat Dec 03, 2005 9:19 am    Post subject: Reply with quote

13 is beyond the bounds of 32bits actually Smile

Also, this is not recursive, it's a loop.


To optimize a bit the loop I'd make it like

Code:

mov eax,1
mov ecx,N

@@:
 mul ecx
 dec ecx
 jnz @B

Size optimized:
@@:
 mul ecx
 loop @B



A bit better I'd say Smile

_________________
thoughts roam free and endless..
Back to top
View user's profile Send private message Visit poster's website
BoR0
Regular
Regular


Joined: 28 Feb 2005
Posts: 105
Location: Europe

PostPosted: Sat Dec 03, 2005 11:29 am    Post subject: Reply with quote

Yes, this example I prefer for using from 1 to 9 limit. I actually wrote it for a friend. To do for larger numbers you could use the FPU.
Back to top
View user's profile Send private message Visit poster's website
moniker
Regular
Regular


Joined: 05 Sep 2005
Posts: 123
Location: lage lande

PostPosted: Sat Dec 03, 2005 6:49 pm    Post subject: Reply with quote

omg, i must have been pretty tired, mistaking a loop for a function call. Guess i was seeing what i wanted to see. it was 4 am after all;


well i'm back to investigate the 3dnow! stuff
Back to top
View user's profile Send private message
tanatos
Frequent poster
Frequent poster


Joined: 16 Feb 2005
Posts: 68

PostPosted: Mon Dec 05, 2005 9:05 pm    Post subject: CSS and me Reply with quote

#include<stdio.h>
#include<conio.h>
void main(void)
{
int n,i;
unsigned long int frac=1;
printf("n=");scanf("%d",&n);
for(i=1;i<=n;i++)
{
frac=frac * i;
}
printf("%d",frac);
}

that looks better :p
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    www.reversing.be Forum Index -> Coding Corner All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
 Copyright © 2022 BiW Reversing
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.97 seconds