#COMPILE EXE "test_all_mnemonics.exe"
#DIM ALL
#TOOLS OFF
#OPTIMIZE OFF
#DEBUG ERROR ON

' ======================================================================
' test_all_mnemonics.pb
' Comprehensive compile test for all 347+ x64 mnemonics in #ASM blocks.
' Each category is a separate function. Compiles with -S flag.
' ======================================================================

' -------------------------------------------------------------------
' Setup: load GPRs with known values, XMM registers, and FPU stack
' -------------------------------------------------------------------
SUB setup_registers()
    #ASM
        mov rax, 0x12345678
        mov rbx, 0x9ABCDEF0
        mov rcx, 0x11111111
        mov rdx, 0x22222222
        mov rsi, 0x33333333
        mov rdi, 0x44444444
        mov r8,  0x55555555
        mov r9,  0x66666666
        mov r10, 0x77777777
        mov r11, 0x88888888
        mov r12, 0x99999999
        mov r13, 0xAAAAAAAA
        mov r14, 0xBBBBBBBB
        mov r15, 0xCCCCCCCC

        mov rbp, rsp
        lea rax, [rsp - 0x200]
        and rax, -16
        pxor xmm0, xmm0
        pxor xmm1, xmm1
        pxor xmm2, xmm2
        pxor xmm3, xmm3
        pxor xmm4, xmm4
        pxor xmm5, xmm5
        pxor xmm6, xmm6
        pxor xmm7, xmm7
        pxor xmm8, xmm8
        pxor xmm9, xmm9
        pxor xmm10, xmm10
        pxor xmm11, xmm11
        pxor xmm12, xmm12
        pxor xmm13, xmm13
        pxor xmm14, xmm14
        pxor xmm15, xmm15
    #ENDASM
END SUB

' ===============================================================
' Category 1: Simple / No-Operand Instructions (28 mnemonics)
' ===============================================================
FUNCTION test_simple_noops() AS LONG
    setup_registers
    #ASM
        nop
        int3
        leave
        cqo
        cdq
        cdqe
        cwde
        cwd
        cbw
        lahf
        sahf
        cmc
        mfence
        lfence
        sfence
        pushfq
        popfq
        clc
        stc
        cld
        std
        cpuid
        rdtsc
        syscall
        ud2
        emms
    #ENDASM
    FUNCTION = 0
END FUNCTION

' Use ret in its own block so it's the last instruction
FUNCTION test_ret() AS LONG
    #ASM
        ret
    #ENDASM
    FUNCTION = 0
END FUNCTION

' bswap needs a register operand
FUNCTION test_bswap() AS LONG
    #ASM
        bswap rax
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 2: Data Movement (9 mnemonics)
' ===============================================================
FUNCTION test_data_mov() AS LONG
    setup_registers
    #ASM
        mov rax, rbx
        movsxd rax, ecx
        lea rax, [rbx + rcx]
        xchg rax, rbx
        push rax
        pop rbx
        movnti DWORD PTR [rdi], eax
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_movzx() AS LONG
    #ASM
        movzx eax, BYTE PTR [rcx]
        movzx eax, WORD PTR [rcx]
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_movsx() AS LONG
    #ASM
        movsx rax, BYTE PTR [rcx]
        movsx rax, WORD PTR [rcx]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 3: Arithmetic (17 mnemonics)
' ===============================================================
FUNCTION test_arithmetic() AS LONG
    setup_registers
    #ASM
        add rax, rbx
        sub rax, rbx
        and rax, rbx
        or rax, rbx
        xor rax, rbx
        cmp rax, rbx
        test rax, rbx
        imul rax, rbx
        mul rbx
        div rbx
        idiv rbx
        inc rax
        dec rax
        not rax
        neg rax
        adc rax, rbx
        sbb rax, rbx
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 4: Shifts and Rotates (7 mnemonics)
' ===============================================================
FUNCTION test_shifts() AS LONG
    setup_registers
    #ASM
        shl rax, 8
        shr rax, 8
        sar rax, 8
        rol rax, 8
        ror rax, 8
        rcl rax, 1
        rcr rax, 1
    #ENDASM
    FUNCTION = 0
