简体   繁体   中英

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

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,

addl %ecx, (%eax)    

writes 0x1 + 0xFF to 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.

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. Can someone explain this please?

In both cases, using (%eax) as an operand means referencing memory at the address contained in %eax . The address in your example is 0x100, and the doubleword at that address contains the value 0xFF.

If you do a read - eg movl (%eax),%ecx - you simply grab the value at 0x100, so you get 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). Note that the value of %eax never changes during this operation; it's just the value pointed to by %eax that changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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