0

I want to get 2 different things:
(1) the (value at bp) plus 16 (mathematical addition) something similar to: mov ax, [bp] + 16
(2) the value of bp+16 (the address bp+16)

in order to get (1) I tried:

mov ax, bp+16  

But it gave an error.

for (2) I tried:

mov ax, [bp+16]  

Which worked. (I hope I did it correctly)..


Why can't assembly understand the evaluation of:

mov ax, bp+16

But can understand:

mov ax, [bp+16]  

because addition is not defined using the + sign, so what happens behind the scenes there?

MathAsker
  • 181
  • 8

1 Answers1

5

There's no form of mov that does math to produce a new value for the destination register that wasn't in a source register or memory operand. https://www.felixcloutier.com/x86/mov. The value you want in AX has to be an immediate, or contained directly in a register or in memory at some address.

If you want the value bp+16 instead of the memory at that address, use lea ax, [bp+16] instead of mov. Load Effective Address does the address calculation and then puts that address in the destination register, instead of loading from it. LEA is just a math instruction that uses memory-operand syntax and machine-encoding. It's convenient as a copy-and-add. (Or with 32-bit addressing modes, also shifting.)

(In terms of full seg:off x86 addressing, LEA only does the "offset" part, the "effective address"; it doesn't add the segment base so SS:BP vs. DS:DI or whatever is irrelevant.)

Doing mov ax, [bp+16] is valid because [] means dereference as a memory operand, and x86 16-bit addressing modes support register + displacement.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
  • So basically `lea ax, [bp+16]` is for the case (1) (which is: storing in ax the value of bp, then adding 16) ? thank you sir. – MathAsker May 21 '20 at 00:17
  • @StackOMeow: your question seems to have its definitions backwards or self-conflicting. You wrote "(1) the value *at* bp plus 16". "at" would imply using it as a memory operand. But yes, LEA is just a math instruction that uses memory-operand syntax and machine-encoding. – Peter Cordes May 21 '20 at 00:20
  • If I were to try and write (1) as a bad assembly code it would be `mov ax, [bp] + 16` (so moving the content that at address bp , then adding to that number 16) – MathAsker May 21 '20 at 00:22
  • @StackOMeow: Oh, I see what you want now. x86 can't do that with one instruction. You could `mov ax, 16` / `add ax, [bp]`, but that's not better than `mov ax, [bp]` / `add ax, 16`. You should use that `[bp] + 16` notation to clarify your question. – Peter Cordes May 21 '20 at 00:23