END FUNCTION

' Shift/rotate by CL
FUNCTION test_shifts_cl() AS LONG
    setup_registers
    #ASM
        shl rax, cl
        shr rax, cl
        sar rax, cl
        rol rax, cl
        ror rax, cl
        rcl rax, cl
        rcr rax, cl
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 5: Bit Manipulation (9 mnemonics)
' ===============================================================
FUNCTION test_bit_manip() AS LONG
    setup_registers
    #ASM
        bt rax, 3
        bts rax, 3
        btr rax, 3
        btc rax, 3
        bsf rbx, rax
        bsr rbx, rax
        popcnt rbx, rax
        tzcnt rbx, rax
        lzcnt rbx, rax
    #ENDASM
    FUNCTION = 0
END FUNCTION

' Bit test with register index
FUNCTION test_bit_manip_reg() AS LONG
    setup_registers
    #ASM
        bt rax, rbx
        bts rax, rbx
        btr rax, rbx
        btc rax, rbx
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 6: Conditional Set (16 mnemonics)
' ===============================================================
FUNCTION test_cond_set() AS LONG
    setup_registers
    #ASM
        seto BYTE PTR [rdi]
        setno BYTE PTR [rdi]
        setb BYTE PTR [rdi]
        setae BYTE PTR [rdi]
        sete BYTE PTR [rdi]
        setne BYTE PTR [rdi]
        setbe BYTE PTR [rdi]
        seta BYTE PTR [rdi]
        sets BYTE PTR [rdi]
        setns BYTE PTR [rdi]
        setp BYTE PTR [rdi]
        setnp BYTE PTR [rdi]
        setl BYTE PTR [rdi]
        setge BYTE PTR [rdi]
        setle BYTE PTR [rdi]
        setg BYTE PTR [rdi]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 7: Conditional Move (16 mnemonics)
' ===============================================================
FUNCTION test_cond_mov() AS LONG
    setup_registers
    #ASM
        cmovo rax, rbx
        cmovno rax, rbx
        cmovb rax, rbx
        cmovae rax, rbx
        cmove rax, rbx
        cmovne rax, rbx
        cmovbe rax, rbx
        cmova rax, rbx
        cmovs rax, rbx
        cmovns rax, rbx
        cmovp rax, rbx
        cmovnp rax, rbx
        cmovl rax, rbx
        cmovge rax, rbx
        cmovle rax, rbx
        cmovg rax, rbx
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 8: Atomic / Synchronization (6 mnemonics)
' ===============================================================
FUNCTION test_atomic() AS LONG
    setup_registers
    #ASM
        cmpxchg DWORD PTR [rdi], eax
        xadd DWORD PTR [rdi], eax
        rdrand eax
        rdseed eax
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_cmpxchg8b() AS LONG
    setup_registers
    #ASM
        cmpxchg8b QWORD PTR [rdi]
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_cmpxchg16b() AS LONG
    setup_registers
    #ASM
        cmpxchg16b [rdi]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 9: SSE Scalar - Double (11 mnemonics)
