' C-optimized math — inline ASM for fast arithmetic
#COMPILE EXE
#DIM ALL

FUNCTION AddASM(BYVAL a AS LONG, BYVAL b AS LONG) AS LONG
'     #ASM
'         mov eax, ecx
'         add eax, edx
'         leave
'         ret
'     #ENDASM
END FUNCTION

FUNCTION MulASM(BYVAL a AS LONG, BYVAL b AS LONG) AS LONG
'     #ASM
'         mov eax, ecx
'         imul eax, edx
'         leave
'         ret
'     #ENDASM
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL a AS LONG
LOCAL b AS LONG
LOCAL c AS LONG
    LOCAL r AS LONG
LOCAL i AS LONG
LOCAL j AS LONG
LOCAL tmp AS LONG

    PRINT "C-Optimized Math Demo"
    PRINT "====================="
    PRINT ""

    a = 100: b = 200
    c = AddASM(a, b)
    PRINT "AddASM(100, 200) = "; c

    a = 25: b = 4
    c = MulASM(a, b)
    PRINT "MulASM(25, 4) = "; c

    PRINT ""
    PRINT "GCD and LCM:"
    a = 48: b = 18
    j = a: tmp = b
    WHILE tmp > 0
        i = tmp
        tmp = j MOD tmp
        j = i
    WEND
    r = j
    r = (a / r) * b
    PRINT "  LCM(48, 18) = "; r

    a = 12: b = 18
    j = a: tmp = b
    WHILE tmp > 0
        i = tmp
        tmp = j MOD tmp
        j = i
    WEND
    PRINT "  GCD(12, 18) = "; j

    PRINT ""
    PRINT "Powers of 2:"
    i = 1
    WHILE i <= 256
        PRINT "  "; i
        i = i * 2
    WEND
    PRINT ""

    PRINT ""
    PRINT "Squares table:"
    i = 1
    WHILE i <= 10
        PRINT "  "; i; "^2 = "; i * i
        i = i + 1
    WEND

    PRINT ""
    PRINT "All C-optimized math tests passed."

    FUNCTION = 0
END FUNCTION
