Author Topic: mov word from effective addr. to reg  (Read 15665 times)

nobody

  • Guest
mov word from effective addr. to reg
« on: November 19, 2007, 02:51:01 PM »
Hi, I have searched the forum and cannot find the answer to my problem, I'm new to asm coding though so maybe I haven't defined the problem correctly.

I'm having trouble moving a word from memory to a register.  The error I am seeing is:  "error: invalid effective address".. Here is some code:

mov  cx, 0011H
myLoop:
  dec  cx
  mov  dx,[state+cx-1]
  ...

state db £hello","$"

I have tried using the following:

lea  dx, [state+cx-1]
mov  dx,[dx]

But again the same error.  If this is a stupid mistake sorry in advance, sometimes I have these moments.  Thanks for any help

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: mov word from effective addr. to reg
« Reply #1 on: November 19, 2007, 03:44:06 PM »
16-bit addressing modes have bx and bp as "base" registers, and di and si as "index" registers. Valid effective addresses can include an (optional) offset, plus an (optional) base register, plus an (optional) index register. Switch to one of those registers, and it should work.

32-bit addressing modes are much more flexible. An (optional) offset, plus an (optional) base register, plus an (optional) "index" register, which may be multiplied by a "scale" of 2, 4, or 8. *Any* register can be used for "base" and "index", except that esp can't be used as "index".

Using 32-bit addressing modes in 16-bit code works. You *could* do "mov dx, [state + ecx -1]", even in 16-bit code. The total offset can't exceed the "segment limit" - ordinarily 0FFFFh (in "real dos" we can change this - in a Windows "dos box", no). The upper word of 32-bit registers is "probably" clear in 16-bit code, but sometimes not, so "xor ecx, ecx" (any and all registers you plan to use in this way) somewhere near the top of your program would be "safer".

I think your best solution would be to use bx, di, or si - bp is "special", in that it defaults to [ss:bp], instead of the usual default of [ds:???]. (*all* x86 addresses involve a segment register - often we can ignore 'em)

Best,
Frank

nobody

  • Guest
Re: mov word from effective addr. to reg
« Reply #2 on: November 19, 2007, 07:44:19 PM »
Thanks Frank, worked a charm.