Page 1 of 1

Segment Registers in 64bit

Posted: Thu Oct 29, 2015 10:42 am
by tlf30
Is there any reason that ES, FS, and GS cannot be used as a random place to store data in 64bit mode?

Re: Segment Registers in 64bit

Posted: Thu Oct 29, 2015 10:57 am
by iansjack
Why just 64-bit mode? This would be even more useful in 32- or 16-bit modes, where you have less general-purpose registers available. But I believe that loading a segment register is a relatively expensive operation, so it may not be very efficient. And the fact that you can only write and read them makes them less useful.

You do already have an additional 8 general-purpose registers available in 64-bit mode.

Re: Segment Registers in 64bit

Posted: Thu Oct 29, 2015 11:51 am
by tlf30
I was just thinking about using them as a place to cache parts of a register during certain operations. But the fact that there is still the overhead of them in 64bit mode was not something that I thought of and is a really good reason to not use them.

Re: Segment Registers in 64bit

Posted: Fri Oct 30, 2015 3:59 am
by Antti
I have thought this idea sometimes. If the global descriptor table contained duplicate entries, you could use the index value for storing data. It is not efficient but still one option. Untested example below.

Code: Select all

; Input: al = byte
PutByteRegisterDs:
    shl ax, 8                   ; ah = input byte
    mov al, (DATA_SEGMENT)      ; ax = segment selector
    mov ds, ax
    ret

; Output: al = byte
GetByteRegisterDs:
    mov ax, ds                  ; ax = segment selector
    shr ax, 8                   ; al = return byte
    ret