/**----------------------------------
C program "caculate pi"
20000位 2s on PC 1.7GHz,RAM256,win2000
by sunnisdu@hotmail.com
山东大学
-------------------------------------*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
unsigned int *arrBase;
unsigned int *arrCac;
unsigned int *arrResult;
unsigned int DecNum=1000;
unsigned int NUM;
unsigned int StartPos;
unsigned int StartPos4;
void initArr(unsigned int* arr)
{
_asm{
mov ecx, NUM
mov ebx, arr
_init:
mov dword ptr [ebx],0
add ebx,4
loop _init
}
}
void addArr(unsigned int* arr,unsigned int* arr2)
{
__asm{
mov ecx, NUM
mov esi, arr
mov edi, arr2
mov eax, ecx
shl eax, 2
add esi, eax
add edi, eax
clc
_add:
sub esi, 4
sub edi, 4
mov eax, [edi]
adc [esi],eax
loop _add
}
}
void subArr(unsigned int* arr,unsigned int* arr2)
{
_asm {
mov ecx, NUM
dec ecx
mov esi, arr
mov edi, arr2
clc
_sub:
mov eax, [edi+ecx*4]
sbb [esi+ecx*4],eax
loop _sub
}
}
void mulArr(unsigned int* arr,unsigned int num)
{
_asm{
mov ecx,NUM
mov ebx,ecx
shl ebx,2
add ebx,arr
xor edx,edx
mov edi,num
xor esi,esi
_mul:
sub ebx,4
mov eax,[ebx]
mul edi
add eax,esi
mov [ebx],eax
adc edx,0
mov esi,edx
loop _mul
}
}
void divArr(unsigned int *arr,unsigned int num)
{
__asm{
mov ebx,arr
mov ecx,NUM
xor edx,edx
_div: mov eax,[ebx]
div num
mov [ebx], eax
add ebx, 4
loop _div
}
}
/*
caculate arctan(1/num), and store the result to arr
*/
void arctanX(unsigned int num)
{
unsigned int dd;
unsigned int remain=0;
unsigned int loopN;
dd=(long)num*num;
loopN=(int)(32*NUM*log(2)/log(5));
initArr(arrBase);
initArr(arrCac);
arrBase[0]=1;
divArr(arrBase,num);
StartPos=1;
_asm mov ebx,1
forloopN:
_asm mov eax, StartPos
_asm shl eax, 2
_asm mov StartPos4,eax
_asm mov edi, arrCac
_asm add edi, StartPos4
_asm mov esi, arrBase
_asm add esi, StartPos4
_asm mov ecx, NUM
_asm sub ecx, StartPos
_asm xor edx, edx
_asm test ebx,2
_asm jnz cond2
cond1:
fori1:
_asm mov eax, [esi]
_asm div ebx
addCac:
_asm add [edi], eax
_asm jnc fori11
_asm push edi
addCarry: _asm sub edi, 4
_asm add dword ptr [edi], 1
_asm jc addCarry
_asm pop edi
fori11:
_asm add edi, 4
_asm add esi, 4
_asm loop fori1
_asm jmp divarrBase
cond2:
fori2:
_asm mov eax, [esi]
_asm div ebx
subCac:
_asm sub [edi], eax
_asm jnc fori22
_asm push edi
subCarry:
_asm sub edi, 4
_asm sub dword ptr [edi],1
_asm jc subCarry
_asm pop edi
fori22:
_asm add esi, 4
_asm add edi, 4
_asm loop fori2
divarrBase:
_asm xor eax, eax
_asm xor edx, edx
_asm mov esi, dd
_asm mov edi, arrBase
_asm mov ecx, NUM
forBase:
_asm mov eax,[edi]
_asm div esi
_asm mov [edi], eax
_asm add edi, 4
_asm loop forBase
_asm inc ebx
_asm inc ebx
_asm mov esi, arrBase
_asm mov eax, StartPos
_asm shl eax, 2
_asm add esi, eax
_asm cmp dword ptr [esi],0
_asm jnz forloopN
_asm inc StartPos
_asm mov eax,StartPos
_asm cmp eax, NUM
_asm jb forloopN
}
void pArr(unsigned int arr[])
{
printf("%d.\n",arr[0]);
for(unsigned int i=1;i<NUM;i++){
arr[0]=0;
mulArr(arr,100000);
printf("%05ld",arr[0]);
if(i%20==0) printf("\n");
}
}
unsigned int main()
{
unsigned int x;
unsigned int i;
unsigned int j;
unsigned int buf[10];
unsigned int tmp;
FILE* fp;
time_t timerStart, timerEnd;
printf("-----caculating pi--------\n");
printf("input nums of pi:");
scanf("%d",&DecNum);
NUM= (int)(DecNum / log10(2) / 32) + 4;
arrBase=(unsigned int*)calloc(NUM,4);
arrCac=(unsigned int*)calloc(NUM,4);
arrResult=(unsigned int*)calloc(NUM,4);
if(arrBase==NULL || arrCac==NULL || arrResult==NULL)
{
printf("\nerror alloc memory!");
return 9;
}
time(&timerStart);
printf("working... ...");
x=5;
initArr(arrResult);
arctanX(x);
mulArr(arrCac,16);
addArr(arrResult,arrCac);
x=239;
arctanX(x);
mulArr(arrCac,4);
subArr(arrResult,arrCac);
time(&timerEnd);
printf(" ok,time used: %ld\n",timerEnd-timerStart);
fp=fopen("pi.txt","w+");
if(fp==NULL){
printf("error! create file");
return 2;
}
else{
fprintf(fp,"%d.\n",arrResult[0]);
for(i=1;i<=DecNum/5;i++){
arrResult[0]=0;
mulArr(arrResult,100000);
fprintf(fp,"%05d",arrResult[0]);
if(i%20==0) fprintf(fp,"\n");
}
fclose(fp);
}
return 0;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |