Page 1 of 1

help me with these codes.

Posted: Tue Jun 03, 2008 8:09 am
by huxuelei
Hi, I was reading the articles at http://www.jamesmolloy.co.uk/tutorial_h ... 20IDT.html.


And I saw a piece of code like this:

Code: Select all

[GLOBAL gdt_flush]    ; Allows the C code to call gdt_flush().

gdt_flush:
   mov eax, [esp+4]  ; Get the pointer to the GDT, passed as a parameter.
   lgdt [eax]        ; Load the new GDT pointer

   mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
   mov ds, ax        ; Load all data segment selectors
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   jmp 0x08:.flush   ; 0x08 is the offset to our code segment: Far jump!
.flush:
   ret 
I can not understand two lines:
1) mov eax, [esp+4]. I usually use ebp register to access a parameter in a function. But there use esp register to do this job. I can not imagine what's the current stack like at this time.

2)jmp 0x08:.flush. I do not know where the instruction will jmp to.

Can any one give me some tips?Thanks.

Posted: Tue Jun 03, 2008 8:21 am
by JamesM
Hi,

1) If you read the second article, "2. Genesis", you'd know that in _cdecl when a function is called it will find its return address at the stack pointer, and its parameters immediately above the stack pointer on the stack.

so mov eax, [esp+4] moves the second item from the stack into EAX, which is of course the function's first parameter.

Note that you can only use EBP once you've set up ebp - i.e. you need to make yourself a stack frame. This isn't needed in this function, as it's a leaf function.

2) it jumps to the label ".flush", which is declared directly below the jmp statement...

This was all explained in the tutorial text, by the way.

Cheers,

James