Page 1 of 1

value of CS at compile time

Posted: Sat Feb 22, 2003 10:50 pm
by Mr_Spam
how do you know what the value of CS is at compile time?

Re:value of CS at compile time

Posted: Sun Feb 23, 2003 8:54 am
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 :)

Re:value of CS at compile time

Posted: Sun Feb 23, 2003 10:21 am
by Mr_Spam
the larger problem is what do I place in the selector word of the IDT?

Re:value of CS at compile time

Posted: Sun Feb 23, 2003 10:34 am
by Pype.Clicker
you should know where is your code segment in the GDT. just take that selector.

Re:value of CS at compile time

Posted: Sun Feb 23, 2003 1:10 pm
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

Re:value of CS at compile time

Posted: Sun Feb 23, 2003 1:19 pm
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.

Re:value of CS at compile time

Posted: Mon Feb 24, 2003 1:10 am
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.