views:

95

answers:

3

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.

+3  A: 

db/dw means data word/data byte. This is some data, without context it could mean anything, that's why there are some comments. equ means equal, it is used to store constants. I guess gdt is defined somewhere else as the adress of/pointer to the Global Descriptor Table.

There's a GDT tutorial here that uses the same constants for a function call:

/* Setup a descriptor in the Global Descriptor Table */
void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)

[...]

/* The third entry is our Data Segment. It's EXACTLY the
 *  same as our code segment, but the descriptor type in
 *  this entry's access byte says it's a Data Segment */
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
schnaader
+2  A: 

http://en.wikibooks.org/wiki/X86_Assembly/Global_Descriptor_Table#GDT

dw and db are 'define word' and 'define byte', respectively but NOT 'define' in the c-style sense. They allocate space in memory of the size word and byte (word depends on architecture, byte is 8 bits).

josh.trow
+7  A: 

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 $-gdt sets up a value in the assembler equal to the difference between this location ($) and the gdt label. In other words, it's the offset of this entry within the GDT itself.

The structure of a GDT entry is as follows:

     alt text

where the individual parts are explained below.

     alt text

For your specific values:

(a) dw FFFFh
(b) dw 0
(c) db 0
(d) db 92h      ; present, ring 0, data, expand-up, writable
(e) db CFh      ; page-granular (4 gig limit), 32-bit
(f) db 0
  1. The base address is calculated from the f, c and b fields, from most significant to least - because these are all zero, the base is at zero.

  2. The selector limit is calculated from the rightmost 4 bits of e and all of a, to give 0xfffff in this case. This has 1 added to it to give 0x100000. See point 3 below for what this means.

  3. 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 of 0x100000 (20 bits), that gives you your full 32-bit (4G) address space.

  4. The d field is the access byte and sets the following properties based on 0x92:

    • Pr present (in-memory) bit to true.
    • Privl privelege level to 0 (need to be ring 0 to get access).
    • Ex executable bit 0 (data selector).
    • DC, direction bit is 0, segment grows up.
    • RW of 1, memory is writable.
    • Ac accessed bit set to 0.
paxdiablo
I feel like I missed something sleeping through computer architecture classes :)
josh.trow