Assembly instructions to find how many threads are enabled in a multi-core system

早过忘川 提交于 2019-11-29 04:47:38

I researched it a bit and came up with these facts. cpuid with eax = 01h returns the APIC ID in EBX[31:24] and HT enable in EDX[28].

This code should do the work:

    ; this code will put the thread id into ecx
    ; and the core id into ebx

    mov eax, 01h
    cpuid
    ; get APIC ID from EBX[31:24]
    shr ebx, 24
    and ebx, 0ffh; not really necessary but makes the code nice

    ; get HT enable bit from EDX[28]
    test edx, 010000000h
    jz ht_off

    ; HT is on
    ; bit 0 of EBX is the thread
    ; bits 7:1 are the core
    mov ecx, ebx
    and ecx, 01h
    shr ebx, 1

    jmp done

ht_off:
    ; the thread is always 0
    xor ecx, ecx

done:
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!