x86 Processor instruction "LDS"; When this is useful?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

x86 Processor instruction "LDS"; When this is useful?

Post by osdevkid »

Hi all,

I know, what "LDS" processor command will do.

Code: Select all

ORG 100h
LDS AX, m
RET
m  DW  1234h
     DW  5678h
END
AX is set to 1234h, DS is set to 5678h.

I want to know, when we will use this command ? where it is actually required in OS development or for any other purpose.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: x86 Processor instruction "LDS"; When this is useful?

Post by xenos »

As you can see in your example, it loads a far pointer into DS:AX. So whenever you want to load a far pointer from memory, you can use this (or a similar) command.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: x86 Processor instruction "LDS"; When this is useful?

Post by osdevkid »

Thank you, I understood.
User avatar
miker00lz
Member
Member
Posts: 144
Joined: Wed Dec 08, 2010 3:16 am
Location: St. Louis, MO USA

Re: x86 Processor instruction "LDS"; When this is useful?

Post by miker00lz »

its very useful when you need to load a 16-bit:16-bit segment:offset vector from some memory location. it can be done easily with other operations, but this generally saves some CPU cycles. not very useful if you just want to store the values somewhere, but rather if you want to go access data from where it points to. LES is the same, but of course, moves the segment into ES.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: x86 Processor instruction "LDS"; When this is useful?

Post by qw »

And LDS, LES etcetera are guaranteed not te be interrupted, which is useful when setting up a stack (with LSS).
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: x86 Processor instruction "LDS"; When this is useful?

Post by bewing »

I'm going to have to disagree with the tone of the previous replies. For almost all current OS or application development, segment registers are obsolete. No application should ever modify them. In a perfect world, your OS should only need to set them once, and then never touch them again. The only exception is playtime experimental rmode OSes. LDS and its cousins are somewhat extra clever opcodes for initting segment registers. Therefore, they are almost completely worthless. As implied above, you can easily perform the same functionality with a few more "regular" opcodes, and you should almost never need to init your segment registers anyway.
User avatar
miker00lz
Member
Member
Posts: 144
Joined: Wed Dec 08, 2010 3:16 am
Location: St. Louis, MO USA

Re: x86 Processor instruction "LDS"; When this is useful?

Post by miker00lz »

bewing wrote:I'm going to have to disagree with the tone of the previous replies. For almost all current OS or application development, segment registers are obsolete. No application should ever modify them. In a perfect world, your OS should only need to set them once, and then never touch them again. The only exception is playtime experimental rmode OSes. LDS and its cousins are somewhat extra clever opcodes for initting segment registers. Therefore, they are almost completely worthless. As implied above, you can easily perform the same functionality with a few more "regular" opcodes, and you should almost never need to init your segment registers anyway.
yeah, of course. in a 32-bit OS it's worthless. it still has it's uses in stuff like simple bootloaders, etc.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: x86 Processor instruction "LDS"; When this is useful?

Post by pcmattman »

Segment registers are still used in modern operating systems - consider FS and GS which are often used for TLS on x86. The concept of segmentation for code/data separation and protection however is mostly dead.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: x86 Processor instruction "LDS"; When this is useful?

Post by jal »

Hobbes wrote:And LDS, LES etcetera are guaranteed not te be interrupted, which is useful when setting up a stack (with LSS).
There's no need for using LSS for that reason, as MOV SS disables interrupts until the next instruction (typically a MOV SP). This has been the behaviour of every x86 since the 8086/88, which didn't discourage countless compiler writers to insert a cli/sti around the pair.


JAL
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: x86 Processor instruction "LDS"; When this is useful?

Post by qw »

jal wrote:There's no need for using LSS for that reason, as MOV SS disables interrupts until the next instruction (typically a MOV SP). This has been the behaviour of every x86 since the 8086/88, which didn't discourage countless compiler writers to insert a cli/sti around the pair.
I know, but there is a small issue:
The [url=http://pdos.csail.mit.edu/6.828/2010/readings/i386.pdf]Intel 80386 Programmer's Reference Manual[/url] wrote:To prevent this situation, the 80386, after both a MOV to SS and a POP to SS instruction, inhibits NMI, INTR, debug exceptions, and single-step traps at the instruction boundary following the instruction that changes SS. Some exceptions may still occur; namely, page fault and general protection fault. Always use the 80386 LSS instruction, and the problem will not occur.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: x86 Processor instruction "LDS"; When this is useful?

Post by jal »

Hobbes wrote:Some exceptions may still occur; namely, page fault and general protection fault. Always use the 80386 LSS instruction, and the problem will not occur.
Ok, I thought you were talking about the 8086. Which, come to think of it, didn't have LSS, or did it?


JAL
Post Reply