0

How do I translate something like this (in C) to x86 assembly?

int i = 0;
int arr[3] = {0};

for(; i < 3; i++)
{
    arr[i] = 1;
}

I've tried to do:

mov [arr+cx], 1

while in loop, but got the error: "Illegal indexing mode" (using TASM if needed).

Thanks everyone!

3CLiPsE
  • 23
  • 4
  • `cx` can't be used with 16-bit addressing. I recommend looking at this link, especially section 1.2.7: http://www.ic.unicamp.br/~celio/mc404s2-03/addr_modes/intel_addr.html#HEADING2-114 – Michael Petch Jan 02 '18 at 19:13
  • Keep your loop counter in `SI` instead of `CX`. Also, you'll need an operand-size because your instruction is ambiguous between a byte or word store. Use `mov word ptr [arr + si], 1` (or `byte ptr`). – Peter Cordes Jan 02 '18 at 19:13
  • AFAIK, in 16 bit mode, only certain register combinations are allowed. Base registers can be `BX` and `BP`, while index registers can be `SI` and `DI`. And you can have a fixed displacement (in this case, `arr`). So `[arr+SI]` is valid, but `[arr+CX]` is not. – Rudy Velthuis Jan 02 '18 at 19:15
  • @PeterCordes : Depending on how you define `arr` you may not have to specify the size. If it was defined as `arr db 23, 34, 56` TASM and MASM will infer the size of `arr` as being byte automagically. If it were `dw` it would assume word size. Also be aware that `arr db 23, 34, 56` is not the same as `arr: db 23, 34, 56` – Michael Petch Jan 02 '18 at 19:21
  • @MichaelPetch: Ah, I wasn't sure if the implied operand-size would happen with symbol + register. (I remembered half way through writing my comment that TASM does that for plain `[arr]`, but decided to leave that part of my comment in.) – Peter Cordes Jan 02 '18 at 19:23
  • It will do it automatically if the displacement is an identifier – Michael Petch Jan 02 '18 at 19:25

0 Answers0