1

I'm given these leal instructions and I have to fill in what they mean. I've been trying to study how the leal instruction acts, but I've been having difficulty finding relevant material around the web. What I found is

leal (src1, src2), dst  //dst = src2 + src1

This info doesn't give enough information, because the leal instruction is far more complex than that.

How is this looking?

Assume %eax holds the value x and %ecx holds the value y. Fill in the table.

%edx =  x + 6___________leal 6(%eax),%edx
%edx =  x + y___________leal (%eax,%ecx),%edx
%edx =  x * 5y___________leal (%eax,%ecx,4),%edx
%edx =  9x + 7___________leal 7(%eax,%eax,8),%edx
%edx =  4y + 10___________leal 0xA(,%ecx,4),%edx
%edx =  x + 3y + 9___________leal 9(%eax,%ecx,2),%edx
phuclv
  • 37,963
  • 15
  • 156
  • 475
rlima1877
  • 33
  • 8
  • I would use intel syntax so it's easier to read. eg. `lea edx, [eax + 6]`. – greatwolf Nov 04 '13 at 01:26
  • Possible duplicate of [lea assembly instruction](https://stackoverflow.com/questions/9153282/lea-assembly-instruction) – phuclv Aug 09 '18 at 01:48
  • @phuclv: If you want a canonical duplicate for LEA on non-pointer values, [Using LEA on values that aren't addresses / pointers?](https://stackoverflow.com/a/46597375) is my attempt. (But it's more about LEA vs. MOV, than how to actually use it for math). If anything, I like this Q&A better than the one you linked. Both the answer and the question, and I might consider making that a duplicate of this. So many LEA questions, it's a wasteland of confusion with so many scattered small answers that explain one part. You have to really understand CPUs and asm for it to make total sense. – Peter Cordes Aug 09 '18 at 02:11

1 Answers1

4

What I would do is translate the above att syntax over to intel syntax as a first step since that is much more intuitive to analyze.

lea edx, [eax + 6]            
lea edx, [eax + ecx]          
lea edx, [eax + ecx * 4]      
lea edx, [eax + eax * 8 + 7]  
lea edx, [ecx * 4 + 10]       
lea edx, [eax + ecx * 2 + 9]  

Even if you know nothing about the assembly syntax you can look at the above can kind of figure out what they mean. You can mentally map this as an assignment statement in your favor programming language: the thing you are assigning to is on the left-hand side and the value being assigned is on the right-hand side. Now it's just a matter of substituting in the registers with your x and y values:

edx = x + 6
edx = x + y
edx = x + y*4
edx = x + x*8 + 7 => x*9 + 7
edx = y*4 + 10
edx = x + y*2 + 9
greatwolf
  • 20,287
  • 13
  • 71
  • 105