null selector

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Krom

null selector

Post 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.
crazybuddha

Re:null selector

Post 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.
smurphy

Re:null selector

Post 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??
crazybuddha

Re:null selector

Post by crazybuddha »

so you can zero out the segment registers (as in initializing all the registers) without causing an exception.
Post Reply