【问题标题】:Understanding lea and assembly了解 lea 和组装
【发布时间】:2016-07-26 08:51:05
【问题描述】:

我希望有人可以验证我对以下汇编代码的理解:

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

我认为代码的作用如下:

  • 如果 esi&esi 为负,则返回地址 0 处的项目
  • 如果esi大于8,则返回地址0处的项目
  • 否则将rsi *2的地址存储到ecx中
  • 然后将 rdi 右移 2 位
  • 将 rdi 复制到 rax 并返回

【问题讨论】:

  • 返回0,地址0处不返回任何东西
  • 其余的准确吗?
  • lea (&amp;rsi,8),%ecx 这一行可能应该写成lea (,%rsi,8),%ecx,意思是ECX=RSI*8,而不是“rsi 的地址”。其余的看起来不错。
  • 当然还有 "如果 esi&esi 为负数" == "如果 esi 为负数"
  • 此处讨论了相同的 asm 代码:​​[stackoverflow.com/questions/36379900/…

标签: assembly x86


【解决方案1】:

这就是代码的作用:

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

所以esi 指定从rdi 向右移出的字节数(0..7)。由于使用了算术移位,rdi 的原始符号被保留。

例如:

Input:
  rdi = 0xFC00AABB12345678
  esi = 2

Output:
  rax = 0xFFFFFC00AABB1234

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2011-02-17
    • 2023-04-02
    相关资源
    最近更新 更多