ECE2049: Embedded Computing in Engineering Design C Term Spring 2018 Lecture #3: Of Integers and Endians (pt. 2) Reading for Today: Davies Ch 2, MSP430 User's Guide Ch 6.1, 6.3 Reading for Next Class: Davies Ch 2, MSP430 User's Guide Ch 6.1, 6.3 Homework #1 (on the web): Due Friday 1/19 (on paper at the beginning of class) Lab #0 (on the web): Report (on paper) due Tues 1/23(in class) Code (zipped archive of modified demo) submit on-line Last class: There are a number of standard formats for encoding data into binary representations for use inside computers. Examples include Char = 1 byte = Can hold ASCII characters OR small integers Integers = Unsigned, 2's comp., Sign-magnitude This Class: Finish discussion of data formats -- Floating Point and Endian discussion Binary Coded Decimal = A legacy format. Each decimal digit encoded in 4-bits 367 = Hexidecimal (hex) Numbers: Base 16 KNOW THESE!!! --> 1 Hex character represent values from 0 to 15d using digits 0 F h DEC BIN HEX DEC BIN HEX What is 158d =? in hex 0 0000 0 8 1000 8 1 0001 1 9 1001 9 2 0010 2 10 1010 A 3 0011 3 11 1011 B 4 0100 4 12 1100 C 5 0101 5 13 1101 D 6 0110 6 14 1110 E 7 0111 7 15 1111 F
Conversion between hex and binary is piece of cake! Just convert each hex digit to a binary nibble... 1001 1110b = 158d Or vise versa 2AC4h = 2 A C 4 Ex: Review: What decimal value does 8008h represent as an (a) unsigned integer, (b) 2's comp integer ( c) sign-magnitude integer (d) BCD (e) Two ASCII characters?
SO, how EXACTLY are the variables declared in programs stored? >> Depends on the compiler being used. In CCS for the MSP430 the types defined as int a; // 16 bit signed (2's comp)integer float b; // 32 bit IEEE floating point char c,h; // 8 bit (unsigned) integer unsigned int d; // 16 bit unsigned integer long int e; // 32 bit signed integer (2's comp) double f; // 64 bit IEEE floating point SIZE and TYPE of variable define the range of values that they can represent! The VALUE stored in a variable CAN NOT exceed the fixed variable size. Variables will overflow or roll over if value exceed variable size Ex: What is the value of h in each loop? c = 253; for (a = 0; a < 4; a++) { h = c + (char) a; // cast not strictly needed } What about Fraction Numbers??? >> There are 2 conventions: Fixed point and Floating Point Fixed point: Binary radix point assigned a fixed location in byte (or word) 00000.101 = Granularity or precision is function of the number of fractional bits assigned --> 4 fractional bits = 2-4 = 0.0625 = smallest fraction >> Often (usually) data just scaled to integer representation
Floating Point (IEEE Standard) : Used to better approximate real valued decimal numbers to a prescribed number of decimal places Single Precision (32 bits): S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF Value = (-1) S 2 (E-127) * (1.F) Ex: What floating point value is represented by CAAA00? Ex: Represent -5.375 as a Single Precision number -5.375
>> How many decimal digits of precision does 32-bit floating point have? >> The effective range of 32-bit floating point variable is ± 10 44.85 to 10 38..53 >> How many different numbers can be represented by a 32 bit float? The MSP430, like Intel 80x86 family processors, is Little Endian --> Each location in computer memory typically holds 1 byte. Storing a 16-bit word takes 2 bytes. The lease significant byte of each 16 bit word is stored first (in the lower memory address) then the more significant byte -- > Low Byte, High Byte --> For double words the lower word is stored first then the upper word Ex: How is 65540 decimal = 00010004h is stored in memory at address 024 Little Endian Address Byte Value 02403h 02402h 02401h 024 01h 04h...... A memory dump from CCS shows addresses left to right starting with address 024 0x02400 = 04 00 01 00... << Bytes appear out of order when read L to R Many other RISC processors are Big Endian. The higher byte (big end) is stored first then the lower byte. BIG Endian Address Byte Value 02403h 02402h 02401h 024 04h 01h......
A memory dump from a big endian processor would also show addresses left to right starting with address 024 0x02400= 00 01 00 04... << But bytes appear in order when read L to R Here is an example of being Endian-ed! A nice plot of a file of unsigned integers as created on a little endian machine. Below is a plot of the same data file having being read in as unsigned integers on a big endian machine. The data is good! It is the same as above! All that has changed is the endianness of the machine that read the data. The table below shows the first few unsigned integer values from the data file created on the little endian computer as read by both of the machines. The byte swap is evident in the hexadecimal values.
Read as Little Endian Read as Big Endian 8178 1FF2 h 61983 F21F h 8193 2001 h 288 0120 h 8194 2002 h 544 0220 h 8182 1FF6 h 63007 F61F h 8201 2009 h 2336 0920 h 8201 2009 h 2336 0920 h >> Endianness DOES NOT affect order of array elements! How would the following variables be stored in memory on the MSP430F5529 (starting at address 024? How would they be stored on a Big Endian microprocessor? unsigned int a = 0x00FF; long int b[2] = {65540, 8201}; char c = 'C'; // ASCII code is 0x43 Address Little Endian (MSP430F5529) Big Endian BTW, The Byte Order for data transmitted over the Internet = BIG ENDIAN!!!