' Fast Memory Copy demo — pure PB with inline ASM memory operations
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL src1 AS LONG, src2 AS LONG, src3 AS LONG, src4 AS LONG
    LOCAL dst1 AS LONG, dst2 AS LONG, dst3 AS LONG, dst4 AS LONG
    LOCAL a AS LONG, b AS LONG, c AS LONG, d AS LONG
    LOCAL i AS LONG, err AS LONG

    PRINT "Fast Memory Copy Demo"
    PRINT "====================="
    PRINT ""

    src1 = 10: src2 = 20: src3 = 30: src4 = 40
    dst1 = 0: dst2 = 0: dst3 = 0: dst4 = 0

    PRINT "Copy with ASM mov..."
    #ASM
        mov eax, %src1
        mov %dst1, eax
        mov eax, %src2
        mov %dst2, eax
        mov eax, %src3
        mov %dst3, eax
        mov eax, %src4
        mov %dst4, eax
    #ENDASM

    PRINT "  dst values: "; dst1; " "; dst2; " "; dst3; " "; dst4

    IF dst1 <> 10 OR dst2 <> 20 OR dst3 <> 30 OR dst4 <> 40 THEN
        PRINT "  COPY FAILED!"
        INCR err
    ELSE
        PRINT "  Copy OK!"
    END IF

    dst1 = 0: dst2 = 0: dst3 = 0: dst4 = 0

    PRINT ""
    PRINT "Zero with ASM xor..."
    #ASM
        xor eax, eax
        mov %dst1, eax
        mov %dst2, eax
        mov %dst3, eax
    #ENDASM

    PRINT "  dst values: "; dst1; " "; dst2; " "; dst3; " "; dst4

    a = 100: b = 200
    PRINT ""
    PRINT "ASM add operation..."
    #ASM
        mov eax, %a
        add eax, %b
        mov %c, eax
    #ENDASM
    PRINT "  a + b = "; c

    PRINT ""
    PRINT "ASM swap via xchg..."
    a = 5: b = 9
    PRINT "  Before: a="; a; " b="; b
    #ASM
        mov eax, %a
        xchg eax, %b
        mov %a, eax
    #ENDASM
    PRINT "  After:  a="; a; " b="; b

    PRINT ""
    PRINT "Byte pointer operations..."
    LOCAL baseVal AS LONG
    LOCAL byte0 AS LONG, byte1 AS LONG, byte2 AS LONG, byte3 AS LONG
    baseVal = &H12345678

    #ASM
        mov eax, %baseVal
        mov byte ptr [%byte0], al
        mov byte ptr [%byte1], ah
        bswap eax
        mov byte ptr [%byte3], al
        mov byte ptr [%byte2], ah
    #ENDASM

    PRINT "  Long "; HEX$(baseVal, 8); " bytes: "
    PRINT HEX$(byte0, 2); " "; HEX$(byte1, 2); " "
    PRINT HEX$(byte2, 2); " "; HEX$(byte3, 2)

    PRINT ""
    IF err = 0 THEN
        PRINT "All tests PASSED."
    ELSE
        PRINT "Tests FAILED with "; err; " error(s)."
    END IF

    FUNCTION = err
END FUNCTION
