Area of a circle
A program that computes the area of a circle using the formula A = πr² with the coprocessor (FPU) stack.
Program source code
title .model small .stack 100h data segment public fmtout db "[%f] \0" array dq 3.40, 4.54, 8.33, 7.54 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 an index mov cx, arrlen ; Copy the array length into the cx register lea bx, ds:[array] ; Load the address of the array variable into the ds:bx register @CalculateLoop: fld qword ptr[bx+si] ; Load the value in bx at index si onto the top of the coprocessor stack fmul st(0), st(0) ; Square the value in ST0 fldpi ; Load the number PI into ST0 fmulp st(1), st(0) ; Multiply ST1 by ST0 fstp qword ptr[bx+si] ; Store and pop the value in ST0 into bx at index 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 an index mov cx, arrlen ; Copy the array length into the cx register @PrintArrayLoop: fld qword ptr[bx+si] ; 2nd: Load the value in bx at index 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
[36.31] [64.75] [217.99] [178.60]
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".