; pi.asm - simple PI computation (9280 digits)- MS-DOS & i386 required ; ; this is original code: ; ; int a=10000,b,c=8400,d,e,f[8401],g; ; main(){for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf ("%.4d",e+d/a),e=d%a) ; for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);} ; ; (use NASM to compile) CPU 386 ITER equ 32480 BASE equ 10000 org 100h section .text ; main() ; { xor ebx, ebx mov ecx, ebx mov ebp, ebx mov bp, BASE ; for(b = 0; b < c; b++) ; { cld mov di, f + 2 mov cx, ITER push cx mov ax, BASE / 5 ; f[b] = a / 5; rep stosw ; } mov [di], bx ; f[ITER] = 0; xor edi, edi ; for(c = ITER; c > 0; c -= 14) ; { pop cx OUTER: push cx mov si, f + 2 mov bx, cx shl bx, 1 ; g = c * 2; add si, bx mov eax, edi cdq div ebp ; e = d % a; push dx ; for(b = c; b > 1; b--) ; { INNER: xor eax, eax mov ax, [si] mul ebp add eax, edi ; d += f[b] * a; dec bx ; g--; div ebx mov [si], dx ; f[b] = d % g; d /= g; dec bx ; g--; mul ecx ; d *= b; mov edi, eax dec si dec si dec cx cmp cx, byte 1 jne INNER div ebp pop bx add ax, bx mov [si], byte '\$' mov cl, 4 mov bx, 10 BINDEC: dec si cwd div bx add dl, '0' mov [si], dl loop BINDEC ; } ; printf("%.4d", e + d / a); mov ah, 9 mov dx, si int 21h pop cx sub cx, byte 14 jnz OUTER ; } int 20h ; } section .bss f resw ITER + 2