# ==================================================================
# RARS 64-bit mode
# to set RARS to 64-bit mode: settings -> 64 bit
# ==================================================================
#
# Using FCLASS.S (Classify Floating-Point Number)
#
# If you want to specifically test negative normal numbers,
# negative subnormal numbers, or negative zero, you can use the
# class instruction.
# a. Instruction: fclass.s rd, rs1
# b. How it works: It inspects rs1 and writes a 10-bit bitmask to
#    integer register rd.
# c. Bit positions for negative values in rd:
#        Bit 0: Negative infinity         (-inf)
#        Bit 1: Negative normal number    (-normal)
#        Bit 2: Negative subnormal number (-subnormal)
#        Bit 3: Negative zero (-0.0)      (-0)
# d. example assembly
#    fclass.s x5, fa0      # Get classification bitmask of fa0
#    andi x5, x5, 15       # Check if any of the last 4 bits are set
#    bnez x5, is_negative  # Branch if negative
#
# ==================================================================

.data
.align 8
pi:       .double 3.141592653589793
negative: .asciz "number is negative\n"
positive: .asciz "number is positive\n"

.text
.globl main
main:
    # ---- load pi into a register
    la       t0,  pi           # load address of pi
    fld      ft0, 0(t0)        # load pi into register

    # ---- test if (double) is negative
    fclass.d t0,  ft0          # test number
    andi     t0,  t0, 15       # mask test bits (0b1111)  
    bnez     t0,  is_negative  # branch if negative

    # ---- number is positive
    li       a7,  4            # syscall number, print string
    la       a0,  positive     # string address
    ecall                      # syscall
    j        exit_program      # jump to exit program

    # ---- number is negative
is_negative:
    li       a7,  4            # syscall number, print string
    la       a0,  negative     # string address
    ecall                      # syscall 

    # ---- exit program
exit_program:
    li       a7,  93           # syscall number, exit program
    mv       a0,  t0           # retuned value
    ecall                      # syscall