I am interested in how and why this ASM code works the way it works and how GDT fits into the picture.. I have written a very very simple C application to see what the ASM code would look like.
w/ syntax highlighting: http://paste.pocoo.org/show/344118/
Code: Select all
#ASM.C :
#
#void main() {
# int a, b;
# a = 100;
# b = a + 5;
#}
.file "asm.c"
.text
.globl main
.type main, @function
main:
pushl %ebp # push ebp to stack - back it up?
movl %esp, %ebp # copy stack pointer to base pointer - where does the stack point point now?
# what's the connection with GDT?
subl $16, %esp # decrement stack pointer by 16, allocate memory for local vars, 2x8byte (shorts?)
movl $100, -4(%ebp) # copy literal 100 to -4 base pointer (-4 relative offset of base pointer)
movl -4(%ebp), %eax # copy -4 offset to eax generic register
addl $5, %eax # add literal 5 to eax register
movl %eax, -8(%ebp) # store the result of addition to -8 relative offset of ebp
leave # what does this line and ret do to the stack? pop?
ret
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
.section .note.GNU-stack,"",@progbits
Intel manual sais:
That is simple... Segment descriptor = an entry in the GDT tables... each descriptor has a segment selector that does some technoblah blah I have no idea about O_oall memory accesses pass through either the global descriptor table (GDT) or an optional local descriptor table (LDT)
That doesn't make much sense to me.. So when I call this application, the things happen in the kernel I do not quite understand. Could someone tell me what? How does GDT fit into the picture?To access a byte in a segment, a segment selector and an offset must be supplied. The segment
selector provides access to the segment descriptor for the segment (in the GDT or LDT). From
the segment descriptor, the processor obtains the base address of the segment in the linear
address space. The offset then provides the location of the byte relative to the base address.