Search code examples
delphix86inline-assemblyfastmm

GetThreadID in assembly


I read the source code of FastMM4, and notice this interesting function

function GetThreadID: Cardinal;
{$ifdef 32Bit}
asm
  mov eax, FS:[$24]
end;
{$else}
begin
  Result := GetCurrentThreadID;
end;
{$endif}

I've tested it, and it works, so my question is any explanation why it works?


Solution

  • The x86 register FS points to the Thread Information Block in Windows. The value in TIB at address FS+0x24 contains ID of the current thread. By moving the value to eax, which is used to pass the function return value, GetThreadID returns the current thread ID.