简体   繁体   中英

Understanding lea and assembly

I was hoping someone could validate my understanding of the following assembly code:

test %esi,%esi
js 17 <build+0x17>
cmp $0x8,%esi
ja 1d <build+0x1d>
lea (&rsi,2),%ecx
shl $0x2,%rdi
mov %rdi,%rax
retq
mov $0x0,%eax //17
retq
mov $0x0,%eax //1d
retq

Here's what I think the code does:

  • If esi&esi is negative return item at address 0
  • If esi is above 8, return item at address 0
  • Else store address of rsi *2 into ecx
  • Then right shift rdi right by 2 bits
  • Copy rdi to rax and return it

This is what the code does:

if (esi >= 0 && esi <= 7) {
    return rdi >> (esi * 8);   // Note: arithmetic shift; preserves sign
}
return 0;

So esi specifies the number of bytes (0..7) to shift out on the right from rdi . Since an arithmetic shift is used, the original sign of rdi is preserved.

For example:

Input:
  rdi = 0xFC00AABB12345678
  esi = 2

Output:
  rax = 0xFFFFFC00AABB1234

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM