The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: arcktos on February 21, 2008, 06:12:45 PM

Title: cx - 16 bit but 32 bit value mov possible? :O
Post by: arcktos on February 21, 2008, 06:12:45 PM
Hey,

Can anybody explain me? -

CX is 16 bit register so maximal value should be 0FFFFh - 65535. How is it possible then, when I:


mov cx, 0FFFFFFFFh


Compiler throws no warings, and no errors but looking on the code in codeview and debug.exe(16 bit debuger, thats why i think) gives FFFF in cx?
Title: Re: cx - 16 bit but 32 bit value mov possible? :O
Post by: MichaelW on February 21, 2008, 07:37:01 PM
It looks like MASM is interpreting the value as -1. Both of these statements:

mov cx, 0ffffffffh
mov cx, -1

Generate the same code:

66B9FFFF  mov  cx,0FFFFh
Title: Re: cx - 16 bit but 32 bit value mov possible? :O
Post by: arcktos on February 21, 2008, 07:48:34 PM
Yea, i wrote 32bit program and debuged in Olly and if I give maximum value for ECX 0FFFFFFFFh i got -1 in debugger but good value in registry...I wanna ask why, compiler which always throws up code like this:


mov ah, cx


Does not throw code i maximum value is reached. Its only error if I give value more than 32bit for example 0FFFFFFFF1 then its invalid instrction operands but why it does not warn when I move 32 bit directly into 16 register? Strange...
Title: Re: cx - 16 bit but 32 bit value mov possible? :O
Post by: BogdanOntanu on February 21, 2008, 11:17:46 PM
Yes, it is a "normal" bug.

It it valid to say mov cx,-1 and it so "happens" that -1 binary representation  in 2's complement on 32 bits is 0xFFFF_FFFF.
Although logically different for humans the two values are the same for a computer.

The assembler should have kept track of the number of characters in the source code number in order to avoid this.

Besides who is using 16 bits and CX register in our days anymore anyway :P ?

Although a compiler can generate and expose a lot of errors it is not possible to protect yourself from yourself only based on compiler "intelligence". Know your tools and be prudent.