The MASM Forum

Miscellaneous => 16 bit DOS Programming => Topic started by: arka.sharma on October 06, 2015, 02:25:41 PM

Title: Mov instruction with segment register
Post by: arka.sharma on October 06, 2015, 02:25:41 PM

Hi All,

I have a question. What is the reason for limitation of mov instruction with segment register. For example the following instruction
Code: [Select]
mov ds, es
Regards,
Arka
Title: Re: Mov instruction with segment register
Post by: MichaelW on October 06, 2015, 09:17:46 PM
I don't have any way to test any of this, but basically I think the reason is that the segment registers are not normal registers. From the Intel manual:
Quote
If the destination operand is a segment register (DS, ES, FS, GS, or SS), the source operand must be a valid
segment selector.
The manual continues with what happens in protected mode:
Quote
In protected mode, moving a segment selector into a segment register automatically causes the segment descriptor information associated with that segment selector to be loaded into the hidden (shadow) part of the segment register. While loading this information, the segment selector and segment descriptor information is validated (see the “Operation” algorithm below). The segment descriptor data is obtained from the GDT or LDT entry for the specified segment selector.
But nothing about real mode.

And regarding CS:
Quote
The MOV instruction cannot be used to load the CS register. Attempting to do so results in an invalid opcode exception (#UD). To load the CS register, use the far JMP, CALL, or RET instruction.

Title: Re: Mov instruction with segment register
Post by: BlueMR2 on October 07, 2015, 12:55:41 AM
The reason, well, as far as design, you'd have to ask the Intel engineers to be sure.  Practically speaking, the design has "sregs" in a different class from the standard "r/m" encoding.  See http://ref.x86asm.net/coder32.html.  My guess on their intentions is that it's not something you'd normally do tons of and the bits/transistor budget was better spent elsewhere.
Title: Re: Mov instruction with segment register
Post by: dedndave on October 07, 2015, 07:21:55 AM
we used to see....
Code: [Select]
    push    es
    pop     ds
to get around that one

also note that you cannot load a segment register with an immediate constant
you can, however, load it with a variable

Code: [Select]
wSeg dw 40h

    mov     es,wSeg