' ===============================================================
FUNCTION test_sse_scalar_double() AS LONG
    setup_registers
    #ASM
        movsd xmm0, QWORD PTR [rcx]
        movsd QWORD PTR [rdi], xmm0
        addsd xmm0, xmm1
        mulsd xmm0, xmm1
        subsd xmm0, xmm1
        divsd xmm0, xmm1
        ucomisd xmm0, xmm1
        comisd xmm0, xmm1
        cvtsi2sd xmm0, rax
        cvtsd2si rax, xmm0
        cvttsd2si rax, xmm0
        cmpsd xmm0, xmm1, 0
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 9b: SSE Scalar - Single (8 mnemonics)
' ===============================================================
FUNCTION test_sse_scalar_single() AS LONG
    setup_registers
    #ASM
        movss xmm0, DWORD PTR [rcx]
        movss DWORD PTR [rdi], xmm0
        comiss xmm0, xmm1
        rcpss xmm0, xmm1
        rsqrtss xmm0, xmm1
        cvtss2sd xmm0, xmm1
        cvtsd2ss xmm0, xmm1
        cvttss2si rax, xmm0
        cmpss xmm0, xmm1, 0
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 10: SSE Packed Single (18 mnemonics)
' ===============================================================
FUNCTION test_sse_packed_single() AS LONG
    setup_registers
    #ASM
        movaps xmmword ptr [rdi], xmm0
        movaps xmm0, xmmword ptr [rcx]
        movups xmm0, xmmword ptr [rcx]
        movups xmmword ptr [rdi], xmm0
        addps xmm0, xmm1
        subps xmm0, xmm1
        mulps xmm0, xmm1
        divps xmm0, xmm1
        maxps xmm0, xmm1
        minps xmm0, xmm1
        sqrtps xmm0, xmm1
        andps xmm0, xmm1
        andnps xmm0, xmm1
        orps xmm0, xmm1
        xorps xmm0, xmm1
        shufps xmm0, xmm1, 0xE4
        unpcklps xmm0, xmm1
        unpckhps xmm0, xmm1
        cmpps xmm0, xmm1, 0
        movntps [rdi], xmm0
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 11: SSE Packed Double (18 mnemonics)
' ===============================================================
FUNCTION test_sse_packed_double() AS LONG
    setup_registers
    #ASM
        movapd xmm0, xmmword ptr [rcx]
        movapd xmmword ptr [rdi], xmm0
        movupd xmm0, xmmword ptr [rcx]
        movupd xmmword ptr [rdi], xmm0
        addpd xmm0, xmm1
        subpd xmm0, xmm1
        mulpd xmm0, xmm1
        divpd xmm0, xmm1
        maxpd xmm0, xmm1
        minpd xmm0, xmm1
        sqrtpd xmm0, xmm1
        andpd xmm0, xmm1
        andnpd xmm0, xmm1
        orpd xmm0, xmm1
        xorpd xmm0, xmm1
        shufpd xmm0, xmm1, 0
        unpcklpd xmm0, xmm1
        unpckhpd xmm0, xmm1
        cmppd xmm0, xmm1, 0
        movntpd [rdi], xmm0
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 12: SSE Integer (57 mnemonics)
' ===============================================================
FUNCTION test_sse_int_mov() AS LONG
    setup_registers
    #ASM
        movd xmm0, eax
        movd eax, xmm0
        movq xmm0, rax
        movq rax, xmm0
        movdqa xmm0, xmmword ptr [rcx]
        movdqa xmmword ptr [rdi], xmm0
        movdqu xmm0, xmmword ptr [rcx]
        movdqu xmmword ptr [rdi], xmm0
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_logic() AS LONG
    setup_registers
    #ASM
        pxor xmm0, xmm1
        pand xmm0, xmm1
        por xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_arith() AS LONG
    setup_registers
    #ASM
        paddb xmm0, xmm1
        paddw xmm0, xmm1
        paddd xmm0, xmm1
        paddq xmm0, xmm1
        psubb xmm0, xmm1
        psubw xmm0, xmm1
        psubd xmm0, xmm1
        psubq xmm0, xmm1
        pmulhw xmm0, xmm1
        pmullw xmm0, xmm1
        pmulhuw xmm0, xmm1
        pmuludq xmm0, xmm1
        psadbw xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_pack_unpack() AS LONG
    setup_registers
    #ASM
        packsswb xmm0, xmm1
        packssdw xmm0, xmm1
        packuswb xmm0, xmm1
        punpcklbw xmm0, xmm1
        punpcklwd xmm0, xmm1
        punpckldq xmm0, xmm1
        punpcklqdq xmm0, xmm1
        punpckhbw xmm0, xmm1
        punpckhwd xmm0, xmm1
        punpckhdq xmm0, xmm1
        punpckhqdq xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_compare() AS LONG
    setup_registers
    #ASM
        pcmpeqb xmm0, xmm1
        pcmpeqw xmm0, xmm1
        pcmpeqd xmm0, xmm1
        pcmpgtb xmm0, xmm1
        pcmpgtw xmm0, xmm1
        pcmpgtd xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_minmax_avg() AS LONG
    setup_registers
    #ASM
        pmaxsw xmm0, xmm1
        pmaxub xmm0, xmm1
        pminsw xmm0, xmm1
        pminub xmm0, xmm1
        pavgb xmm0, xmm1
        pavgw xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_shift() AS LONG
    setup_registers
    #ASM
        psllw xmm0, xmm1
        pslld xmm0, xmm1
        psllq xmm0, xmm1
        psrlw xmm0, xmm1
        psrld xmm0, xmm1
        psrlq xmm0, xmm1
        psraw xmm0, xmm1
        psrad xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_shift_imm() AS LONG
    setup_registers
    #ASM
        psllw xmm0, 4
        pslld xmm0, 4
        psllq xmm0, 4
        psrlw xmm0, 4
        psrld xmm0, 4
        psrlq xmm0, 4
        psraw xmm0, 4
        psrad xmm0, 4
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse_int_shuffle() AS LONG
    setup_registers
    #ASM
        pshufd xmm0, xmm1, 0xE4
        pshuflw xmm0, xmm1, 0xE4
        pshufhw xmm0, xmm1, 0xE4
        pmovmskb eax, xmm0
        pinsrw xmm0, eax, 0
        pextrw eax, xmm0, 0
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 13: x87 FPU (30 mnemonics)
' ===============================================================
FUNCTION test_x87_fpu_load() AS LONG
    setup_registers
    #ASM
        fld QWORD PTR [rcx]
        fst QWORD PTR [rdi]
        fstp QWORD PTR [rdi]
        fild QWORD PTR [rcx]
        fist DWORD PTR [rdi]
        fistp QWORD PTR [rdi]
        fisttp QWORD PTR [rdi]
        fld1
        fldz
        fldpi
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_x87_fpu_arith() AS LONG
    setup_registers
    #ASM
        fadd QWORD PTR [rcx]
        fsub QWORD PTR [rcx]
        fmul QWORD PTR [rcx]
        fdiv QWORD PTR [rcx]
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_x87_fpu_stack() AS LONG
    setup_registers
    #ASM
        fadd
        fsub
        fmul
        fdiv
        fcom
        fcomp
        fcompp
        fxch
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_x87_fpu_transcendental() AS LONG
    setup_registers
    #ASM
        fabs
        fchs
        fsqrt
        fptan
        fpatan
        fsin
        fcos
        frndint
        fscale
        fwait
    #ENDASM
    FUNCTION = 0
