S
S
Soultaker2014-04-17 08:29:43
Programming
Soultaker, 2014-04-17 08:29:43

How is lea different from mov in this example?

short int A[20];
short int B[20];
...
lea eax, A;
lea ebx, B;

and a second example:
short int *A = new short int[20];
short int *B = new short int[20];
...
mov eax, A;
mov ebx, B;

Both codes do the same thing. As I understand it, instead of transferring data from a memory location to a register (as MOV does), the LEA instruction loads the data address into the register. But proceeding from this, I cannot explain why these examples work and how they work. Help explain. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Pronin, 2014-04-17
@nightw0rk

lea inserts data pointer into register, mov copies data
mov eax,1; eax = 1
mov eax,VarName ; will give an error if the size of VarName is greater than eax
. In your example, everything is logical, in 1 case an array is created, through lea the address to the array is put into the register, in the other case we create an array and get its address, and already put it in the register

R
Rsa97, 2014-04-17
@Rsa97

Let's see what code will be generated in each case:

@_main	proc	near
  push      ebp
  mov       ebp,esp
; short int A[20];
  add       esp,-40 
  push      ebx
; asm lea eax, A;
 	lea	 eax, word ptr [ebp-40]
; return A[0];
  movsx     eax,word ptr [ebp-40]

@_main	proc	near
  push      ebp
  mov       ebp,esp
; short int *A = new short int[20];
  add       esp,-4
  push      ebx
  push      40
  call      @$bnwa$qui
  pop       ecx
  mov       dword ptr [ebp-4],eax
; mov eax, A;
 	mov	 eax, dword ptr [ebp-4]
; return A[0];
  mov       eax,dword ptr [ebp-4]
  movsx     eax,word ptr [eax]

That is, in the first case, the variable A is directly an array, and in the second it stores the address of the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question