CS-220, Week 4-C
R. Eckert

80X86 Data Formats

All of the following are stored as single bytes and defined with DB--

ASCII Characters: 
     
     7-bit or 8-bit character codes
     Example: 01000001b = 41h = 65d --> 'A'

Integers:

  BCD: (Binary Coded Decimal)
     Unpacked --> one coded digit/byte
         Example 5 --> 00000101b
     Packed --> two coded digits/byte
         Example 27 --> 00100111b

  Binary Byte (char or unsigned char in C):
     unsigned (00000000 to 11111111b), (0 to 0FFh)
              ==> (0 to 2^8-1), (0 to +255d)
     signed--use 8 bit two's complement:
              (10000000 to 01111111), (80h to 7Fh)
              ==> (-2^7 to +2^7-1), (-128 to +127)

Larger Integers:

The following are defined as words (two bytes) and defined with DW
The least significant byte is stored at the low address, the most
significant byte at the high address ("little endian")--

  binary integers, address offsets, segment register bases:

     unsigned (0000000000000000b to 1111111111111111b, (0 to 0FFFFh)
            ==> (0 to 2^16-1), (0 to 65535d)

     signed--use 16 bit two's complement:
            (1000000000000000b to 0111111111111111b (8000h to 7FFFh)
            ==> (-2^15 to +2^15-1), (-32768d to +32767d)


The following are defined as double words (four bytes), defined with DD:

  unsigned integers (00000000 to 0FFFFFFFFh) ==> (0 to +2^32-1)
  signed integers--use 32-bit twos complement:
                   (80000000h to 7FFFFFFFh) ==> (-2^31 to +2^31-1)

Addresses (double word):

     Segment:offset
     Offset stored first (least significant byte first), then the
        segment base (least significant byte first)
     Example: 0F3400h could be considered as: F3000+0400h
              In base/offset notation --> 0F300:400h
              Would be stored as  00 04 00 F3
                                 low -----> high addresses
              (This is one possible way of storing this address)

Representing non integers (numbers with fractional parts)

1. Fixed point:

     Use a determined number of bits to store the integer part
        (could be signed or unsigned)
     And a determined number of bits to store the fractional part

  Example: Store +13.375 as a "16/16" signed fixed point number
           using a "little endian" representation
              i.e., 16 bit 2's complement for integer part,
              and 16 bits for the fractional part
           Store number as a double word:
              LSW = fractional part, MSW = integer part
           Express answer in hexadecimal

     Interger part: 13 = 0000 0000 0000 1101 binary
                       = 00 0D hex
     Fractional part: .375 = .0110 0000 0000 0000
                           = .60 00 hex
     Resulting double word (little endian): 00 0D 60 00

2. Floating Point (IEEE "Short Real") numbers (32 bits):

     Express number as --  S * M * 2^E
     S = sign bit, M = Matissa, E = Exponent

     Sign bit    8 exponent bits   23 mantissa bits
      msb    --------------------------->    lsb

     Cast number into normalized binary form:  +/- 1.xxxxxxxx * 2^E

     Sign bit: 1 ==> negative, 0 ==> positive

     Exponent bits (bias-127):  127 + E

     Mantissa bits: xxxxxxxxx (i.e., don't store the leading 1)

     Example: Convert -13.375d to IEEE short real format;
              Express the result in hexadecimal:

     1. Convert to binary:

         Integer part 13d = 1101b

         Fractional part .375d = 0.011b

         So the result is 1101.011b

     2. Normalize:  1101.011 = 1.101011 * 2^3

     3. Sign bit: negative number ==> 1

     4. Exponent bits:  127 + 3 = 130d = 10000010b

     5. Mantissa bits: 10101100000000000000000b

     6. Final result expressed in binary:

             1 10000010 10101100000000000000000

     7. To express in hex, group:

             1100 0001 0101 0110 0000 0000 0000 0000

               C1560000h

        This would be what would be stored in memory.
        To verify, look at the .LST file you get when
        you write an assembly language program that has
        the following directive in a data segment:

               DD    -13.375

Quad Words:

The following are defined as quad words (eight bytes), defined with DQ:

  unsigned integers (0 to 0FFFFFFFFFFFFFFFFh) ==> (0 to +2^64-1)
  signed integers--use 64-bit two's complement):
                  (8000000000000000h to 7FFFFFFFFFFFFFFFh)
                   ==> (-2^63 to +2^63-1)

  Floating Point (IEEE "Long Real") numbers--
     Sign bit    11 exponent bits   52 mantissa bits
      msb    --------------------------->    lsb

     Cast number into normalized binary form:  +/- 1.xxxxxxxx * 2^E

     Sign bit: 1 ==> negative, 0 ==> positive

     Exponent bits (bias-1023):  1023 + E

     Mantissa bits: xxxxxxxxx (i.e., don't store the leading 1)