Question

This is code snipper from header.S file in kernel code. I could not understand what the lretw instruction does. I've checked out so many online sources for the instruction.

# We will have entered with %cs = %ds+0x20, normalize %cs so
# it is on par with the other segments.
        pushw   %ds 
        pushw   $6f 
        lretw

Can any one help me in understanding this instruction?

Was it helpful?

Solution

ret is the instruction to return from a procedure. So basically it pops the return address from the stack into the EIP register.

the l prefix is here to tell that it is a far return from procedure. In this case, the instruction first pops a value from the stack into the EIP register and then pops a second value into the CS register.

the w suffix is here because at this step we are running in real mode, and operands are 16 bits wide.

The exact code is:

    pushw   %ds
    pushw   $6f
    lretw
6:

The 6: is very important here. So what this does is: push the value of ds into the stack, push the adress of the 6 label into the stack, and then trigger this lretw instruction. So basically, it will load the address of label 6 into the instruction pointer register, and load the cs register with the value of the ds register. So this is just a trick to continue the execution at label 6 with a change of the cs register value.

You should download http://www.intel.com/design/intarch/manuals/243191.htm which gives precise details for all instructions, including a pseudo-code that details what each instruction is doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top