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!
assembly x86-64
|
show 2 more comments
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!
assembly x86-64
1
The reason for twolea
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 withlea 0x11(%rdi),%eax
? That just doeseax=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 hadlea 0x11(%rdi,%rsi,4),%eax
it would doeax = rsi * 4 + rdi + 0x11
? Also forjg 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 sof1+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 uselea
to do multiplication by 5 likelea (%eax, %eax, 4), %eax
=>eax = eax + eax*4 = eax*5
)
– Ped7g
2 days ago
|
show 2 more comments
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!
assembly x86-64
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
assembly x86-64
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 twolea
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 withlea 0x11(%rdi),%eax
? That just doeseax=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 hadlea 0x11(%rdi,%rsi,4),%eax
it would doeax = rsi * 4 + rdi + 0x11
? Also forjg 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 sof1+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 uselea
to do multiplication by 5 likelea (%eax, %eax, 4), %eax
=>eax = eax + eax*4 = eax*5
)
– Ped7g
2 days ago
|
show 2 more comments
1
The reason for twolea
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 withlea 0x11(%rdi),%eax
? That just doeseax=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 hadlea 0x11(%rdi,%rsi,4),%eax
it would doeax = rsi * 4 + rdi + 0x11
? Also forjg 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 sof1+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 uselea
to do multiplication by 5 likelea (%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 to
lea
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 to
lea
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
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 withlea 0x11(%rdi),%eax
? That just doeseax=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 doeax = rsi * 4 + rdi + 0x11
? Also forjg 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 to
lea
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 sof1+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 uselea
to do multiplication by 5 likelea (%eax, %eax, 4), %eax
=>eax = eax + eax*4 = eax*5
)– Ped7g
2 days ago