[英]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 一样,我们的老师要求我们解释这部分代码。
my basic understanding of what it does is:我对其作用的基本理解是:
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.