<<< Extending Signed and Unsigned Integers | Index | Sign Extending Unsigned Value >>> |
Consider:
.DATA mem8 SBYTE -5 mem16 SWORD +5 mem32 SDWORD -5 .CODE . . . mov al, mem8 ; Load 8-bit -5 (FBh) cbw ; Convert to 16-bit -5 (FFFBh) in AX mov ax, mem16 ; Load 16-bit +5 cwd ; Convert to 32-bit +5 (0000:0005h) in DX:AX mov ax, mem16 ; Load 16-bit +5 cwde ; Convert to 32-bit +5 (00000005h) in EAX mov eax, mem32 ; Load 32-bit -5 (FFFFFFFBh) cdq ; Convert to 64-bit -5 ; (FFFFFFFF:FFFFFFFBh) in EDX:EAX
Sign extending instructions efficiently convert unsigned values as well, provided the sign bit is zero.
This example, for instance, correctly widens mem16 whether you treat the variable as signed or unsigned.
The processor does not differentiate between signed and unsigned values.
For instance, the value of mem8 in the previous example is literally 251 (0FBh) to the processor.
It ignores the human convention of treating the highest bit as an indicator of sign.
The processor can ignore the distinction between signed and unsigned numbers because binary arithmetic works the same in either case.
The programmer, not the processor, must keep track of which values are signed or unsigned, and treat them accordingly.
<<< Extending Signed and Unsigned Integers | Index | Sign Extending Unsigned Value >>> |