【问题标题】:What do these assembly lines mean?这些装配线是什么意思?
【发布时间】:2021-05-01 13:18:15
【问题描述】:

我正在做作业,因为我不明白这些行应该是什么意思?

mov rax, qword ptr [rbp - 0x28]
mov rax, qword ptr [rax]
mov eax, dword ptr [rax]
mov dword ptr [rbp - 0x10], eax
mov rax, qword ptr [rbp - 0x28]
mov rax, qword ptr [rax]
mov rax, qword ptr [rax + 8]
mov qword ptr [rbp - 8], rax

据我了解,这些行只是将 rbp - 0x28 中的任何内容复制到 rbp - 0x10 中。然后它将 rbp - 0x20 中的任何内容(因为 rax + 8 = rbp - 0x28 + 8 = rbp - 0x20)放入 rbp - 8

稍后我们有这些行:

mov rax, dword ptr [rbp - 8]
mov eax, dword ptr [rax]
mov dword ptr [rbp - 0x10], eax
mov rax, qword ptr [rbp - 8]
mov rax, qword ptr [rax + 8]
mov qword ptr [rbp - 8], rax

我相信它将 rbp - 8 中的任何内容放入 rbp - 0x10 然后将 rbp 中的任何内容(因为 rax + 8 = rbp - 8 + 8 = rbp)放入 rbp - 8 但它似乎真的错了在我的胆量中......

谁能给我解释一下这几行代码,以便我改进?

非常感谢您, 祝你有美好的一天

【问题讨论】:

  • 看起来像未优化的调试模式代码,使用 RBP 作为帧指针,因此它的小偏移量是本地变量。其中两个局部变量是指针(或指向指针的指针),可能指向链表结构,考虑到加载值的取消引用量。您是否可能错过了这样一个事实,即mov rax, [rax+8] 是像 p = p->next 这样的负载,而不仅仅是 RAX 上的数学?
  • 括号[...]表示内存访问。所以mov rax, qword ptr [rax] 解引用了一个指针,然后 那个 值被mov eax, dword ptr [rax] 解引用为另一个指针

标签: assembly x86 x86-64


【解决方案1】:

您分享的代码(很可能)是关于访问和复制对象属性/变量的:

mov rax, qword ptr [rbp - 0x28]    ; gets the QWORD pointer of the object from the local variable at RBP-40 (which is the 64-bit address of the object)
mov rax, qword ptr [rax]           ; dereferences the QWORD pointer to set RAX to point to the beginning of the object
mov eax, dword ptr [rax]           ; retrieves the value of the first DWORD of the object (often this is the pointer to the DESTRUCTOR of the object, but in this case this makes no sense)
mov dword ptr [rbp - 0x10], eax    ; copies the DWORD value in EAX to the DWORD variable at RBP-16
mov rax, qword ptr [rbp - 0x28]    ; again, same object as above
mov rax, qword ptr [rax]           ; again, dereference
mov rax, qword ptr [rax + 8]       ; retrieve the QWORD value at position +8 (often this is a pointer to a method, this depends on the object structure - but in this case it seems to be the pointer to another object, see below)
mov qword ptr [rbp - 8], rax       ; copies the QWORD value to the local variable at RBP-8

因此,这段代码很可能会访问一个对象并将其两个属性/方法指针复制到相对于当前方法堆栈帧的基指针 (RBP) 的局部变量。

通常,析构函数的地址位于对象的基地址,但在这种情况下,只复制了一个 DWORD,所以可能还有其他事情发生。

在第二种情况下,它可能是将另一个对象的地址复制到局部变量中。下面的代码很有可能发生这种情况。

关于第二个样本:

mov rax, dword ptr [rbp - 8]     ; Get the QWORD variable (the second from the above code)
mov eax, dword ptr [rax]         ; dereference it (see above)
mov dword ptr [rbp - 0x10], eax  ; and copy it to the local variable at RBP-16 (notice that the value from the above code is overwritten!!!)
mov rax, qword ptr [rbp - 8]     ; again, get the object PTR
mov rax, qword ptr [rax + 8]     ; again, dereference it
mov qword ptr [rbp - 8], rax     ; and replace the value of the local variable at RBP-8 (the old object PTR) with the address of the (first?) (QWORD) value/address of the object (also overwritten!!!)

【讨论】:

    猜你喜欢
    • 2016-04-01
    • 2013-05-09
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2014-12-24
    • 2020-09-25
    • 1970-01-01
    相关资源
    最近更新 更多