END FUNCTION

' fbstp/fbld need special TWORD PTR memory
FUNCTION test_x87_fpu_bcd() AS LONG
    setup_registers
    #ASM
        fbld TWORD PTR [rcx]
        fbstp TWORD PTR [rdi]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 14: String Instructions (20 mnemonics)
' ===============================================================
FUNCTION test_string_mov() AS LONG
    setup_registers
    #ASM
        cld
        movsb
        movsw
        movsd
        movsq
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_string_stos() AS LONG
    setup_registers
    #ASM
        cld
        stosb
        stosw
        stosd
        stosq
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_string_lods() AS LONG
    setup_registers
    #ASM
        cld
        lodsb
        lodsw
        lodsd
        lodsq
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_string_scas() AS LONG
    setup_registers
    #ASM
        cld
        scasb
        scasw
        scasd
        scasq
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_string_cmps() AS LONG
    setup_registers
    #ASM
        cld
        cmpsb
        cmpsw
        cmpsd
        cmpsq
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 15: Conditional Jumps (16 mnemonics)
' ===============================================================
FUNCTION test_cond_jumps() AS LONG
    setup_registers
    #ASM
        je Lab_JmpTarget
        jne Lab_JmpTarget
        jb Lab_JmpTarget
        jae Lab_JmpTarget
        ja Lab_JmpTarget
        jbe Lab_JmpTarget
        jl Lab_JmpTarget
        jge Lab_JmpTarget
        jle Lab_JmpTarget
        jg Lab_JmpTarget
        js Lab_JmpTarget
        jns Lab_JmpTarget
        jp Lab_JmpTarget
        jnp Lab_JmpTarget
        jo Lab_JmpTarget
        jno Lab_JmpTarget
    Lab_JmpTarget:
        nop
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 16: Loop (3 mnemonics)
' ===============================================================
FUNCTION test_loop() AS LONG
    setup_registers
    #ASM
        mov rcx, 10
    Lab_LoopStart:
        nop
        loop Lab_LoopStart
        mov rcx, 10
    Lab_LoopeStart:
        nop
        loope Lab_LoopeStart
        mov rcx, 10
    Lab_LoopneStart:
        nop
        loopne Lab_LoopneStart
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 17: Call / Jmp (2 mnemonics)
' ===============================================================
FUNCTION test_call_jmp() AS LONG
    setup_registers
    #ASM
        call Lab_CallDest
    Lab_CallDest:
        pop rax
        jmp Lab_JmpDest
    Lab_JmpDest:
        nop
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 18: SSE3 Instructions (13 mnemonics)
' ===============================================================
FUNCTION test_sse3() AS LONG
    setup_registers
    #ASM
        haddps xmm0, xmm1
        hsubps xmm0, xmm1
        haddpd xmm0, xmm1
        hsubpd xmm0, xmm1
        lddqu xmm0, [rcx]
        movddup xmm0, xmm1
        movshdup xmm0, xmm1
        movsldup xmm0, xmm1
        addsubpd xmm0, xmm1
        addsubps xmm0, xmm1
        fisttp QWORD PTR [rdi]
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse3_monitor() AS LONG
    setup_registers
    #ASM
        monitor
        mwait
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 19: SSSE3 Instructions (15 mnemonics)
' ===============================================================
FUNCTION test_ssse3() AS LONG
    setup_registers
    #ASM
        phaddw xmm0, xmm1
        phaddd xmm0, xmm1
        phaddsw xmm0, xmm1
        phsubw xmm0, xmm1
        phsubd xmm0, xmm1
        phsubsw xmm0, xmm1
        psignb xmm0, xmm1
        psignw xmm0, xmm1
        psignd xmm0, xmm1
        pabsb xmm0, xmm1
        pabsw xmm0, xmm1
        pabsd xmm0, xmm1
        palignr xmm0, xmm1, 1
        pshufb xmm0, xmm1
        pmaddubsw xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 20: SSE4.1 Instructions (52 mnemonics)
