共有回帖数 0 个
-
/**----------------------------------
C program "caculate pi"
by sunnisdu@hotmail.com
山东大学
2004-10-16
-------------------------------------*/
#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 mov ecx, NUM
_asm sub ecx, eax
_asm shl eax, 2
_asm mov edi, arrCac
_asm add edi, eax
_asm mov esi, arrBase
_asm add esi, eax
_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 adc dword ptr [edi-4],0 // this makes addCarry less possible 减少进位addCarry的可能
_asm jnc fori11
_asm push edi
_asm sub edi, 4
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 sbb dword ptr [edi-4],0 // this makes subCarry less possible 减少借位subCarry的可能
_asm jnc fori22
_asm push edi
_asm sub edi, 4
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;iNUM;i++){
arr[0]=0;
mulArr(arr,100000);
printf("%05ld",arr[0]);
if(i%20==0) printf("n");
}
}
void main()
{
unsigned int x;
unsigned int i;
unsigned int j;
unsigned int buf[10];
unsigned int tmp;
FILE* fp;
clock_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;
}
timerStart=clock();
printf("working... ...");
x=5;
initArr(arrResult);
arctanX(x);
mulArr(arrCac,16);
addArr(arrResult,arrCac);
x=239;
arctanX(x);
mulArr(arrCac,4);
subArr(arrResult,arrCac);
timerEnd=clock();
printf("nOk,time used: %.3fsn",(float)(timerEnd-timerStart)/1000.0);
printf("nWriting to file pi.txt ... ...");
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);
}
printf("nFinished! press any key to exitn");
}
计算e的程序:
N equ 2505
DATA SEGMENT
k dd 1
divisor dw 3
DATA ENDS
STK SEGMENT PARA STACK
dw 200h dup(?)
STK ENDS
CODE SEGMENT
.386
ASSUME CS:CODE,FS:DATA,SS:STK
start: mov ax,DATA
mov fs,ax
mov bx,cs
mov ax,ds
sub bx,ax
mov ax,offset end_prog
shr ax,4
add bx,ax
inc bx
mov ah,4ah
int 21h
mov bx,(N+1)/8+1
mov ah,48h
int 21h
jnc ds_ok
jmp exit
ds_ok: mov ds,ax
mov bx,(N+1)/4+1
mov ah,48h
int 21h
jnc es_ok
jmp exit
es_ok: mov es,ax
mov word ptr ds:[2],5000
mov dword ptr es:[4],5000
mov esi,2
xor eax,eax
loop_clear: mov [esi+esi],ax
mov es:[esi*4],eax
inc esi
cmp esi,N
jbe loop_clear
mov cx,10000
loop1: xor dx,dx
mov bx,divisor
mov esi,fs:k
loop_compute:
mov ax,dx
mul cx
add ax,[esi+esi]
adc dx,0
div bx
add es:[esi*4],eax
mov [esi+esi],ax
inc esi
cmp esi,N
jbe loop_compute
mov esi,fs:k
loop_search:cmp word ptr [esi+esi],0
jz next
mov fs:k,esi
inc fs:divisor
jmp loop1
next: inc esi
cmp esi,N
jbe loop_search
mov esi,N
loop_carry: mov eax,es:[esi*4]
xor edx,edx
div ecx
mov es:[esi*4],dx
dec esi
add es:[esi*4],eax
cmp esi,1
jae loop_carry
mov dl,'2'
mov ah,2
int 21h
mov dl,'.'
mov ah,2
int 21h
mov esi,1
loop_print: xor dx,dx
mov ax,es:[esi*4]
mov cx,1000
div cx
push dx
mov dl,al
call disp
pop ax
xor dx,dx
mov cx,100
div cx
push dx
mov dl,al
call disp
pop ax
mov cl,10
div cl
push ax
mov dl,al
call disp
pop dx
mov dl,dh
call disp
inc esi
cmp esi,N-5
jbe loop_print
exit: mov ah,4ch
int 21h
disp proc
mov ah,2
add dl,30h
int 21h
ret
disp endp
end_prog:
CODE ENDS
END start
可在DOS下进行汇编、连接��
楼主 2016-02-12 15:40 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知