Volume of a sphere
A program that computes the volume of a sphere using the formula V = (4/3)πr³ and the coprocessor (FPU) stack.
Program source code
title .model small .stack 100h data segment public fmtout db "[%f] \0" array dq 3.4, 6.1, 7.7, 4.9, 2.3 arrlen equ ($-array) / 8 data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions include Handymac.inc ; Include the file with macros main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure xor si, si ; Clear the si register, it will serve as the index mov cx, arrlen ; Copy the array length into the cx register lea bx, ds:[array] ; Load the address of the array into the ds:bx register @CalculateLoop: fld qword ptr[bx+si] ; Load the value at bx indexed by si onto the top of the coprocessor stack fst st(1) ; Save the value in ST0 into ST1 fmul st(0), st(0) ; Square the value in ST0 fmulp st(1), st(0) ; Multiply ST0 by ST1 and pop, obtaining the value cubed fldpi ; Load the value of PI into ST0 fmulp st(1), st(0) ; Multiply and pop ST1 into ST0 fld1 ; Load the number 1 into ST0. ST1 := ST0 fadd st(0), st(0) ; Add and pop ST0 with ST0 to obtain 2 fadd st(0), st(0) ; Add and pop ST0 with ST0 to obtain 4 fld1 ; Load the number 1 into ST0. ST1 := ST0 fadd st(0), st(0) ; Add and pop ST0 with ST0 to obtain 2 fld1 ; Load the number 1 into ST0. ST1 := ST0 faddp st(1), st(0) ; Add and pop ST1 with ST0 to obtain 3 fdivp st(1), st(0) ; Divide ST1 by ST0 and pop from the stack fmulp st(1), st(0) ; Multiply and pop ST1 into ST0 fstp qword ptr[bx+si] ; Store and pop the value from ST0 into bx indexed by si incq si ; Increment the si index by 8 loop @CalculateLoop ; Loop while the cx register is not equal to 0 xor si, si ; Clear the si register, it will serve as the index mov cx, arrlen ; Copy the array length into the cx register @PrintArrayLoop: fld qword ptr[bx+si] ; 2nd: load the value at bx indexed by si onto the top of the stack lea ax, ds:[fmtout] ; 1st: load the address of the fmtout format string into ax call PrintFormatString ; Call the PrintFormatString procedure incq si ; Increment the si index by 8 loop @PrintArrayLoop ; Loop while the cx register is not equal to 0 xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
[164.63] [950.77] [1912.32] [492.80] [50.96]
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".