Search code examples
masmx86-16emu8086

Adding an offset to a memory address


I have some code like so (emu8086)

data segment
    str1 db "hello"
    len dw 4h
data ends

code segment
    ...
    ...
    mov si, offset str1
    lea di, [si + len]
code ends

I would expect this to make di point to the address of DS:0004, however the actual instruction generated is LEA DI, [SI] + 021h.

If instead, I use:

lea di, [si + 4]

Then it works as expected.

How do I make the first version work in a similar way to the second?


Solution

  • Where is your "expected" 4 coming from? If it's from the contents of len dw 4h, then you need a load, like perhaps add si, [len].

    lea does not access the contents of memory.

    x86 doesn't have a copy-and-add with a memory source, so you have to choose between a "destructive" add with a register destination, or lea that just does math with registers + assemble-time constants