News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

LDS question

Started by MajorDill, November 29, 2011, 01:14:06 PM

Previous topic - Next topic

MajorDill

This is a beginner question and may not belong in the section, so sorry in advance if it doesn't.

80x86, masm type compiler

I want to use the LDS instruction (Load Data Segment Register).  I understand the instruction,
what I don't get is how to set it up in the data area.

seg_1
   
   xDataAddr_DI    DW   Offset  xData     ;This gets me the DI register portion of the address
   xDataAddr_DS   DW   ?????????????    ;How do you put the segment address portion (without hard coding it)

xData could either be in the same segment or another segment. (I would like to know both)

Thank-you

dedndave

first of all, LDS and LES are rarely used in 32-bit code
these instructions apply primarily to 16-bit programs

there are a number of ways to define segments
i am guessing you want the program's data segment

   xDataAddr_DI    DW   Offset  xData     ;This gets me the DI register portion of the address
   xDataAddr_DS    DW   _DATA


should work

   xDataAddr_DI    DW   Offset  xData     ;This gets me the DI register portion of the address
   xDataAddr_DS    DW   seg xData


is another way (i think that's right)
in this case, it uses the segment in which xData was defined

both will assemble as "relocatable elements"
i.e., the actual value is not known until the OS loads the program and fills in the relocatable value(s)

another way for LDS/LES to be used is off the stack
the segment and offset are pushed and/or passed as parameters
then, they are retrieved from the stack, for example...
        push    SourceSeg
        push    SourceOfs
        push    DestSeg
        push    DestOfs
;
;
;
        les     di,[esp]    ;load destination address into ES:DI
        lds     si,[esp+4]  ;load source address into DS:SI


i have used these instructions to load 2 words into registers at a time
in other words, the "segment" value does not need to be a segment - just some value
if i am not using ES at the time.....
        les     ax,AddrOf2Words
        mov     dx,es

on the 8086/8088, it was the only way to load 2 words at once   :P

MichaelW

Typically, the data would be a "far" pointer, where a far pointer specifies both a segment address and an offset address. One (at one time common) usage would be to restore a "hooked" interrupt vector to its previous value, using Interrupt 21h, function 25h.


    prevIsr8  dd 0
    . . .
    ;-----------------------------------------
    ; Unhook the system timer tick interrupt.
    ;-----------------------------------------

    push ds
    lds dx, prevIsr8
    mov ax, 2508h
    int 21h
    pop ds

eschew obfuscation

sinsi

Quote from: dedndave on November 29, 2011, 01:50:22 PM
        les     di,[esp]    ;load destination address into ES:DI
        lds     si,[esp+4]  ;load source address into DS:SI
In real mode this can work but watch out if the high word of ESP is non-zero, this will cause an exception, and using [sp] is invalid.
In protected mode loading segment registers can cause problems too since they should be valid selectors.
Light travels faster than sound, that's why some people seem bright until you hear them.

MajorDill

Thanks

I kept putting an @ in front of it which the compiler didn't like.  Just putting the seg alone worked.