Trouble understanding LEA assembly











up vote
0
down vote

favorite












I am very new to assembly, so I just want to make sure I am understanding whats happening in this code:



  400610:   83 ff 1d                cmp    $0x1d,%edi
400613: 7f 0c jg 400621 <f1+0x11>
400615: 89 f8 mov %edi,%eax
400617: c1 e0 04 shl $0x4,%eax
40061a: 8d 04 f8 lea (%rax,%rdi,8),%eax
40061d: 8d 04 78 lea (%rax,%rdi,2),%eax
400620: c3 retq
400621: c1 ff 02 sar $0x2,%edi
400624: 8d 47 11 lea 0x11(%rdi),%eax
400627: c3 retq


From what I can see, there is a jump to 400621 but I am not sure what f1+0x11 signifies.



If it does not jump, it continues and shifts %eax to the left 4 (multiplies by 16), then performs eax = rax + rdi * 8, then eax = rax + rdi * 2? I am not sure what the purpose of doing that twice is.



If it does jump, it shifts %eax to the right 2 (divides by 4) and then I am not sure what (lea 0x11(%rdi),%eax) does.



Help would be appreciated, thank you!










share|improve this question




















  • 1




    The reason for two lea instructions is that the effective addressing is limited so you can't achieve that with a single one. If you understand those ones, what problem do you have with lea 0x11(%rdi),%eax? That just does eax=edi+0x11.
    – Jester
    Nov 21 at 0:06










  • Ah, yes, the maximum number it can be is 8, I know that. So for lea, when a number is outside of the parentheses, it just adds it? If I had lea 0x11(%rdi,%rsi,4),%eax it would do eax = rsi * 4 + rdi + 0x11? Also for jg 400621 <f1+0x11>, 400621 signifies the address, but what does the second part signify?
    – Andrew Zaw
    Nov 21 at 0:08








  • 1




    1) Yes. Not specific tolea of course, that's just the general form of an address. 2) The same thing. It's just a friendly service of your disassembler showing it as an offset from the previous symbol. f1 is presumably the name of this function so f1+0x11=0x400610+0x11=0x400621
    – Jester
    Nov 21 at 0:21












  • Ah, that makes sense, thank you very much!
    – Andrew Zaw
    Nov 21 at 0:51










  • "the maximum number it can be is 8" - the address scale in 64b mode can be only 1, 2, 4 or 8. ... it's not about "min/max" value, but only these powers of two are available, "5" is invalid too ( (,%edi,5) = error ). (but you can still use lea to do multiplication by 5 like lea (%eax, %eax, 4), %eax => eax = eax + eax*4 = eax*5)
    – Ped7g
    2 days ago

















up vote
0
down vote

favorite












I am very new to assembly, so I just want to make sure I am understanding whats happening in this code:



  400610:   83 ff 1d                cmp    $0x1d,%edi
400613: 7f 0c jg 400621 <f1+0x11>
400615: 89 f8 mov %edi,%eax
400617: c1 e0 04 shl $0x4,%eax
40061a: 8d 04 f8 lea (%rax,%rdi,8),%eax
40061d: 8d 04 78 lea (%rax,%rdi,2),%eax
400620: c3 retq
400621: c1 ff 02 sar $0x2,%edi
400624: 8d 47 11 lea 0x11(%rdi),%eax
400627: c3 retq


From what I can see, there is a jump to 400621 but I am not sure what f1+0x11 signifies.



If it does not jump, it continues and shifts %eax to the left 4 (multiplies by 16), then performs eax = rax + rdi * 8, then eax = rax + rdi * 2? I am not sure what the purpose of doing that twice is.



If it does jump, it shifts %eax to the right 2 (divides by 4) and then I am not sure what (lea 0x11(%rdi),%eax) does.



Help would be appreciated, thank you!










