2

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?

xrisk
  • 3,790
  • 22
  • 45

1 Answers1

2

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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • So if I understand correctly, I can't use `lea` to load the address of si+len. Rather, I would have to use an `add` instruction. – xrisk May 02 '19 at 05:14
  • Can you elaborate on how `lea di, [si + len]` reduces to `LEA DI, [SI] + 021h` ? Is it because `len` is situated at 021h? – xrisk May 02 '19 at 05:15
  • @Rishav: That's weird syntax, normally you'd write `lea di, [si + 21h]`, but yes if you see that in disassembly it's because `offset len` = 21h. If you used `[si + len]` as a memory operand for any other instruction, you'd access memory at `len + si`, like C array indexing with `si` as a byte offset. – Peter Cordes May 02 '19 at 05:18
  • Thank you. And yes, that syntax was produced by emu8086 not me. – xrisk May 02 '19 at 05:20
  • @Rishav: emu8086 is weird, and has some strange behaviour. Like `mov [si], 1` assembles instead of giving an error about ambiguous operand size. It just picks a default operand size, either byte or word I forget which! – Peter Cordes May 02 '19 at 05:21