3

I'm studying assembly language and can't resolve the following exercise myself.

Assume the following values are stored at the indicated memory addresses and registers:

enter image description here

Now, we have an instruction:

addl %ecx , (%eax)

For me it means - storing the result of addition of values stored in %ecx and in memory address (%eax), in a memory address (%eax).

Correct answer for that exercise is : Value 0x100 and destination address 0x100.

I understand that right operand is destination address, but how did we get value of 0x100 by the calculation %ecx + (%eax)?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 2
    That instruction looks like it is an intel instruction with AT&T syntax (it would help to define that within the question because otherwise operands are reverse!). Which means that 0x01 (value of ecx) is added to the 0xFF already present at address 0x100. What's the problem? – Damon Aug 10 '15 at 10:06
  • @Damon i understand all for now, thank you! My problem was i tried to add 0x100 + 0x1 – Evgeniy Kleban Aug 10 '15 at 10:11

1 Answers1

7

First, I hate AT&T syntax, which is what you have here... that aside.

EAX contains 0x100. 0x100 has the value 0xFF in it.

ECX contains 0x1.

0x1 + 0xFF = 0x100. So far so good.

The final result is then placed into the address pointed to by EAX. Therefore, (0X100) == 0x100

I think you were most of the way there.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • 1
    Thanks for the explanation. I want to add: It also helps to think of `addX src, dest` as `dest += src` so in C: `dest = dest + src`. Also note that parentheses notate a pointer. The parentheses basically tell us "treat the value as an address and follow it", so basically "dereferencing" it. But attention: When using e.g. the LEA instruction, the parentheses don't dereference the pinter since LEA doesn't really touch memory. That's correct? – xotix Jan 10 '20 at 12:11
  • You have that right, @xotix. Check this out: https://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction/30064227#30064227 – David Hoelzer Jan 10 '20 at 13:34