0

I'm not sure how to understand the assembly command mov. e.g. in the following section:

mov -4(%ebp),%eax # %eax=*(%ebp-4), so eax is geting the **value** that is stored in ebp-4

in general, whenever I do:

mov (mem_address), %reg # reg = *(mem_address), so the **value** inside the adress mem is stored in the register

but if I do:

mov %eax, -8(%ebp) # in C: ebp-8 = eax

so if I try to translate into C then, moving data from mem to reg is like reg=*mem but moving data from reg to mem is just mem=reg because the register stores the value and not the address to the value? did I get it right?

and one last thing, is a label an address or a value? e.g.

mov label, %eax # in C: eax = *label? just like regular address in memory?

thank you for your help!

E. Ginzburg
  • 197
  • 1
  • 1
  • 9
  • 5
    I'm voting to close this question as off-topic because it's a question about programming not specifically related to embedded systems. Stackoverflow is a better site for this kind of question. – The Photon Nov 30 '19 at 16:00
  • 1
    Short answer: you can't translate assembly instructions one-to-one into C. If you write C, your C compiler will manage moving data between registers and memory without needing explicit C code to tell it to do that. Meaning, it will create instructions like this as needed, but there won't be any specific C code that corresponds to this instruction. – The Photon Nov 30 '19 at 16:03
  • @ThePhoton I'm not sure what's better to do: should I delete my question from here and post it in stack overflow? I didn't mean to translate explicitly, just how to understand if I'm talking about the address or the value, and I thought it would be easier to explain it using ideas and notations from C. thank you for your help – E. Ginzburg Nov 30 '19 at 16:10
  • 1
  • Search stackoverflow and see if a similar question has already been asked. If not, you can flag this question and ask the mods to migrate it.
  • – The Photon Nov 30 '19 at 16:12
  • As a quick answer, it's easier to think about how to translate from C to assembly. Suppose you have *a = x-2;. You will have to mov the value x into a register. Then subtract a constant (2). Then mov a (an address) into a register. Then mov the data from the register that has data to the memory pointed to by the register holding the address a. In this very simple example, you've used mov to move both addresses and data, and you 've used registers to hold both addresses and data. It's been up to the compiler to keep track of which is which. – The Photon Nov 30 '19 at 16:30
  • @ThePhoton thank you! – E. Ginzburg Nov 30 '19 at 17:17
  • "so if I try to translate into C then" - why would you do this? – Bruce Abbott Nov 30 '19 at 20:25
  • ebp-8=eax is just not possible in C: ebp-8 is not an “lvalue”, it can’t be on the left of an assignment, because it’s the result of a calculation. Where did you get that this assembly could be translated into this C statement? – jcaron Nov 30 '19 at 23:43
  • no magic/difference in C reg = (mem_address) or (mem_address) = reg. both work just fine. Now the stack would be a problem here since thats not a fixed address you can program in C, you would have to do some more C stuff to get the address of something on the stack within a function. – old_timer Dec 01 '19 at 02:22