1

I'm trying to move the bl register into an array of bytes in x86 assembly language, but the following statement produces an error message from the masm assembler.

mov arr[2], bl produces the output 1>p4.asm(48): error A2101: cannot add two relocatable labels.

Is it possible to move a register into an array in x86 assembly language?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • I've spent days searching for an answer to this question, and I've found no answers yet. – Anderson Green Mar 26 '13 at 10:54
  • `bx` is a 16bit register, that is bigger than a byte, if you need only a byte contained in `bx`, you need to access `bl` its lower 8 bits part – Seki Mar 26 '13 at 12:06
  • @Seki I've fixed that part now, but I'm still trying to find out how to move a register into a specific index of an array. – Anderson Green Mar 26 '13 at 12:09

2 Answers2

3

Basically something that the following should work for a hard coded index value:

mov byte ptr [arr + 2], bl  ;store bl

The [] are optional: Confusing brackets in MASM32

For a more generic way:

mov si, 2                ;your index
mov al, bl               ;bl = byte value from your question
mov bx, offset arr
mov byte ptr [bx+si], al
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Seki
  • 11,135
  • 7
  • 46
  • 70
  • `mov byte ptr [ax], bl` produces this error: `1>p4.asm(54): error A2031: must be index or base register` – Anderson Green Mar 26 '13 at 13:45
  • sorry, while I can read quite easily assembly I am a bit rusty to write some myself. I forget that the `mov byte ptr` must use a base (bx / bp) or an index (si / di) register. See my update. – Seki Mar 26 '13 at 14:51
0

If we use an instruction with an immediate-operand, then we have to specify how many bytes our operand will be obtain. Therefore we have to use a keyword like byte, word, or dword for to inform the assembler how many following bytes of the ram location we want to acess.

mov byte ptr[bx+si], 01h
mov word ptr[bx+si], 0001h
mov dword ptr[bx+si], 00000001h

But if we use a register as an operand (with it known among of bytes), then we do not need it to specify (for to become a smaller listing with an easier overview).

mov [bx+si], al
mov al, [bx+si]

mov [bx+si], ax
mov ax, [bx+si]

Exception with MASM and with a DWORD-Register as an operand.

mov dword ptr[bx+si], eax
mov eax, dword ptr[bx+si]

Dirk