' ===============================================================
FUNCTION test_sse41_blend() AS LONG
    setup_registers
    #ASM
        blendps xmm0, xmm1, 0xF0
        blendpd xmm0, xmm1, 0x0F
        pblendw xmm0, xmm1, 0xF0
        blendvps xmm0, xmm1
        blendvpd xmm0, xmm1
        pblendvb xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_dot() AS LONG
    setup_registers
    #ASM
        dpps xmm0, xmm1, 0xF1
        dppd xmm0, xmm1, 0x01
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_extract_insert() AS LONG
    setup_registers
    #ASM
        extractps eax, xmm0, 0
        insertps xmm0, xmm1, 0x00
        pextrb eax, xmm0, 0
        pextrd eax, xmm0, 0
        pextrq rax, xmm0, 0
        pinsrb xmm0, eax, 0
        pinsrd xmm0, eax, 0
        pinsrq xmm0, rax, 0
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_mov() AS LONG
    setup_registers
    #ASM
        movntdqa xmm0, [rcx]
        ptest xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_pack() AS LONG
    setup_registers
    #ASM
        packusdw xmm0, xmm1
        mpsadbw xmm0, xmm1, 0
        phminposuw xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_minmax() AS LONG
    setup_registers
    #ASM
        pmaxsb xmm0, xmm1
        pmaxsd xmm0, xmm1
        pmaxud xmm0, xmm1
        pmaxuw xmm0, xmm1
        pminsb xmm0, xmm1
        pminsd xmm0, xmm1
        pminud xmm0, xmm1
        pminuw xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_pmov_sx() AS LONG
    setup_registers
    #ASM
        pmovsxbw xmm0, xmm1
        pmovsxbd xmm0, xmm1
        pmovsxbq xmm0, xmm1
        pmovsxwd xmm0, xmm1
        pmovsxwq xmm0, xmm1
        pmovsxdq xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_pmov_zx() AS LONG
    setup_registers
    #ASM
        pmovzxbw xmm0, xmm1
        pmovzxbd xmm0, xmm1
        pmovzxbq xmm0, xmm1
        pmovzxwd xmm0, xmm1
        pmovzxwq xmm0, xmm1
        pmovzxdq xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_sse41_pmul_round() AS LONG
    setup_registers
    #ASM
        pmuldq xmm0, xmm1
        pmulld xmm0, xmm1
        roundps xmm0, xmm1, 0x01
        roundpd xmm0, xmm1, 0x01
        roundss xmm0, xmm1, 0x01
        roundsd xmm0, xmm1, 0x01
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 21: SSE4.2 Instructions (6 mnemonics)
' ===============================================================
FUNCTION test_sse42() AS LONG
    setup_registers
    #ASM
        crc32 eax, BYTE PTR [rcx]
        crc32 eax, WORD PTR [rcx]
        crc32 eax, DWORD PTR [rcx]
        pcmpestri xmm0, xmm1, 0x0A
        pcmpestrm xmm0, xmm1, 0x0A
        pcmpistri xmm0, xmm1, 0x0A
        pcmpistrm xmm0, xmm1, 0x0A
        pcmpgtq xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 22: AES Instructions (6 mnemonics)
