Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get linear address of FS:[0] in 32-bit protected mode / MSVC inline asm

I used this instruction in Visual C++ inline assembly

lea eax, FS:[0]

Why did eax get a zero?

And how do I get the linear address of FS:[0]?

like image 761
Zhibo Shen Avatar asked Jan 17 '26 05:01

Zhibo Shen


2 Answers

Assuming FS points to the Windows Thread Information Block (TIB), also known as the Thread Environment Block (TEB), you get the linear address of the TIB by reading the 32-bit value at fs:[0x18]. The best way to do this in Visual C++ is to use the __readfsdword intrinsic:

TEB *teb = (TEB *) __readfsdword(0x18);
like image 164
Ross Ridge Avatar answered Jan 19 '26 17:01

Ross Ridge


The LEA instruction ("Load Effective Address") is badly named (e.g. should probably be called LEO/"Load Effective Offset") because it only calculates the offset within a segment.

like image 31
Brendan Avatar answered Jan 19 '26 18:01

Brendan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!