Lods byte ptr ds:[esi] ;i know that it loads a byte that is a pointer my question is " is the pointer ds with esi as an offset or is it the other way around or am i just plain wrong" ?

Dani AI

Generated

A short, practical clarification (building on and ):

ds:[esi] means “memory at the address formed by the DS segment base plus the contents of ESI.” The assembler/CPU treats ESI as an offset; the segment register supplies the segment base (so the effective address is segment-base + ESI) and the LODS family reads from that memory into AL/AX/EAX and then updates (E)SI automatically. See the Intel instruction description for LODS. (Intel SDM — LODS reference). (studizba.com)

How that becomes a runnable address depends on CPU mode: in 16-bit real mode the CPU forms a 20-bit address by shifting the 16-bit segment value left 4 bits and adding the offset; in protected mode the segment register is a selector and the segment descriptor supplies a 32/64-bit base that is added to the offset (and if paging is enabled that linear address is then translated to a physical address). (Intel SDM — segmentation and addressing). (manuals.plus)

Defaults and overrides you should watch for: most memory operands use DS by default for index registers like (E)SI; accesses that use BP/RBP or SP/RSP default to SS. You can supply segment-override prefixes (cs:, ds:, es:, fs:, gs:, ss:) to change the chosen segment on a particular instruction; the LODS documentation notes DS can be overridden when appropriate. Also note that in 64-bit (long) mode CS/DS/ES/SS are effectively flat (base = 0) and only FS/GS carry nonzero bases for things like TLS — so many segment overrides are ignored in long mode. (studylib.net)

Little, immediately useful example (behavior controlled by the direction flag DF):

; forward (lowest->highest addresses)
cld         ; clear DF
lodsb       ; AL := byte ptr [DS:ESI], ESI += 1

; backward (highest->lowest)
std         ; set DF
lodsb       ; AL := byte ptr [DS:ESI], ESI -= 1

For authoritative detail read the LODS and segmentation sections of the Intel SDM linked above; they state the DS:(E)SI convention, the DF behavior, and the rules for segment bases/overrides. (studizba.com)

Recommended Answers

All 4 Replies

esi just contains an address that is within the ds segment. ds is not a pointer -- it contains the address of the data segment. Sometimes you might even see cs:[esi] or even ss:[esi] in which case esi is pointer to somewhere in the code segment or stack segment, respectively.

esi just contains an address that is within the ds segment. ds is not a pointer -- it contains the address of the data segment. Sometimes you might even see cs:[esi] or even ss:[esi] in which case esi is pointer to somewhere in the code segment or stack segment, respectively.

thanks AncientDragon :) you have all the answers :D

The default for a [si] [esi] [rsi] access is the ds: Data segment / selector. But it can be overriden with an alternate such as AncientDragon indicated
cs: Code Segment/Selector,
es:Extra Segment/Selector,
ss: Stack Segment/Selector
fs, gs Selectors

Another item to note is LODS also increments the index by the size of the data referenced!

Assuming 32-bit Protected Mode

lodsb
   mov al,ds:[esi]
   add esi,1
lodsw
   mov ax,ds:[esi]
   add esi,2
lodsd
   mov eax,ds:[esi]
   add esi,4

You can use the Repeat instruction rep

rep lodsd
L1: mov eax,ds:[esi]
   add esi,4
   dec ecx
   jne L1

The default for a [si] [esi] [rsi] access is the ds: Data segment / selector. But it can be overriden with an alternate such as AncientDragon indicated
cs: Code Segment/Selector,
es:Extra Segment/Selector,
ss: Stack Segment/Selector
fs, gs Selectors

Another item to note is LODS also increments the index by the size of the data referenced!

Assuming 32-bit Protected Mode

lodsb
   mov al,ds:[esi]
   add esi,1
lodsw
   mov ax,ds:[esi]
   add esi,2
lodsd
   mov eax,ds:[esi]
   add esi,4

You can use the Repeat instruction rep

rep lodsd
L1: mov eax,ds:[esi]
   add esi,4
   dec ecx
   jne L1

Thanks for the info lods is an instruction i rarely see let alone use :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.