Segment Registers in 64bit

Programming, for all ages and all languages.
Post Reply
tlf30
Member
Member
Posts: 35
Joined: Fri Feb 15, 2013 9:29 pm

Segment Registers in 64bit

Post by tlf30 »

Is there any reason that ES, FS, and GS cannot be used as a random place to store data in 64bit mode?
Programming is like fishing, you must be very patient if you want to succeed.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Segment Registers in 64bit

Post 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.
tlf30
Member
Member
Posts: 35
Joined: Fri Feb 15, 2013 9:29 pm

Re: Segment Registers in 64bit

Post 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.
Programming is like fishing, you must be very patient if you want to succeed.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Segment Registers in 64bit

Post 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
Post Reply