0

I came across this line from a textbook.

mov var, 10                   ;var contains 1 at 0xA

mov RAX, [var+5]              ;This will add 5 to the address already held by var, then dereference the new address and put whatever is at that address into RAX.

So the steps taken will be

  1. Calculate new Address
  2. Dereference new address to get value stored in memory location

For step1, will they use (0x1 from the value(1) + 5)
OR (10 from address(0xA) + 5)?
What would be the answer for Step1 ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    *address already held by var* is a confusing way to state it. It sounds like it's talking about the data from memory, but it's not; x86 doesn't have memory-indirect addressing modes. Assuming `var` is a label in `.data` or `.bss`, e.g. `extern char var[]`, it's actually just accessing memory at 5 + symbol address. e.g. like C `(&rax, &var[5], 8)`, except of course rax is a register. So the address math is all done at assemble time, assembling to an instruction with a `[disp32]` or `[RIP+rel32]` [addressing mode](https://stackoverflow.com/q/34058101) which will access `var+5`. – Peter Cordes Jan 01 '22 at 08:03
  • 1
    Also, wait a minute, `;var contains 1 at 0xA` makes no sense. The only way `mov var, 10` will assemble is with `10` (0xA) as the value. And only in MASM syntax where bare `var` is the same as `[var]`, and can imply an operand-size for the store. There's no `1` anywhere, either as an offset to the symbol address or as an immediate value. – Peter Cordes Jan 01 '22 at 08:04
  • 1
    There are two possibilities: var is a register (e.g. by defining a macro called var), then the address is 15. Or var is a memory address, then the calculated address is the address of var + 5 bytes. – Sebastian Jan 01 '22 at 08:43
  • 1
    I think the best way to explain is that `var` doesn't *hold* an address, it *is* an address. The fact that you can `mov var, 10` instead of having to write `mov qword [var], 10` or `mov qword [var+0], 10` is just MASM being weird. – Peter Cordes Jan 01 '22 at 09:02
  • 2
    It's possible that `var` is not even an address, but an expression, such as `var = [bp + 1111]`. I don't remember the syntax, or which assembler, but this was a possibility to define variables 30 years ago. From this expression it's possible to construct new addresses in confusing ways, just like `a[1] == 1[a]` in C. `var + 5` would be `[bp + 1111] + 5 == [bp + 1116]`. – Aki Suihkonen Jan 01 '22 at 15:09

0 Answers0