' ===============================================================
FUNCTION test_aes() AS LONG
    setup_registers
    #ASM
        aesenc xmm0, xmmword ptr [rcx]
        aesenclast xmm0, xmmword ptr [rcx]
        aesdec xmm0, xmmword ptr [rcx]
        aesdeclast xmm0, xmmword ptr [rcx]
        aesimc xmm0, xmmword ptr [rcx]
        aeskeygenassist xmm0, xmm1, 0x01
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 23: PCLMULQDQ (1 mnemonic)
' ===============================================================
FUNCTION test_pclmulqdq() AS LONG
    setup_registers
    #ASM
        pclmulqdq xmm0, xmm1, 0x01
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 24: BMI1/BMI2 Instructions (13 mnemonics)
' ===============================================================
FUNCTION test_bmi_andn() AS LONG
    setup_registers
    #ASM
        andn rax, rbx, rcx
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_bmi_bextr() AS LONG
    setup_registers
    #ASM
        bextr rax, rcx, rbx
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_bmi_blsi() AS LONG
    setup_registers
    #ASM
        blsi rax, rcx
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_bmi_blsmsk() AS LONG
    setup_registers
    #ASM
        blsmsk rax, rcx
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_bmi_blsr() AS LONG
    setup_registers
    #ASM
        blsr rax, rcx
    #ENDASM
    FUNCTION = 0
END FUNCTION

