At one place, the Intel documentation implies that the relationship between segment registers and general registers does not exist in flat protected mode. This seemed unreasonable to me and I suspected they were saying because all segment registers are the same it is no longer material.
Being curious I ran a little experiment that confirmed what most of you probably know - that the relationship is still there, but just not material.
The experiment - code that deposited information into a variable whose address was in ebx. Ebx instructions are coupled with seg register ds. After I had loaded the address of the variable into ebx I modified ds either (1) to be what it already was ( 0x10 pointing to a read/write segment descriptior in the gdt) or (2) to be 0x08 (which pointed to the read only code descriptor).
As you probably know (and what I learned): the code ran when I used 0x10 and bombed with 0x08.
Conclusions:
1 - there is no harm in messing with ds after you load an address via lea as long as you fix ds before it is used.
2- the relationship between general registers and segment registers does not go away in flat mode, it just becomes immaterial.
Code: Select all
; experiment
EXTERN timer_ticks ; variable in c code
; system does not complain if you put offset of a r/o segment into ds
; .. as long as you dont use it
mov eax, 0x08 ; code segment gdt offset
mov ds, eax ; bad value - did not complain
mov eax, 0x10 ; data segment offset
mov ds, eax ; fix
; write to an arbitrarily selected variable - with good or bad value in ds
lea ebx, [timer_ticks] ; addr of tt in ebx
mov eax, 0x08 ; bad data seg selector
mov eax, 0x10 ; good data seg selector - pgm runs ok unless line commented out
mov ds, eax ; make ds either bad or good, depending on the ;
mov [ebx], dword 0x1000 ; write 1000 to timer_ticks (or bomb .. and it does bomb)
; prepare to show t_t in main program to see that we did what we wanted to do
mov eax, [timer_ticks]
mov [st], eax ; st is used to display 0x1000 as diagnostic "start" at run time
; end experiment