07 - number representations

Upload: relevances

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 07 - Number Representations

    1/38

    EEM336

    Microprocessors I

    Representation of Numbers

    1

  • 8/14/2019 07 - Number Representations

    2/38

    Contents

    Representation of Numbers

    Unsigned, Sign magnitude, Onescomplement, Twos complement, Biased

    Real Numbers

    Single Precision, Double Precision

    BCD Numbers

    ASCII Numbers

    Shift and Rotate

    2

  • 8/14/2019 07 - Number Representations

    3/38

  • 8/14/2019 07 - Number Representations

    4/38

    Integer Representation

    There are 5 commonly known integerrepresentations:

    1. Unsigned

    2. Sign magnitude

    3. Ones complement

    4. Twos complement

    5. Biased (not commonly known)

    4

  • 8/14/2019 07 - Number Representations

    5/38

    Integer Representation(continued)

    All have been used at various timesfor various reasons.

    Virtually all modern computersoperate based on twos complementrepresentation. Because,

    Hardware is faster

    Hardware is simpler (which makes itfaster)

    5

  • 8/14/2019 07 - Number Representations

    6/38

    Unsigned Numbers

    The standard binary encoding thatyou already know

    Only positive integer numbers arerepresented

    Range: 0 to 2n - 1 for n bits

    Examples: 5 is represented as 0000 0101

    134 is represented as 1000 0110

    6

  • 8/14/2019 07 - Number Representations

    7/38

    Sign Magnitude

    Both positive and negative integerscan be represented

    The most significant bit (MSB) is used

    to represent the sign For positive numbers, MSB is 0 and

    for negative numbers, MSB is 1

    Examples: 5 is represented in 8-bit as 0000 0101

    -5 is represented in 8-bit as 1000 0101

    7

  • 8/14/2019 07 - Number Representations

    8/38

    Ones Complement

    Positive integers use the samerepresentation as unsigned

    Negation is done by taking a bitwisecomplement of the positiverepresentation

    Examples:

    5 is represented in 8-bit as 0000 0101

    -5 is represented in 8-bit as 1111 1010

    8

  • 8/14/2019 07 - Number Representations

    9/38

    Twos Complement

    Positive integers use the samerepresentation as unsigned

    Negation is done by taking the onescomplements and adding 1

    Examples:

    5 is represented in 8-bit as 0000 0101

    -5 is represented in 8-bit as 1111 1011

    9

  • 8/14/2019 07 - Number Representations

    10/38

    Biased Representation

    A bias value is chosen as either 2n-1 or 2n-1-1,according to the representation algorithm

    The integer number to be represented is added tothe bias

    Unsigned representation of the result is the biasedrepresentation of the number

    Examples:

    In 8-bit, if bias is 127, then 5 is 1000 0100 (132)

    In 8-bit, if bias is 127, then -5 is 0111 1010 (122)

    10

  • 8/14/2019 07 - Number Representations

    11/38

    Sign Extension in TwosComplement

    Assume that you have a signed number in 8 bits andyou want to convert it to 16 bits

    This type of conversion from smaller number of bits tolarger number of bits is called sign extension

    In this case, the original number is copied into LSBsand the sign bit is copied into the MSBs

    Examples:5: 0000 0101 in 8 bits 0000 0000 0000 0101 in 16 bits

    -5: 1111 1010 in 8 bits 1111 1111 1111 1010 in 16 bits

    11

  • 8/14/2019 07 - Number Representations

    12/38

    CharacterRepresentation

    ASCII codes are used to represent the characters

    Characters are generally represented in 8-bits

    Examples:

    A is represented as 0100 0001 (65 or 41h)

    B is represented as 0100 0010 (66 or 42h)

    0 is represented as 0011 0000 (48 or 30h)

    9 is represented as 0011 1001 (57 or 39h)

    In Assembly, characters should be written into single-quotes (i.e.dont use double-quotes)

    12

  • 8/14/2019 07 - Number Representations

    13/38

    Converting from Stringto Integer

    The 0 character and the number 0 aredifferent

    If you want to convert the string 354 to

    integer, you should follow this algorithm: Read 3 and translate it to 3 (just subtract

    30h)

    Read 5 and translate it to 5

    integer = 3 * 10 + 5 = 35

    Read 4 and translate it to 4

    integer = 35 * 10 + 4 = 354

    13

  • 8/14/2019 07 - Number Representations

    14/38

    Representation of RealNumbers

    Real numbers (or floating-point numbers)are often encountered in computer programs

    A real number should be converted to binarysystem and encoded into bits

    There are several representations but Intelmicroprocessors use IEEE 754, version 10.0standard

    Intel 8086 has no floating point instructions, so

    you have to code them by yourself

    14

  • 8/14/2019 07 - Number Representations

    15/38

    Converting Real Numbersinto Binary

    Separate the number into integerand fractional parts

    You can easily convert the integerpart into binary

    You can convert the fractional partby multiplying it by 2 continuouslyand taking the integer parts of theresults

    15

  • 8/14/2019 07 - Number Representations

    16/38

    Example: Convert 64.2 toBinary

    Separate it into 64 and 0.2

    Binary representation of 64 is 100 0000

    0.2 can be gotten using the algorithm:

    0.2 * 2 = 0.4 0 (MSB) 0.4 * 2 = 0.8 0

    0.8 * 2 = 1.6 1

    0.6 * 2 = 1.2 1

    0.2 * 2 = 0.4 0 (and it repeats itself) 0.2 = 0.0011 0011 0011

    Then, 64.2 is 100 0000.0011 0011 0011

    16

  • 8/14/2019 07 - Number Representations

    17/38

    Scientific Notation

    The real numbers should benormalized and written in scientificnotation

    In scientific notation, a number hasthree components: Sign

    Mantissa

    Exponent

    In normalized form, the integer part ofthe mantissa should be 1.

    17

  • 8/14/2019 07 - Number Representations

    18/38

    Example: ScientificNotation of 64.2

    64.2 can be written in binary as:

    1.00 0000 0011 0011 0011 26

    Here, 1.00 0000 0011 0011 0011 is called the mantissa

    6 is the exponent

    Sign is plus (+)

    18

  • 8/14/2019 07 - Number Representations

    19/38

    IEEE 754 Standard

    According to the IEEE 754 standard,floating-point numbers can berepresented in either 32 bits or 64

    bits

    32 bit version is called single-precision and 64 bit version is

    called double-precision

    19

  • 8/14/2019 07 - Number Representations

    20/38

    Single Precision

    The first bit (bit 31) represents the sign (0 forpositive and 1 for negative numbers)

    Next 8 bits (23-30) represent the biasedexponent(bias = 127)

    Next 23 bits (0-22) represent the 24 bits of themantissa (24 bits in 23 bits!!! Is it correct?)

    Yes! The first bit of the mantissa, which comes from theinteger part, is not needed to be coded

    20

    S Exponent Mantissa

    31 30 23 22 0

  • 8/14/2019 07 - Number Representations

    21/38

    Double Precision

    The first bit (bit 63) represents the sign (0 forpositive and 1 for negative numbers)

    Next 11 bits (52-62) represent the biasedexponent(bias = 1023)

    Next 52 bits (0-51) represent the 53 bits of themantissa (53 bits in 52 bits!!! Is it correct?)

    Yes! The first bit of the mantissa, which comes from theinteger part, is not needed to be coded

    21

    S Exponent Mantissa

    63 62 52 51 0

  • 8/14/2019 07 - Number Representations

    22/38

    64.2 in Single Precision

    Since 64.2 is positive, sign bit should be 0

    Exponent is 6; add 127 and obtain biased value (6+ 127 = 133 = 1000 0101)

    Mantissa is 1.00 0000 0011 0011 0011

    Ignore 1 in integer part and take the 23 bits fromthe fractional part: 000 0000 0110 0110 01100110

    Combine them: 0100 0010 1000 0000 0110 01100110 0110 (or 42806666h)

    22

  • 8/14/2019 07 - Number Representations

    23/38

    64.2 in Double Precision

    Since 64.2 is positive, sign bit should be 0

    Exponent is 6; add 1023 and obtain biased value (6 +1023 = 1029 = 100 0000 0101)

    Mantissa is 1.00 0000 0011 0011 0011

    Ignore 1 in integer part and take the 52 bits from thefractional part: 0000 0000 1100 1100 1100 1100 11001100 1100 1100 1100 1100 1100

    Combine them: 0100 0000 0101 0000 0000 1100 11001100 1100 1100 1100 1100 1100 1100 1100 1100 (or40500CCCCCCCCCCCh)

    23

  • 8/14/2019 07 - Number Representations

    24/38

    Exceptions

    Zero is stored as all zeros.

    The number infinity is stored as allones in the exponent and all zeros in

    the mantissa. The sign bit indicateseither a positive or a negative infinity.

    + = 01111 1111 000 0000 0000

    0000 0000 0000- = 11111 1111 000 0000 0000

    0000 0000 0000

    24

  • 8/14/2019 07 - Number Representations

    25/38

    Exercises

    Calculate the single and double precisionrepresentations of12.3 (ans: 4144 CCCDh and4028 9999 9999 999Ah)

    Calculate the single and double precisionrepresentations of-23.4 (ans: C1BB 3333hand C037 6666 6666 6666h)

    What does the single precision floating point

    number 3F80 0000h represent?

    25

  • 8/14/2019 07 - Number Representations

    26/38

    BCD (Binary CodedDecimal) Data

    In some cases, you might considerrepresenting each digits of an integernumber in 4 bits

    For example, 1234h might representthe decimal value 1234

    These type of numbers are called

    BCD numbers There are some special instructions

    to be used with BCD numbers

    26

  • 8/14/2019 07 - Number Representations

    27/38

    DAA

    DAA: Decimal Adjust after Addition

    Corrects the result of addition of twopacked BCD values on AL register

    Example:

    MOV AL, 15 ; AL = 0Fh

    DAA ; AL = 15h

    27

  • 8/14/2019 07 - Number Representations

    28/38

    DAA Example

    Sum two 4-digit BCD numbers stored inBX and DX and store the result in CX asa BCD.MOV DX, 1234h ; Load 1234 BCD

    MOV BX, 3099h ; Load 3099 BCD MOV AL, BL ; Sum low bytes

    ADD AL, DL ; AL = 34h + 99h = CDh

    DAA ; CF = 1, AL = 33h (133)

    MOV CL, AL ; Store low byte of result

    MOV AL, BH ; AL = 30h (sum high bytes)

    ADC AL, DH ; AL = 43h

    DAA ; CF = 0, AL = 43h

    MOV CH, AL ; Store high byte of result

    28

  • 8/14/2019 07 - Number Representations

    29/38

    DAS

    DAS: Decimal Adjust afterSubtraction

    Corrects the result of subtraction oftwo packed BCD values on ALregister

    Similar to DAA but DAS follows asubtraction

    29

  • 8/14/2019 07 - Number Representations

    30/38

    ASCII Coded Numbers

    You may use ASCII codes of numbersto represent them

    For example, you can represent 19 as

    3139h since ASCII codes of 1 and 9are 31h (=49) and 39h (=57)respectively

    There are some instructions to be usedon ASCII coded numbers: AAA, AAD,AAM, and AAS.

    30

  • 8/14/2019 07 - Number Representations

    31/38

    AAA

    AAA: ASCII Adjust after Addition

    Corrects result in AH and AL afteraddition of two ASCII coded numbers

    Works on AL and changes AL and AH 3030h should be added to AX after AAA

    Example:MOV AX, '01' ; AX = 3031h (ASCII code for 1)

    ADD AL, '9' ; AL = 0Ah

    AAA ; AH = 01h, AL = 00h

    ADD AX, 3030h ; AX = 3130 (ASCII code for 10)

    31

  • 8/14/2019 07 - Number Representations

    32/38

    Other ASCII CodeOperations

    AAD: ASCII Adjust before Division

    AAM: ASCII Adjust afterMultiplication

    AAS: ASCII Adjust after Subtraction

    32

  • 8/14/2019 07 - Number Representations

    33/38

    Shift and Rotate

    Shift and rotate instructions manipulatebinary numbers at the binary bit level

    Common applications in low-level

    software used to control I/O devices Shift/Rotate count may be an immediate

    value or CL

    Examples:

    SHL AX, 1SHR BX, CL

    33

  • 8/14/2019 07 - Number Representations

    34/38

    Shift

    Position or move numbers to the left orright within a register or memorylocation.

    also perform simple arithmetic asmultiplication by powers of 2+n (left shift) anddivision by powers of 2-n (right shift).

    The microprocessors instruction set

    contains four different shift instructions: two are logical; two are arithmetic shifts

    34

  • 8/14/2019 07 - Number Representations

    35/38

    The Shift Instructions

    logical shifts move0 in the rightmostbit for a logical leftshift;

    0 to the leftmost bitposition for a logicalright shift

    arithmetic rightshift copies thesign-bit through the

    number logical right shift

    copies a 0 throughthe number.

    35

  • 8/14/2019 07 - Number Representations

    36/38

    Logical vs. Arithmetic?

    Logical shifts multiply or divideunsigneddata; arithmetic shiftsmultiply or divide signeddata.

    a shift left always multiplies by 2 foreach bit position shifted

    a shift right always divides by 2 for each

    position shifting a two places, multiplies or

    divides by 4

    36

  • 8/14/2019 07 - Number Representations

    37/38

    Rotate

    37

  • 8/14/2019 07 - Number Representations

    38/38

    References

    http://pages.cs.wisc.edu/~smoler/x86te

    emu8086 Emulators Documentation

    Textbook

    38

    http://pages.cs.wisc.edu/~smoler/x86text/TOC.lectnotes.htmlhttp://pages.cs.wisc.edu/~smoler/x86text/TOC.lectnotes.html