shaz wrote:
Can u please explain the role of stack pointer .and what might happen if i don't set it.
The stack pointer (ESP) points to the end of the stack. When the CPU PUSHes a value onto the stack, it subtracts 4 from ESP then writes the value at that address. To POP a value, it read the value at ESP, then adds 4 to ESP.
C relies on the stack to pass parameters, as do most high-level languages. So before your kernel jumps to any C code, it must set aside a sufficiently large stack region and set ESP to the highest address of that region.
For example:
Code:
mov esp, stack_end
jmp some_c_code
...
stack:
resb 4096
stack_end:
Quote:
As u see in the code both code and data selectors have same base and limit.But code segment is readable and data segment is writable.
Please explain how same memory can be both readable and writable as both code and data selectors
point to same memory.
Because they both point to the same memory
. Segment protection says, "if my code accesses memory through this selector, apply this protection". If two segments point to the same memory, then you can use either protection.
However, you can't access data through a code segment, and you can't run code in a data segment, so you need at least one of each.
Quote:
Is there any possibility that i may overwrite my code as both code and data segments point to same memory.And if this possibility is true please suggest me what should i move in code and data selectors to avoid code overwriting by data.
If you really want to stop code from modifying other code, you'll either need to keep all your code and data separate and allocate segments for each (not recommended), or use paging and make your code pages read-only (recommended).