├── .gitignore ├── LICENSE ├── Makefile ├── README ├── e.bat ├── pi.asm ├── pi.com ├── pi.img └── pi.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Oscar Toledo G. http://nanochess.org/ 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile contributed by jtsiomb 2 | 3 | src = pi.asm 4 | 5 | .PHONY: all 6 | all: pi.img pi.com 7 | 8 | pi.img: $(src) 9 | nasm -f bin -o $@ $(src) 10 | 11 | pi.com: $(src) 12 | nasm -f bin -o $@ -Dcom_file=1 $(src) 13 | 14 | .PHONY: clean 15 | clean: 16 | $(RM) pi.img pi.com 17 | 18 | .PHONY: rundosbox 19 | rundosbox: pi.com 20 | dosbox $< 21 | 22 | .PHONY: runqemu 23 | runqemu: pi.img 24 | qemu-system-i386 -fda pi.img 25 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | PI calculator in 146 bytes (boot sector or COM file) 2 | by Oscar Toledo G. Nov/11/2021 3 | 4 | http://nanochess.org 5 | https://github.com/nanochess 6 | 7 | This is a simple program to calculate the PI digits. It can be 8 | run as a COM file or put into a boot sector of a floppy disk to be 9 | run. 10 | 11 | It's compatible with 8088 (the original IBM PC), and it will take 12 | a lot of time on a real hardware! ;) 13 | 14 | If you want to assemble it, you must download the Netwide Assembler 15 | (nasm) from www.nasm.us 16 | 17 | Use this command line: 18 | 19 | nasm -f bin pi.asm -Dcom_file=1 -o pi.com 20 | nasm -f bin pi.asm -Dcom_file=0 -o pi.img 21 | 22 | Tested with VirtualBox for Mac OS X running Windows XP running this 23 | program, it also works with DosBox and probably with qemu: 24 | 25 | qemu-system-x86_64 -fda pi.img 26 | 27 | Enjoy it! 28 | 29 | 30 | >> THE BOOK << 31 | 32 | Do you would like to learn 8086/8088 assembly language? Then get 33 | my book Programming Boot Sector Games and you'll also find a 34 | 8086/8088 crash course! 35 | 36 | Now available from Lulu: 37 | 38 | Soft-cover 39 | http://www.lulu.com/shop/oscar-toledo-gutierrez/programming-boot-sector-games/paperback/product-24188564.html 40 | 41 | Hard-cover 42 | http://www.lulu.com/shop/oscar-toledo-gutierrez/programming-boot-sector-games/hardcover/product-24188530.html 43 | 44 | eBook 45 | https://nanochess.org/store.html 46 | 47 | These are some of the example programs documented profusely 48 | in the book: 49 | 50 | * Guess the number. 51 | * Tic-Tac-Toe game. 52 | * Text graphics. 53 | * Mandelbrot set. 54 | * F-Bird game. 55 | * Invaders game. 56 | * Pillman game. 57 | * Toledo Atomchess. 58 | * bootBASIC language. 59 | -------------------------------------------------------------------------------- /e.bat: -------------------------------------------------------------------------------- 1 | nasm -f bin pi.asm -o pi.img 2 | nasm -f bin pi.asm -Dcom_file=1 -o pi.com 3 | 4 | -------------------------------------------------------------------------------- /pi.asm: -------------------------------------------------------------------------------- 1 | ; 2 | ; Calculate PI 3 | ; 4 | ; by Oscar Toledo G. 5 | ; 6 | ; Creation date: Nov/11/2021. 7 | ; 8 | 9 | ; Based on http://www.codecodex.com/wiki/Calculate_digits_of_pi#C 10 | ; See also https://atariage.com/forums/topic/250321-calculate-pi-and-do-something/ 11 | ; Matched with https://mathshistory.st-andrews.ac.uk/HistTopics/2000_places/ 12 | 13 | cpu 8086 14 | 15 | %ifndef com_file ; If not defined create a boot sector 16 | com_file: equ 0 17 | %endif 18 | 19 | %if com_file 20 | org 0x0100 ; Start address for COM file 21 | %else 22 | org 0x7c00 ; Start address for boot sector 23 | %endif 24 | 25 | scale: equ 100 26 | arrinit: equ scale/5 27 | digits: equ 14000-14 ; It will generate (14000-14)/14*2 digits 28 | ; Cannot go further with current precision 29 | 30 | arr: equ $+0x0200 31 | 32 | start: 33 | mov ax,0x0002 ; Set text mode 80x25 and clear screen 34 | int 0x10 ; Call BIOS 35 | cld ; Reset direction flag 36 | push cs ; Copy Code Segment to Data and Extended Segment 37 | pop ds 38 | push cs 39 | pop es 40 | 41 | mov di,arr ; Point to arr 42 | mov cx,digits+1 ; Total digits plus one 43 | mov ax,arrinit ; Starting value 44 | rep stosw ; Reset array 45 | 46 | xor si,si ; Carry 47 | mov bx,digits 48 | m1: push bx 49 | xor ax,ax ; #sum = 0 50 | ; j = #i (uses again bx) 51 | m2: mul bx ; #sum = #sum * j 52 | push bx 53 | push dx 54 | push ax 55 | shl bx,1 56 | mov ax,[bx+arr] ; scale * arr[j] 57 | mov cx,scale 58 | mul cx 59 | pop bx 60 | add ax,bx ; Add both 61 | pop bx 62 | adc dx,bx 63 | pop bx 64 | mov cx,bx ; (j * 2 - 1) 65 | shl cx,1 66 | dec cx 67 | div cx 68 | push bx 69 | shl bx,1 70 | mov [bx+arr],dx ; arr(j) = #sum % (j * 2 - 1) 71 | ; ax contains the division result. 72 | pop bx 73 | dec bx 74 | jne m2 75 | pop bx 76 | mov cx,scale 77 | mov dx,0 78 | div cx 79 | add ax,si ; carry + #sum / scale 80 | push dx ; #sum % scale 81 | mov ah,0 82 | mov dl,10 83 | div dl 84 | add al,0x30 ; Left digit 85 | cmp al,0x3a 86 | jne m3 87 | sub al,0x0a 88 | m3: 89 | call output 90 | cmp bx,digits 91 | jne m4 92 | mov al,'.' ; The start 3 is generated, but insert a period 93 | call output 94 | m4: 95 | mov al,ah 96 | add al,0x30 ; Right digit 97 | call output 98 | pop si ; carry = #sum % scale 99 | sub bx,14 100 | cmp bx,1 101 | jge m1 102 | 103 | mov ah,0x00 ; Wait for a key 104 | int 0x16 105 | int 0x20 ; Exit 106 | 107 | ; 108 | ; Output a character to screen 109 | ; 110 | output: push ax 111 | push bx 112 | push cx 113 | push dx 114 | push si 115 | mov ah,0x0e 116 | mov bx,0x0007 117 | int 0x10 118 | pop si 119 | pop dx 120 | pop cx 121 | pop bx 122 | pop ax 123 | ret 124 | 125 | %if com_file 126 | %else 127 | times 510-($-$$) db 0x4f 128 | db 0x55,0xaa ; Make it a bootable sector 129 | %endif -------------------------------------------------------------------------------- /pi.com: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanochess/pi/c3edb38509b595573c3a04e4c07dbebffe038153/pi.com -------------------------------------------------------------------------------- /pi.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanochess/pi/c3edb38509b595573c3a04e4c07dbebffe038153/pi.img -------------------------------------------------------------------------------- /pi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanochess/pi/c3edb38509b595573c3a04e4c07dbebffe038153/pi.png --------------------------------------------------------------------------------