Thursday, March 8, 2012

Sign extension & Zero extension
3.10 Sign Extension, Zero Extension, Contraction, and Saturation
Since two's complement format integers have a fixed length, a small problem develops. What happens if you need to convert an eight bit two's complement value to 16 bits? This problem, and its converse (converting a 16 bit value to eight bits) can be accomplished via sign extension and contraction operations. Likewise, the 80x86 works with fixed length values, even when processing unsigned binary numbers. Zero extension lets you convert small unsigned values to larger unsigned values.
Consider the value "-64". The eight bit two's complement value for this number is $C0. The 16-bit equivalent of this number is $FFC0. Now consider the value "+64". The eight and 16 bit versions of this value are $40 and $0040, respectively. The difference between the eight and 16 bit numbers can be described by the rule: "If the number is negative, the H.O. byte of the 16 bit number contains $FF; if the number is positive, the H.O. byte of the 16 bit quantity is zero."
To sign extend a value from some number of bits to a greater number of bits is easy, just copy the sign bit into all the additional bits in the new format. For example, to sign extend an eight bit number to a 16 bit number, simply copy bit seven of the eight bit number into bits 8..15 of the 16 bit number. To sign extend a 16 bit number to a double word, simply copy bit 15 into bits 16..31 of the double word.
You must use sign extension when manipulating signed values of varying lengths. Often you'll need to add a byte quantity to a word quantity. You must sign extend the byte quantity to a word before the operation takes place. Other operations (multiplication and division, in particular) may require a sign extension to 32-bits. You must not sign extend unsigned values.
Sign Extension:
Eight Bits  Sixteen Bits  Thirty-two Bits

 $80        $FF80         $FFFF_FF80
 $28        $0028         $0000_0028
 $9A        $FF9A         $FFFF_FF9A
 $7F        $007F         $0000_007F
 ---        $1020         $0000_1020
 ---        $8086         $FFFF_8086

To extend an unsigned byte you must zero extend the value. Zero extension is very easy - just store a zero into the H.O. byte(s) of the larger operand. For example, to zero extend the value $82 to 16-bits you simply add a zero to the H.O. byte yielding $0082.
Zero Extension:
Eight Bits    Sixteen Bits           Thirty-two Bits

 $80            $0080                 $0000_0080
 $28            $0028                 $0000_0028
 $9A            $009A                 $0000_009A
 $7F            $007F                 $0000_007F
 ---            $1020                 $0000_1020
 ---            $8086                 $0000_8086

The 80x86 provides several instructions that will let you sign or zero extend a smaller number to a larger number. The first group of instructions we will look at will sign extend the AL, AX, or EAX register. These instructions are
  • cbw(); // Converts the byte in AL to a word in AX via sign extension.
  • cwd(); // Converts the word in AX to a double word in DX:AX
  • cdq(); // Converts the double word in EAX to the quad word in EDX:EAX
  • cwde(); // Converts the word in AX to a doubleword in EAX.

'via Blog this'

No comments:

Post a Comment