share|improve this question




















  • 1




    The reason for two lea instructions is that the effective addressing is limited so you can't achieve that with a single one. If you understand those ones, what problem do you have with lea 0x11(%rdi),%eax? That just does eax=edi+0x11.
    – Jester
    Nov 21 at 0:06










  • Ah, yes, the maximum number it can be is 8, I know that. So for lea, when a number is outside of the parentheses, it just adds it? If I had lea 0x11(%rdi,%rsi,4),%eax it would do eax = rsi * 4 + rdi + 0x11? Also for jg 400621 <f1+0x11>, 400621 signifies the address, but what does the second part signify?
    – Andrew Zaw
    Nov 21 at 0:08








  • 1




    1) Yes. Not specific tolea of course, that's just the general form of an address. 2) The same thing. It's just a friendly service of your disassembler showing it as an offset from the previous symbol. f1 is presumably the name of this function so f1+0x11=0x400610+0x11=0x400621
    – Jester
    Nov 21 at 0:21












  • Ah, that makes sense, thank you very much!
    – Andrew Zaw
    Nov 21 at 0:51










  • "the maximum number it can be is 8" - the address scale in 64b mode can be only 1, 2, 4 or 8. ... it's not about "min/max" value, but only these powers of two are available, "5" is invalid too ( (,%edi,5) = error ). (but you can still use lea to do multiplication by 5 like lea (%eax, %eax, 4), %eax => eax = eax + eax*4 = eax*5)
    – Ped7g
    2 days ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am very new to assembly, so I just want to make sure I am understanding whats happening in this code:



  400610:   83 ff 1d                cmp    $0x1d,%edi
400613: 7f 0c jg 400621 <f1+0x11>
400615: 89 f8 mov %edi,%eax
400617: c1 e0 04 shl $0x4,%eax
40061a: 8d 04 f8 lea (%rax,%rdi,8),%eax
40061d: 8d 04 78 lea (%rax,%rdi,2),%eax
400620: c3 retq
400621: c1 ff 02 sar $0x2,%edi
400624: 8d 47 11 lea 0x11(%rdi),%eax
400627: c3 retq


From what I can see, there is a jump to 400621 but I am not sure what f1+0x11 signifies.



If it does not jump, it continues and shifts %eax to the left 4 (multiplies by 16), then performs eax = rax + rdi * 8, then eax = rax + rdi * 2? I am not sure what the purpose of doing that twice is.



If it does jump, it shifts %eax to the right 2 (divides by 4) and then I am not sure what (lea 0x11(%rdi),%eax) does.



Help would be appreciated, thank you!










share|improve this question















I am very new to assembly, so I just want to make sure I am understanding whats happening in this code:



  400610:   83 ff 1d                cmp    $0x1d,%edi
400613: 7f 0c jg 400621 <f1+0x11>
400615: 89 f8 mov %edi,%eax
400617: c1 e0 04 shl $0x4,%eax
40061a: 8d 04 f8 lea (%rax,%rdi,8),%eax
40061d: 8d 04 78 lea (%rax,%rdi,2),%eax
400620: c3 retq
400621: c1 ff 02 sar $0x2,%edi
400624: 8d 47 11 lea 0x11(%rdi),%eax
400627: c3 retq


From what I can see, there is a jump to 400621 but I am not sure what f1+0x11 signifies.



If it does not jump, it continues and shifts %eax to the left 4 (multiplies by 16), then performs eax = rax + rdi * 8, then eax = rax + rdi * 2? I am not sure what the purpose of doing that twice is.



If it does jump, it shifts %eax to the right 2 (divides by 4) and then I am not sure what (lea 0x11(%rdi),%eax) does.



Help would be appreciated, thank you!







assembly x86-64






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 0:06









Jester

45.9k34381




45.9k34381










asked Nov 21 at 0:03









Andrew Zaw

605




