The RoundFloor, RoundCeil and RoundNearest functions

These functions round the floating-point number on the top of the math coprocessor stack (ST(0)) according to the specified rounding mode: RoundFloor (down), RoundCeil (up), RoundNearest (to the nearest integer).

Parameters: ST(0): floating-point number on the top of the FPU stack.

Return value: The rounding result is stored in ST(0).

Flag state: The function does not modify the flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the floating-point rounding functions.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fmtstr  db "Result: %d\0"
    number1 dq 45.3
    number2 dq 213.3
    result  dw ?
data ends

code segment public                 ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc            ; Include the file with functions

    main proc                       ; Start of the main procedure
        call    SetDataSegment      ; Call the SetDataSegment procedure

        fld     qword ptr[number1]  ; Load number1 onto the top of the math coprocessor stack
        fadd    qword ptr[number2]  ; Add the variable number2 to number1 (st(0))
        call    RoundFloor          ; Call the RoundFloor procedure (st(0))
        fistp   word ptr[result]    ; Store the value in the variable result and pop it off the stack

        push    result              ; 2nd: Save the value of the variable result on the stack
        lea     ax, ds:[fmtstr]     ; 1st: Load the address of the variable fmtstr into the ds:ax register
        call    PrintFormatString   ; Call the PrintFormatString procedure (ax:fmtstr, result)
        add     sp, 2               ; Align the stack

        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

Result: 258

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