FUNCTION test_bmi2() AS LONG
    setup_registers
    #ASM
        bzhi rax, rcx, rdx
        mulx rax, rcx, rbx
        pdep rax, rcx, rdx
        pext rax, rcx, rdx
        rorx rax, rbx, 4
        sarx rax, rcx, rdx
        shlx rax, rcx, rdx
        shrx rax, rcx, rdx
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 25: MOVBE / ADCX / ADOX (4 mnemonics)
' ===============================================================
FUNCTION test_movbe_adcx() AS LONG
    setup_registers
    #ASM
        movbe eax, DWORD PTR [rcx]
        movbe DWORD PTR [rdi], eax
        adcx rax, rbx
        adox rax, rbx
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 26: XSAVE / XRSTOR (5 mnemonics)
' ===============================================================
FUNCTION test_xsave() AS LONG
    setup_registers
    #ASM
        xsave [rdi]
        xsaveopt [rdi]
        xsavec [rdi]
        xrstor [rcx]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 27: CLFLUSH / PREFETCH (7 mnemonics)
' ===============================================================
FUNCTION test_cache_control() AS LONG
    setup_registers
    #ASM
        clflush [rcx]
        clflushopt [rcx]
        prefetcht0 [rcx]
        prefetcht1 [rcx]
        prefetcht2 [rcx]
        prefetchnta [rcx]
        prefetchw [rcx]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 28: MASKMOVDQU / LDDQU (2 mnemonics)
' ===============================================================
FUNCTION test_maskmov() AS LONG
    setup_registers
    #ASM
        maskmovdqu xmm0, xmm1
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 29: F16C (2 mnemonics)
' ===============================================================
FUNCTION test_f16c() AS LONG
    setup_registers
    #ASM
        vcvtph2ps xmm0, xmmword ptr [rcx]
        vcvtps2ph xmmword ptr [rdi], xmm0, 0
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' Category 30: FXRSTOR / FXSAVE (2 mnemonics)
' ===============================================================
FUNCTION test_fxsave() AS LONG
    setup_registers
    #ASM
        fxsave [rdi]
        fxrstor [rcx]
    #ENDASM
    FUNCTION = 0
END FUNCTION

