Area of an equilateral triangle

A program that computes the area of an equilateral triangle using the formula S = (√3/4) * a² with the coprocessor (FPU) stack.

Program source code

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtout db  "[%f] \0"
    aArray dq  2.3, 6.1, 7.7, 4.9, 3.3
    sArray dq  5 dup(0.0)
    arrlen equ ($-sArray) / 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:[aArray]       ; Load the address of the aArray variable into the ds:bx register
        lea     bp, es:[sArray]       ; Load the address of the sArray variable into the es:bp register

    @CalculateLoop:
        fld     qword ptr ds:[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
        fld1                          ; Load the number 1 into ST0. ST1 := ST0
        fadd    st(0), st(0)          ; Add ST0 to ST0 to obtain 2
        fld     st(0)                 ; Load/store the value in ST0
        fld1                          ; Load the number 1 into ST0. ST1 := ST0
        faddp   st(1), st(0)          ; Add ST1 to ST0 and pop to obtain 3
        fsqrt                         ; Take the square root of the number in ST0
        fxch    st(1)                 ; Swap the value in ST1 with the value in ST0
        fadd    st(0), st(0)          ; Add ST0 to ST0 to obtain 4
        fdivrp  st(1), st(0)          ; Divide ST1 by ST0 in reverse order and pop from the stack
        fmulp   st(1), st(0)          ; Multiply and pop ST1 by ST0
        fstp    qword ptr es:[bp+si]  ; Store and pop the value in ST0 into es:bp 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 es:[bp+si]  ; 2nd: Load the value at es:bp 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

[12.21] [85.93] [136.92] [55.44] [25.14]

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".