Page 1 of 1

null selector

Posted: Fri Aug 09, 2002 10:05 am
by Krom
I have read in this forum that the null selector must be all zeroes, and i wants to say that "it is not true". You can use the null selector to other things, the most common is to point the GDT, here one example(the numbers in gdt1 and gdt2 (0xabcd and oxab) are there only to put something instead of 0):

gdt:
dw 24 ;size of gdt
dd gdt ; linear pointer to gdt
dw 0; nothing here
gdt1:
dw 0xabcd ; some data of selector
dw 0xabcd ; some data of selector
db 0xab
db 0xab
db 0xab
db 0xab
gdt2:
dw 0xabcd ; some data of selector
dw 0xabcd ; some data of selector
db 0xab
db 0xab
db 0xab
db 0xab


The "dd gdt" needs to be a linear pointer and like in the example may not be linear(because ds points to a segment diferent than 0, or because the compiler doesnt start to count at 0) and it may need that you change it by hand, if you know that it will be correct just dont chage it.

load it with

lgdt [gdt]

and you are done, and doesnt waste some bytes.

Re:null selector

Posted: Fri Aug 09, 2002 10:46 am
by crazybuddha
You are quite right. It doesn't matter what you put in the first (0) entry. This is because entry 0 won't be used by the processor.

However, eight bytes isn't such a great savings so obfuscating the code is questionable. In a trivial setting, like this, it doesn't matter one way or the other.

Re:null selector

Posted: Fri Aug 09, 2002 10:47 am
by smurphy
yes.. you are right.
but what happens if intel sudenly decides to use that area for something special? it will probably not happen, but...

why did intel put a null-descriptor their at the first place??

Re:null selector

Posted: Fri Aug 09, 2002 11:01 am
by crazybuddha
so you can zero out the segment registers (as in initializing all the registers) without causing an exception.