Can anybody explain me this piece of assembly code?
LINEAR_DATA_SEL equ $-gdt
dw 0FFFFh
dw 0
db 0
db 92h ; present, ring 0, data, expand-up, writable
db 0CFh ; page-granular (4 gig limit), 32-bit
db 0
Here I have already googled about the command equ, dw and db but I can’t understand what this code actually do(especially the first line). what is this $-gdt and the parameters of dw and db? Kindly explain in detail if possible. Thanks in advance.
It’s actually an 8-byte entry in the global descriptor table. It creates a descriptor addressing the entire 4G address space as a selector.
The
equ $-gdtsets up a value in the assembler equal to the difference between this location ($) and thegdtlabel. In other words, it’s the offset of this entry within the GDT itself.The structure of a GDT entry is as follows:
where the individual parts are explained below.
For your specific values:
The base address is calculated from the
f,candbfields, from most significant to least – because these are all zero, the base is at zero.The selector limit is calculated from the rightmost 4 bits of
eand all ofa, to give0xfffffin this case. This has 1 added to it to give0x100000. See point 3 below for what this means.The top 4 bits of
e(the flags) set the granularity (4K rather than 1 byte) and the operand size (32-bit). With a granularity of 4K (12 bits) and page count of0x100000(20 bits), that gives you your full 32-bit (4G) address space.The
dfield is the access byte and sets the following properties based on0x92:Prpresent (in-memory) bit to true.Privlprivelege level to 0 (need to be ring 0 to get access).Exexecutable bit 0 (data selector).DC, direction bit is 0, segment grows up.RWof 1, memory is writable.Acaccessed bit set to 0.