Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CX work in '[ ]' in 16 Bit Assembly when it is not a base or index register?

When using MASM 6.15 under DOSBox whenever I try to do:

mov al, [cx]

or

mov al, [cx + bx]

or

mov al, [cx + 4]

I am getting the error: 'Only Base or Index Registers are allowed'

But then again, let's say I have an array var1. If I do:

.model small
.stack 4096

.data
 var1 BYTE 1, 2, 3, 4, 5, 6, 7, 8, 9

.code
main proc
mov ax, @data
mov ds, ax

mov cx, 5
mov al, [var1 + cx]

mov ah, 4Ch
int 21h
main endp
end main

It works perfectly fine. Why doesn't it give the same error as above? CX is not a base or index register.

What is the whole working essence of the [] operator?

like image 942
Parker Queen Avatar asked Oct 22 '18 17:10

Parker Queen


People also ask

What is CX in assembly?

CX is known as the count register, as the ECX, CX registers store the loop count in iterative operations. DX is known as the data register. It is also used in input/output operations.

How does add work in assembly?

The add instruction adds together its two operands, storing the result in its first operand. Note, whereas both operands may be registers, at most one operand may be a memory location. The inc instruction increments the contents of its operand by one. The dec instruction decrements the contents of its operand by one.

How does MOV work in assembly?

The MOV instruction MOV copies the data in the source to the destination. The data can be either a byte or a word. Sometimes this has to be explicitly stated when the assembler cannot determine from the operands whether a byte or word is being referenced.

What are EAX EBX ECX EDX registers?

The EAX, EBX, ECX, EDX, EBP, EDI, and ESI registers are all 32-bit general-purpose registers, used for temporary data storage and memory access. Some of CPU instructions modify specific registers.


1 Answers

This is a bug in some versions of MASM. There are cases like these where MASM will try to incorrectly encode an instruction that shouldn't be possible. It should have thrown an error trying to encode this, as CX can't be used as a base or index in 16-bit addressing.

mov al, [var1 + cx]

Instead of generating an error it incorrectly generates it as:

mov bh, var1[bx+si]

The invalid instruction is encoded as:

8A B8 xx xx

Where xx xx is the offset of var1.

like image 119
Michael Petch Avatar answered Nov 15 '22 09:11

Michael Petch