value of CS at compile time

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
Mr_Spam

value of CS at compile time

Post by Mr_Spam »

how do you know what the value of CS is at compile time?
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:value of CS at compile time

Post by Pype.Clicker »

you don't.

if you're writing a .COM file, you just assume CS is set to the program segment by the DOS

if you're writing a real-mode .EXE file, your program might be relocated by the EXE loader, so all you can do is have mov ax,cs ; mov [my_code],ax at run time

If you're writing some pmode program (linux, windows, whatever), your model is usually flat (only one code segment) so you don't care :)
Mr_Spam

Re:value of CS at compile time

Post by Mr_Spam »

the larger problem is what do I place in the selector word of the IDT?
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:value of CS at compile time

Post by Pype.Clicker »

you should know where is your code segment in the GDT. just take that selector.
Mr_Spam

Re:value of CS at compile time

Post by Mr_Spam »

in my bootloader, my GDT looks like this:

   [bits 16]
   
   org 0x7C00
start:
   jmp boot
GDT
Null_Sel equ $-GDT      
dd 0x0
dd 0x0
OS_Code equ $-GDT   
dw 0xFFFF      ; segment limit
dw 0x0         ; base address
db 0x0         ; base
db 0x9A         ; segment data
db 0xCF
   db 0x0         ; base
OS_Data equ $-GDT   
dw 0xFFFF      ; segment limit
dw 0x0         ; base address
db 0x0         ; base
db 0x92         ; segment data
db 0xCF
   db 0x0         ; base
GDTEnd
boot:

so how would i calculate the address of OS_Code?

thanks,

Mr Spam
richie

Re:value of CS at compile time

Post by richie »

Hello!
To get the current value of CS in AX do the following instructions:

Code: Select all

push cs
pop ax
The value of cs isn't available since compile time. But if you have it in ax you can use it anywhere.
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:value of CS at compile time

Post by Pype.Clicker »

OS_code is actually computed by the assembler : $ (current_position) - GDT = 8,
so you can safely assume OSCODE==8 in your whole kernel.
Post Reply