605








  • 1




    The reason for two lea instructions is that the effective addressing is limited so you can't achieve that with a single one. If you understand those ones, what problem do you have with lea 0x11(%rdi),%eax? That just does eax=edi+0x11.
    – Jester
    Nov 21 at 0:06










  • Ah, yes, the maximum number it can be is 8, I know that. So for lea, when a number is outside of the parentheses, it just adds it? If I had lea 0x11(%rdi,%rsi,4),%eax it would do eax = rsi * 4 + rdi + 0x11? Also for jg 400621 <f1+0x11>, 400621 signifies the address, but what does the second part signify?
    – Andrew Zaw
    Nov 21 at 0:08








  • 1




    1) Yes. Not specific tolea of course, that's just the general form of an address. 2) The same thing. It's just a friendly service of your disassembler showing it as an offset from the previous symbol. f1 is presumably the name of this function so f1+0x11=0x400610+0x11=0x400621
    – Jester
    Nov 21 at 0:21












  • Ah, that makes sense, thank you very much!
    – Andrew Zaw
    Nov 21 at 0:51










  • "the maximum number it can be is 8" - the address scale in 64b mode can be only 1, 2, 4 or 8. ... it's not about "min/max" value, but only these powers of two are available, "5" is invalid too ( (,%edi,5) = error ). (but you can still use lea to do multiplication by 5 like lea (%eax, %eax, 4), %eax => eax = eax + eax*4 = eax*5)
    – Ped7g
    2 days ago
















  • 1




    The reason for two lea instructions is that the effective addressing is limited so you can't achieve that with a single one. If you understand those ones, what problem do you have with lea 0x11(%rdi),%eax? That just does eax=edi+0x11.
    – Jester
    Nov 21 at 0:06










  • Ah, yes, the maximum number it can be is 8, I know that. So for lea, when a number is outside of the parentheses, it just adds it? If I had lea 0x11(%rdi,%rsi,4),%eax it would do eax = rsi * 4 + rdi + 0x11? Also for jg 400621 <f1+0x11>, 400621 signifies the address, but what does the second part signify?
    – Andrew Zaw
    Nov 21 at 0:08








  • 1




    1) Yes. Not specific tolea of course, that's just the general form of an address. 2) The same thing. It's just a friendly service of your disassembler showing it as an offset from the previous symbol. f1 is presumably the name of this function so f1+0x11=0x400610+0x11=0x400621
    – Jester
    Nov 21 at 0:21












  • Ah, that makes sense, thank you very much!
    – Andrew Zaw
    Nov 21 at 0:51










  • "the maximum number it can be is 8" - the address scale in 64b mode can be only 1, 2, 4 or 8. ... it's not about "min/max" value, but only these powers of two are available, "5" is invalid too ( (,%edi,5) = error ). (but you can still use lea to do multiplication by 5 like lea (%eax, %eax, 4), %eax => eax = eax + eax*4 = eax*5)
    – Ped7g
    2 days ago










1




1




The reason for two lea instructions is that the effective addressing is limited so you can't achieve that with a single one. If you understand those ones, what problem do you have with lea 0x11(%rdi),%eax? That just does eax=edi+0x11.
– Jester
Nov 21 at 0:06




The reason for two lea instructions is that the effective addressing is limited so you can't achieve that with a single one. If you understand those ones, what problem do you have with lea 0x11(%rdi),%eax? That just does eax=edi+0x11.
– Jester
Nov 21 at 0:06












Ah, yes, the maximum number it can be is 8, I know that. So for lea, when a number is outside of the parentheses, it just adds it? If I had lea 0x11(%rdi,%rsi,4),%eax it would do eax = rsi * 4 + rdi + 0x11? Also for jg 400621 <f1+0x11>, 400621 signifies the address, but what does the second part signify?
– Andrew Zaw
Nov 21 at 0:08






Ah, yes, the maximum number it can be is 8, I know that. So for lea, when a number is outside of the parentheses, it just adds it? If I had lea 0x11(%rdi,%rsi,4),%eax it would do eax = rsi * 4 + rdi + 0x11? Also for jg 400621 <f1+0x11>, 400621 signifies the address, but what does the second part signify?
– Andrew Zaw
Nov 21 at 0:08






1




1




1) Yes. Not specific tolea of course, that's just the general form of an address. 2) The same thing. It's just a friendly service of your disassembler showing it as an offset from the previous symbol. f1 is presumably the name of this function so f1+0x11=0x400610+0x11=0x400621
– Jester
Nov 21 at 0:21






1) Yes. Not specific tolea of course, that's just the general form of an address. 2) The same thing. It's just a friendly service of your disassembler showing it as an offset from the previous symbol. f1 is presumably the name of this function so f1+0x11=0x400610+0x11=0x400621
– Jester
Nov 21 at 0:21














Ah, that makes sense, thank you very much!
– Andrew Zaw
Nov 21 at 0:51




Ah, that makes sense, thank you very much!
– Andrew Zaw
Nov 21 at 0:51












"the maximum number it can be is 8" - the address scale in 64b mode can be only 1, 2, 4 or 8. ... it's not about "min/max" value, but only these powers of two are available, "5" is invalid too ( (,%edi,5) = error ). (but you can still use lea to do multiplication by 5 like lea (%eax, %eax, 4), %eax => eax = eax + eax*4 = eax*5)
– Ped7g
2 days ago






"the maximum number it can be is 8" - the address scale in 64b mode can be only 1, 2, 4 or 8. ... it's not about "min/max" value, but only these powers of two are available, "5" is invalid too ( (,%edi,5) = error ). (but you can still use lea to do multiplication by 5 like lea (%eax, %eax, 4), %eax => eax = eax + eax*4 = eax*5)
– Ped7g
2 days ago



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403451%2ftrouble-understanding-lea-assembly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403451%2ftrouble-understanding-lea-assembly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...