[英]Arithmetic operations in AT&T assembly (adding memory and a register)
I am having trouble understanding the behavior of adding two values, one in a register and one in memory. 我无法理解将两个值相加的行为,一个在寄存器中,一个在存储器中。
Assume for memory we have: 假设内存有:
Address Value
0x100 0xFF
And assume for registers, we have: 并假设寄存器有:
Register Value
%eax 0x100
%ecx 0x1
Now my undrestanding is that when you use (%eax) as an operand, what you're doing is referencing the memory at that address, ie, you'll get the value 0xFF, ie 现在,我不了解的是,当您使用(%eax)作为操作数时,您正在执行的操作是引用该地址处的内存,即您将获得值0xFF,即
(%eax) = 0xFF
But when (%eax) is the destination of an addition or subtraction, the reference (%eax) gives us back the address in memory instead of referencing the memory (similar to lea behavior), ie, 但是,当(%eax)是加法或减法的目标时,引用(%eax)会给我们返回内存中的地址,而不是引用内存(类似于lea行为),即
addl %ecx, (%eax)
writes 0x1 + 0xFF to 0x100. 将0x1 + 0xFF写入0x100。 What is confusing me is that when we read the value to perform the addition (%eax) gives us back 0xFF, but when we use (%eax) as the destiation of addition, we write to 0x100.
令我感到困惑的是,当我们读取该值以执行加法运算(%eax)时,我们会返回0xFF,但是当我们使用(%eax)作为加法运算的目的地时,我们将其写入0x100。
Can someone explain this please? 有人可以解释一下吗?
when we read the value to perform the addition
(%eax)
gives us back 0xFF, but when we use(%eax)
as the destiation of addition, we write to 0x100.当我们读取执行加法的值时
(%eax)
返回0xFF,但是当我们使用(%eax)
作为加法目标时,我们写入0x100。 Can someone explain this please?有人可以解释一下吗?
In both cases, using (%eax)
as an operand means referencing memory at the address contained in %eax
. 在两种情况下,使用
(%eax)
作为操作数都意味着在%eax
包含的地址处引用内存。 The address in your example is 0x100, and the doubleword at that address contains the value 0xFF. 您的示例中的地址为0x100,该地址处的双字包含值0xFF。
If you do a read - eg movl (%eax),%ecx
- you simply grab the value at 0x100, so you get 0xFF
. 如果您进行读取-例如
movl (%eax),%ecx
您只需在0x100处抓取值,那么您将获得0xFF
。
If you do addl %ecx,(%eax)
you first read from memory address 0x100 (ie the value 0xFF), add the value of %ecx
(1) to it to get the value 0x100, and then write the result back to the same address that you read it from (0x100). 如果执行
addl %ecx,(%eax)
,则首先从内存地址0x100中读取(即值0xFF),将%ecx
(1)的值添加到其中以得到值0x100,然后将结果写回到与您从中读取它的地址相同(0x100)。 Note that the value of %eax
never changes during this operation; 请注意,
%eax
的值在此操作期间永远不会改变; it's just the value pointed to by %eax
that changes. 只是
%eax
指向的值会改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.