Pages

marți, 1 decembrie 2026

FASM : Check FMA3 , OSXSAVE, XGETBV and AVX2 .

To add FMA3 and AVX2 in the same style (using write, print_yesno, and segment readable writeable), you need to check FMA3 (CPUID leaf 1, ECX bit 12). Then check OSXSAVE (CPUID leaf 1, ECX bit 27) and XGETBV (XCR0 bits 1 and 2), so that AVX, FMA3, and AVX2 are reported as YES only when the operating system saves the YMM registers. Last, check AVX2 (CPUID leaf 7, sub-leaf 0, EBX bit 5), after confirming that the maximum standard leaf (EAX from leaf 0) is at least 7.The FMA3: Executes a \times b + c in one instruction with a single rounding, boosting speed and numerical accuracy.The AVX2: Extends 256-bit YMM vector operations to integers and adds faster register permutations and gather loads.The OSXSAVE: Indicates that the OS enabled XSAVE/XGETBV support so YMM state can be managed safely.The XGETBV: Reads XCR0 to verify that the OS actually saves and restores XMM (bit 1) and YMM (bit 2) registers across context switches. Let's see source code:
format ELF64 executable 3

segment readable executable
entry main

main:
    ; Vendor: CPUID leaf 0 (EBX, EDX, ECX = 12 caractere)
    xor  eax, eax
    cpuid
    mov  dword [max_leaf], eax
    mov  dword [vendor],   ebx
    mov  dword [vendor+4], edx
    mov  dword [vendor+8], ecx

    lea  rsi, [lbl_vendor]
    mov  rdx, lbl_vendor_len
    call write
    lea  rsi, [vendor]
    mov  rdx, 12
    call write
    call newline

    ; Brand string: leaf-urile 0x80000002..0x80000004 (48 caractere)
    mov  eax, 0x80000000
    cpuid
    cmp  eax, 0x80000004
    jb   no_brand

    mov  r8d, 0x80000002
    lea  rdi, [brand]
brand_loop:
    mov  eax, r8d
    cpuid
    mov  dword [rdi],    eax
    mov  dword [rdi+4],  ebx
    mov  dword [rdi+8],  ecx
    mov  dword [rdi+12], edx
    add  rdi, 16
    inc  r8d
    cmp  r8d, 0x80000004
    jbe  brand_loop

    lea  rsi, [lbl_brand]
    mov  rdx, lbl_brand_len
    call write
    lea  rsi, [brand]
    mov  rdx, 48
    call write
    call newline
    jmp  features

no_brand:
    lea  rsi, [lbl_nobrand]
    mov  rdx, lbl_nobrand_len
    call write

features:
    ; Flaguri de baza: CPUID leaf 1 - EDX bit 26 = SSE2, ECX bit 12 = FMA3, ECX bit 28 = AVX
    mov  eax, 1
    xor  ecx, ecx
    cpuid
    mov  dword [feat_edx], edx
    mov  dword [feat_ecx], ecx

    ; Verificam daca OS-ul a activat XMM (bit 1) si YMM (bit 2) in XCR0
    xor  r8d, r8d                    ; r8d = 1 daca AVX/YMM este activ in OS, altfel 0
    bt   ecx, 27                     ; OSXSAVE
    jnc  .print_flags
    bt   ecx, 28                     ; AVX hardware
    jnc  .print_flags

    push rcx
    xor  ecx, ecx
    xgetbv                           ; EDX:EAX = XCR0
    pop  rcx
    and  eax, 0x6                    ; biții 1 (XMM) si 2 (YMM)
    cmp  eax, 0x6
    jne  .print_flags
    mov  r8d, 1

.print_flags:
    ; SSE2
    lea  rsi, [lbl_sse2]
    mov  rdx, lbl_sse2_len
    call write
    mov  eax, [feat_edx]
    shr  eax, 26
    and  eax, 1
    call print_yesno

    ; AVX (necesita suport CPU + activare OS prin XGETBV)
    lea  rsi, [lbl_avx]
    mov  rdx, lbl_avx_len
    call write
    mov  eax, r8d
    call print_yesno

    ; FMA3 (necesita ECX bit 12 in leaf 1 + YMM activ in OS)
    lea  rsi, [lbl_fma3]
    mov  rdx, lbl_fma3_len
    call write
    mov  eax, [feat_ecx]
    shr  eax, 12
    and  eax, 1
    and  eax, r8d
    call print_yesno

    ; AVX2: CPUID leaf 7, sub-leaf 0 -> EBX bit 5 (plus YMM activ in OS)
    lea  rsi, [lbl_avx2]
    mov  rdx, lbl_avx2_len
    call write

    xor  eax, eax
    cmp  dword [max_leaf], 7
    jb   .show_avx2

    mov  eax, 7
    xor  ecx, ecx
    cpuid
    shr  ebx, 5
    and  ebx, 1
    and  ebx, r8d
    mov  eax, ebx

.show_avx2:
    call print_yesno
    call exit

; eax = 1 -> "DA", altfel "NU"
print_yesno:
    test eax, eax
    jz   .no
    lea  rsi, [yes]
    mov  rdx, 3
    call write
    ret
.no:
    lea  rsi, [no]
    mov  rdx, 3
    call write
    ret

; rsi = adresa, rdx = lungime
write:
    mov  eax, 1           ; sys_write
    mov  edi, 1           ; stdout
    syscall
    ret

newline:
    lea  rsi, [nl]
    mov  rdx, 1
    call write
    ret

exit:
    mov  eax, 60          ; sys_exit
    xor  edi, edi
    syscall

segment readable writeable

max_leaf       dd 0
vendor         rb 12
brand          rb 48
feat_edx       dd 0
feat_ecx       dd 0

lbl_vendor     db "Vendor:  "
lbl_vendor_len = $ - lbl_vendor
lbl_brand      db "Model:  "
lbl_brand_len  = $ - lbl_brand
lbl_nobrand    db "Model:  necunoscut", 10
lbl_nobrand_len = $ - lbl_nobrand
lbl_sse2       db "SSE2:  "
lbl_sse2_len   = $ - lbl_sse2
lbl_avx        db "AVX:   "
lbl_avx_len    = $ - lbl_avx
lbl_fma3       db "FMA3:  "
lbl_fma3_len   = $ - lbl_fma3
lbl_avx2       db "AVX2:  "
lbl_avx2_len   = $ - lbl_avx2
yes            db "DA", 10
no             db "NU", 10
nl             db 10
The result is:
flat assembler  version 1.73.35  (16384 kilobytes memory, x64)
3 passes, 818 bytes.
Vendor:  AuthenticAMD
Model:  AMD Ryzen 5 3600 6-Core Processor              .
SSE2:  DA
AVX:   DA
FMA3:  DA
AVX2:  DA