简体   繁体   English

指针解引用和乘法的基本 C / x86 汇编代码的逐步说明

[英]Step-by-step explanation of basic C / x86 assembly code for pointer dereference and multiply

I come here today because I am currently on a university work on introduction with assembly x86.我今天来到这里是因为我目前正在大学从事关于装配 x86 的介绍工作。 As we did C prior this, our teacher asked us to explain this portion of code.正如我们之前做的 C 一样,我们的老师要求我们解释这部分代码。 与问题相关的c代码

my basic understanding of what it does is:我对其作用的基本理解是:

  • EAX will contain the value of the memory area associated with y. EAX 将包含与 y 关联的 memory 区域的值。
  • ECX will contain the value of the memory zone associated with EAX, ie the pointer to y. ECX 将包含与 EAX 关联的 memory 区域的值,即指向 y 的指针。
  • imul will multiply the value of the pointer eax (ie the pointer to y) and the value of x. imul 将指针 eax(即指向 y 的指针)的值与 x 的值相乘。
  • We move the pointer of y in EDX, then we move the value of the multiplication to the pointer of y.我们在 EDX 中移动 y 的指针,然后将乘法的值移动到 y 的指针。

Am I right?我对吗? If not, could someone give me a better explaination of the part I misunderstood?如果没有,有人可以更好地解释我误解的部分吗?

Thank you in advance.先感谢您。

As with any assembler, you pretty much need to sit with the ISA manual at hand, because each instruction comes with a lot of flavours depending on what you pass to it.与任何汇编程序一样,您几乎需要手头有 ISA 手册,因为根据您传递给它的内容,每条指令都有很多不同的风格。

First check out What does `dword ptr` mean?首先查看`dword ptr` 是什么意思? So basically this 32 bit accesses the contents at a certain address.所以基本上这个 32 位访问某个地址的内容。

Since y is a pointer, the contents of y is the address where *y can be found.因为y是一个指针,所以 y 的内容就是可以找到*y y地址。 So first the y address will get moved into eax and then the contents of the address just uploaded into eax will be moved into ecx .因此,首先将y地址移动到eax中,然后将刚刚上传到eax的地址的内容移动到ecx中。

imul with two operands goes like this:带有两个操作数的imul如下所示:

  • Two-operand form.二操作数形式。 With this form the destination operand (the first operand) is multiplied by the source operand (second operand).使用这种形式,目标操作数(第一个操作数)乘以源操作数(第二个操作数)。 The destination operand is a general-purpose register and the source operand is an immediate value, a general-purpose register, or a memory location.目标操作数是通用寄存器,源操作数是立即数、通用寄存器或 memory 位置。 The product is then stored in the destination operand location.然后将产品存储在目标操作数位置。

So it multiplies ecx with whatever was stored at dword ptr[x] , that is the contents of x .因此,它将ecx与存储在dword ptr[x]中的任何内容相乘,即x的内容。 And stores the result in ecx .并将结果存储在ecx中。

Then again the address y is uploaded to a register edx (maybe an optimization flaw, because eax already holds it).然后再次将地址y上传到寄存器edx (可能是优化缺陷,因为eax已经拥有它)。 And then the result of the multiplication stored in exc is moved to that location.然后将存储在exc中的乘法结果移动到该位置。

Code with comments:带注释的代码:

        mov     eax,dword ptr [y]         ;eax = y
        mov     ecx,dword ptr [eax]       ;ecx = *y
        imul    ecx,dword ptr [x]         ;ecx = (*y) * x
        mov     edx,dword ptr [y]         ;edx = y
        mov     dword ptr[edx], ecx       ;(*y) = (*y) * x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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