Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly commands LEA/LDS/LES [duplicate]

What is the difference between the following commands: LEA, LDS, LES? I searched for the answer but I'm not very clear on it. From what I understood block1 should be equivalent to block2.

.data
  str1 db 'My first string. $'
  str2 db 'My second string. $'
  ptr_str1 dd str1 
  ptr_str2 dd str2
.code
  _block1:
  mov AX, @data
  mov DS, AX
  mov ES, AX
  lea SI, str1
  lea DI, str2

  _block2:
  lds SI, ptr_str1
  les DI, ptr_str2

... but when I print str1 and str1 using the following macro, the second block doesn't return the expected results.

print MACRO param
    lea DX, param
    mov AH, 9
    int 21h
ENDM
like image 803
Roxana Ciobanu Avatar asked Dec 06 '13 22:12

Roxana Ciobanu


People also ask

What does Lea command do in assembly?

The lea instruction places the address specified by its first operand into the register specified by its second operand. Note, the contents of the memory location are not loaded, only the effective address is computed and placed into the register.

What is the difference between LES and LDS instruction?

lds means Load pointer using DS and likewise les means Load pointer using ES.

What does LDS mean in assembly language?

Purpose. Load a doubleword of data into the specified general purpose register.

What is Lea DX in assembly language?

The LEA (Load Effective Address) instruction is a way of obtaining the address which arises from any of the Intel processor's memory addressing modes. it moves the contents of the designated memory location into the target register.


1 Answers

lea means Load Effective Address. So lea SI, str1 sets si to the offset of str1. The correct memory addressing syntax used by lea and other instructions depends on the assembler used, some assemblers want lea si,[str1].

lds and les do something completely different compared to lea. lds means Load pointer using DS and likewise les means Load pointer using ES. In practice, lds SI, ptr_str1 sets ds and si based on the values stored in the memory address ds:ptr_str1 (the syntax is [ds:ptr_str1] in some assemblers).

So, what these instructions do in your code:

  1. lea SI, str1 sets si to point to the address of str1. This seems perfectly OK.

  2. lds SI, ptr_str1 sets si to 'My' (0x794d in hexadecimal) and also sets ds to ' f' (0x6620 in hexadecimal). Note that x86 is a little-endian architecture. ds is the default segment from where the pointer value is read using lds and les. So instead of loading the string address to ds:si, the first 4 bytes of the string are used as an address that is loaded into ds:si. I assume this is not what you wanted to do.

  3. les DI, ptr_str2 sets di according to the value stored in 0x6620:ptr_str2 (ds is the default segment for most x86 instructions, and currently ds has the value 0x6620) and also sets es according to the value stored in 0x6620:ptr_str2+2. Probably this is not what you wanted to do.

like image 148
nrz Avatar answered Oct 18 '22 16:10

nrz