0

the code below as I understand it says store the pointer in %rsi in %eax if thats correct then the second line says add the pointer in %eax to the pointer in %rdi ?

very confused. I know assembly doesn't have pointers I am just speaking as translating assembly to c. I must write the assembly code into c code, and these two lines are killing me. Can I have clarification?

movl    (%rsi), %eax
addl    %eax, (%rdi)
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 1
    The first line doesn't *store* the pointer, it *dereferences* it. You could also say it *loads* from an *address* instead. The second line is a (non-atomic) *read-modify-write*, where the value of `eax` is added to the *object pointed to* by `rdi`. – EOF Feb 18 '16 at 01:39
  • so rather than copy the memory space of `%rsi` into `%eax` move the contents of `%rsi` into `%eax`? I've said the same thing twice just now but I mean in the context of pointers – Christopher Jakob Feb 18 '16 at 01:43
  • See those brackets? That means `(%register)` is an *address in memory*. `movl (%rsi), %eax` is *vastly* different from "moving `%rsi` into `%eax`", the closest equivent to which would be something like `movq %rsi, %rax`, or `movl %esi, %eax`. – EOF Feb 18 '16 at 01:46
  • Is the title of your question backwards? It seems like you want to know how to translate assembly to C, not C to assembly. – Barmar Feb 18 '16 at 04:42
  • in the way, I am asking my question it is not. The question is based on thinking about the assembly code as it relates to pointers in c. – Christopher Jakob Feb 18 '16 at 06:17

1 Answers1

0

Since you seem to be using using AT&T syntax, the parentheses dereference the value in %rsi. The C equivalent for these expressions would be:

/* Expression 1 */
unsigned int* p = some_address;
unsigned int i = *p; /* *p dereferences the address in p */


/* Expression 2 */
unsigned int* p = some_address;
unsigned int i = 8;
i += *p /* Increase i by the value pointed to by p */
Levi
  • 1,921
  • 1
  • 14
  • 18
  • Where did `8` come from? Shouldn't `i` just have the value it got from the first expression? – Barmar Feb 18 '16 at 01:47
  • @EOF Sorry about that. I'm more of a Intel person – Levi Feb 18 '16 at 01:48
  • @Barmar `8` is just an example value. I intend the two expressions to be treated as separate examples – Levi Feb 18 '16 at 01:49
  • Maybe I'm wrong, but I assumed the two statements were intended to be executed sequentially. – Barmar Feb 18 '16 at 01:50
  • I think I get the entire point. Allow me to say what the code does in english. – Christopher Jakob Feb 18 '16 at 01:51
  • 1
    So it's basically `*rdi += *rsi`. – Barmar Feb 18 '16 at 01:51
  • 1
    take what is in the memory address of rsi and copy the contents then add the contents that is in rsi and add it to the memory space of rdi which is exactly what @Barmar said of `*rdi += *rsi` What I also see, is that pointing (rdi) rather than rdi is in this case strictly academic. You don't need to do this at all. just pratice. @Levi @EOF <-- by the way, very clever name lol – Christopher Jakob Feb 18 '16 at 01:51
  • real quick are any of you familar with what the command setbe means? I want to say set byte equal? most likely wrong and still researching. – Christopher Jakob Feb 18 '16 at 02:09
  • @ChristopherJakob: look up `setcc` in the insn ref manual (x86 tag wiki for links). `be` is the below-equal condition. See http://stackoverflow.com/questions/33666617/which-is-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and for some notes about using `setcc`, since it's only available with 8bit operand-size. – Peter Cordes Feb 18 '16 at 02:37
  • ahh @PeterCordes thank you. I will very much look that up and the reference you just gave me is awesome! thank you. – Christopher Jakob Feb 18 '16 at 06:18