' ===============================================================
' MAIN: Run all tests
' ===============================================================
FUNCTION PBMAIN() AS LONG
    LOCAL result AS LONG
    LOCAL failures AS LONG
    result = 0

    ' Category 1
    result = test_simple_noops()
    IF result <> 0 THEN INCR failures

    ' ret in its own test
    result = test_ret()
    IF result <> 0 THEN INCR failures

    result = test_bswap()
    IF result <> 0 THEN INCR failures

    ' Category 2
    result = test_data_mov()
    IF result <> 0 THEN INCR failures
    result = test_movzx()
    IF result <> 0 THEN INCR failures
    result = test_movsx()
    IF result <> 0 THEN INCR failures

    ' Category 3
    result = test_arithmetic()
    IF result <> 0 THEN INCR failures

    ' Category 4
    result = test_shifts()
    IF result <> 0 THEN INCR failures
    result = test_shifts_cl()
    IF result <> 0 THEN INCR failures

    ' Category 5
    result = test_bit_manip()
    IF result <> 0 THEN INCR failures
    result = test_bit_manip_reg()
    IF result <> 0 THEN INCR failures

    ' Category 6
    result = test_cond_set()
    IF result <> 0 THEN INCR failures

    ' Category 7
    result = test_cond_mov()
    IF result <> 0 THEN INCR failures

    ' Category 8
    result = test_atomic()
    IF result <> 0 THEN INCR failures
    result = test_cmpxchg8b()
    IF result <> 0 THEN INCR failures
    result = test_cmpxchg16b()
    IF result <> 0 THEN INCR failures

    ' Category 9
    result = test_sse_scalar_double()
    IF result <> 0 THEN INCR failures
    result = test_sse_scalar_single()
    IF result <> 0 THEN INCR failures

    ' Category 10
    result = test_sse_packed_single()
    IF result <> 0 THEN INCR failures

    ' Category 11
    result = test_sse_packed_double()
    IF result <> 0 THEN INCR failures

    ' Category 12
    result = test_sse_int_mov()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_logic()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_arith()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_pack_unpack()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_compare()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_minmax_avg()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_shift()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_shift_imm()
    IF result <> 0 THEN INCR failures
    result = test_sse_int_shuffle()
    IF result <> 0 THEN INCR failures

    ' Category 13
    result = test_x87_fpu_load()
    IF result <> 0 THEN INCR failures
    result = test_x87_fpu_arith()
    IF result <> 0 THEN INCR failures
    result = test_x87_fpu_stack()
    IF result <> 0 THEN INCR failures
    result = test_x87_fpu_transcendental()
    IF result <> 0 THEN INCR failures
    result = test_x87_fpu_bcd()
    IF result <> 0 THEN INCR failures

    ' Category 14
    result = test_string_mov()
    IF result <> 0 THEN INCR failures
    result = test_string_stos()
    IF result <> 0 THEN INCR failures
    result = test_string_lods()
    IF result <> 0 THEN INCR failures
    result = test_string_scas()
    IF result <> 0 THEN INCR failures
    result = test_string_cmps()
    IF result <> 0 THEN INCR failures

    ' Category 15
    result = test_cond_jumps()
    IF result <> 0 THEN INCR failures

    ' Category 16
    result = test_loop()
    IF result <> 0 THEN INCR failures

    ' Category 17
    result = test_call_jmp()
    IF result <> 0 THEN INCR failures

    ' Category 18
    result = test_sse3()
    IF result <> 0 THEN INCR failures
    result = test_sse3_monitor()
    IF result <> 0 THEN INCR failures

    ' Category 19
    result = test_ssse3()
    IF result <> 0 THEN INCR failures

    ' Category 20
    result = test_sse41_blend()
    IF result <> 0 THEN INCR failures
    result = test_sse41_dot()
    IF result <> 0 THEN INCR failures
    result = test_sse41_extract_insert()
    IF result <> 0 THEN INCR failures
    result = test_sse41_mov()
    IF result <> 0 THEN INCR failures
    result = test_sse41_pack()
    IF result <> 0 THEN INCR failures
    result = test_sse41_minmax()
    IF result <> 0 THEN INCR failures
    result = test_sse41_pmov_sx()
    IF result <> 0 THEN INCR failures
    result = test_sse41_pmov_zx()
    IF result <> 0 THEN INCR failures
    result = test_sse41_pmul_round()
    IF result <> 0 THEN INCR failures

    ' Category 21
    result = test_sse42()
    IF result <> 0 THEN INCR failures

    ' Category 22
    result = test_aes()
    IF result <> 0 THEN INCR failures

    ' Category 23
    result = test_pclmulqdq()
    IF result <> 0 THEN INCR failures

    ' Category 24
    result = test_bmi_andn()
    IF result <> 0 THEN INCR failures
    result = test_bmi_bextr()
    IF result <> 0 THEN INCR failures
    result = test_bmi_blsi()
    IF result <> 0 THEN INCR failures
    result = test_bmi_blsmsk()
    IF result <> 0 THEN INCR failures
    result = test_bmi_blsr()
    IF result <> 0 THEN INCR failures
    result = test_bmi2()
    IF result <> 0 THEN INCR failures

    ' Category 25
    result = test_movbe_adcx()
    IF result <> 0 THEN INCR failures

    ' Category 26
    result = test_xsave()
    IF result <> 0 THEN INCR failures

    ' Category 27
    result = test_cache_control()
    IF result <> 0 THEN INCR failures

    ' Category 28
    result = test_maskmov()
    IF result <> 0 THEN INCR failures

    ' Category 29
    result = test_f16c()
    IF result <> 0 THEN INCR failures

    ' Category 30
    result = test_fxsave()
    IF result <> 0 THEN INCR failures

    IF failures = 0 THEN
        PRINT "ALL ASM MNEMONIC TESTS PASSED"
        FUNCTION = 0
    ELSE
        PRINT "SOME ASM MNEMONIC TESTS FAILED: "; failures
        FUNCTION = 1
    END IF
END FUNCTION
