c语言核心部分(来源网上):
#define N 8 //计算N的阶乘,最多能算到9999
long a[8916]={1,0},n,i,c,len;
int main()
{
n=N;
for ( len=1;n>1; n--)
{
for (c=0,i=0; i
{
long p= a[i]*n+c;
a[i]= p % 10000;
c=p / 10000;
}
a[i]= c;
if (c>0)
len++;
}
printf("%d",a[len-1]);
for( len--;len>=0;len--)
printf("%04d",a[len]);
system("Pause");
return 0;
} 我用汇编改写了一下: ;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
;-=- Factorial By G-Spider @2010
;-=- ml /c /coff Factorial.asm
;-=- link /subsystem:console Factorial.obj
;-=- invoke MessageBox,0,0,0,0
;-=- _Factorial函数最多能算到9999!
;-=- 按字符Q或q 退出
;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
.386
.model flat,stdcall
include user32.inc
include kernel32.inc
include msvcrt.inc
includelib user32.lib
includelib kernel32.lib
includelib msvcrt.lib
.data
fmt0 db '%d! = %d',0
fmt1 db '%04d',0
fmt2 db 0dh,0ah,0
inform db 'Please input num (0~999):',0dh,0ah,0
outform db 'Please Re-input num 0~999 Or Q-exit: ',0dh,0ah,0
szPause db 'Pause',0
dwStrlen dd 8916
.data?
lpOutputBuf dd ?
len dd ?
.code
;****************************
_Factorial proc lpA:dword,N:dword
local @c,@len
mov @len,1
mov esi,lpA
cmp N,0
je EXIT
Loop1:
mov @c,0
xor ebx,ebx
@@:
;p= a[i]*n+c;
mov eax,[esi+ebx*4]
imul eax,N
add eax,@c
cdq
mov ecx,10000 ;基为10000
idiv ecx
mov [esi+ebx*4],edx
mov @c,eax
inc ebx
cmp ebx,@len
jb @B
mov [esi+ebx*4],eax
cmp eax,0
jbe A00
inc @len
A00:
dec N
cmp N,1
ja Loop1
EXIT:
mov eax,@len
ret
_Factorial endp
;******************************
_Input proc
local @num
mov @num,0
@@:
invoke crt_getchar
mov ebx,eax
.if ebx=='q' || ebx =='Q'
invoke ExitProcess,0
.elseif ebx >= '0' && ebx <= '9'
mov ecx,@num
imul ecx,10
sub ebx,'0'
add ecx,ebx
mov @num,ecx
.elseif ebx==0ah
mov eax,@num
.if eax < 0 || eax >999 ;实际可以用>9999
invoke crt_printf,offset outform
mov @num,0
jmp @B
.endif
ret
.endif
jmp @B
_Input endp
;******************************
_OutPut proc lpBuf:dword,@num:dword
invoke _Factorial,lpBuf,@num
dec eax
push eax
push esi
mov esi,lpBuf
invoke crt_printf,offset fmt0,@num,dword ptr[esi+eax*4]
pop esi
pop eax
test eax,eax
jz EXIT
dec eax
@@:
push eax
push esi
invoke crt_printf,offset fmt1,dword ptr[esi+eax*4]
pop esi
pop eax
dec eax
jns @B
EXIT:
invoke crt_printf,offset fmt2
xor eax,eax
ret
_OutPut endp
;******************************
start:
invoke crt_printf,offset inform
mov eax,dwStrlen
shl eax,2
mov dwStrlen,eax
invoke crt_malloc,eax
mov lpOutputBuf,eax
@@:
invoke RtlZeroMemory,lpOutputBuf,dwStrlen
mov esi,lpOutputBuf
mov dword ptr [esi],1
invoke _Input
invoke _OutPut,lpOutputBuf,eax
jmp @B
end start