Code/data segments

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
AK84

Code/data segments

Post by AK84 »

Honestly Im a bit confused...I read in the Intel manual that you need at last two segments(Protected Mode).
And a tutorial said that you need the code segment where instructions are being stored, and a data segment where variables will be stored. But I dont get it, do I need
to store my variabels in a special segment and my code in another? How do I do this?
I got a working kernel, and I havent think about this. Will all my variables be
stored in the data segment automaticlly? I thought variables are stored "in the
program", or at least in the end of it.

OR is it just that when the processor gets an instruction that requires a variable, e.g. on adress 0x10, it will start to check from the data segment?s base adress for the variabel?
proxy

Re:Code/data segments

Post by proxy »

yes and no, your variables are in fact part of your program. However you oare seeming to forget an important detail..segments can and often do overlap...

For example, if you only intend to use paging, just set both segments to 0 - 4G (covering whole address space).

the reason for 2 segments is that the perm bits in a segment dont allow for read, write and execute all at once. To get the effective of all 3, just make two overlap and you are set.

proxy
AK84

Re:Code/data segments

Post by AK84 »

Thank you!
But what is the code/data segment for? And if I DONT want them to overlap eachother, how do I put my instructions in one segment and my variables in another? (cause the compiler will put the variabels it as a part of my kernel, not in a special segment?)

I mean since I need at least the code/data segment, there must be different things the computer uses them for, just that I wont notice it because they?re overlapping eachother
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Code/data segments

Post by Pype.Clicker »

a C compiler will usually separate code from the instructions and let the linker collect all the code sections together, then all all the data sections together.

You can do the very same in ASM by the use of "section" or "segment" directive (check your assembler's manual)

Code: Select all

section .data
    msg db "this is a message"
    magic dd 0xdeadbeef

section .text
    _start:
    mov edx,msg
    call _print
    cmp eax, [magic]
    je .out
    cli
    hlt
.out:
    ret
when loading your kernel, you can then either use a structured output (ELF, COFF) and inspect headers to know what size you should give to each segment or use "smart" labels (start_of_code, end_of_code) in your linker script.
Post Reply