Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 988
justyy Avatar asked Apr 23 '13 15:04

justyy


2 Answers

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.

like image 166
Samuli Hynönen Avatar answered Nov 07 '22 02:11

Samuli Hynönen


This method uses the information stored in the Thread Environment Block

like image 21
MBo Avatar answered Nov 07 '22 02:11

MBo