Slide 1Midterm TopicsMidterm TopicsOverview for TodayHomework 1Slide 6Lab 1Signed vs. Unsigned in CSign ExtensionSign Extension ExampleSummary: Expanding, Truncating: Basic RulesCasting SurprisesWhy Should I Use Unsigned?Slide 14Fixed Point FractionsFloating PointFloating PointFloating Point - NormalizedSlide 19VideoFloating Point- DenormalizedFloating Point - Special ValsIA32 General Purpose RegistersSome Other RegistersA Simple ExampleWhat really happens...Slide 27Slide 28Slide 29Simple Addressing ModesSlide 31Moving DataLinux DemoMidterm TopicsCS 33 Week 2 DiscussionMidterm Topics1. Integer Binary data - encode/decode (2s complement, binary to/from decimal and hexadecimal) - size conversion (sign extend) - operations (add, multiply) - fractional fixed point2. Floating point - encode/decode normalized - denormalized - operations3. Boolean operations - truth tables (&, |, ^, ~)4. Assembly Language - operands5. Stack operationMidterm Topics1. Integer Binary data - encode/decode (2s complement, binary to/from decimal and hexadecimal) - size conversion (sign extend) - operations (add, multiply) - fractional fixed point2. Floating point - encode/decode normalized - denormalized - operations3. Boolean operations - truth tables (&, |, ^, ~)4. Assembly Language - operands5. Stack operationOverview for Today●Homework 1 Solution●Questions about Lab 1?●Unsigned/Signed Integer●Review Floating Point●Review Assembly Language●Midterm Review (Midterm 1 next Wed, Oct. 22)●C/Linux tutorialHomework 1●Determine if my system is little-endian or big-endian.23HexHex2345 674501Lab 1●Due Sunday at 6:00 PM●Please submit only lab1.c file●Questions?Carnegie Mellon8Signed vs. Unsigned in CConstantsBy default are considered to be signed integersUnsigned if have “U” as suffix0U, 4294967259UCastingExplicit casting between signed & unsigned same as U2T and T2Uint tx, ty;unsigned ux, uy;tx = (int) ux;uy = (unsigned) ty;Implicit casting also occurs via assignments and procedure callstx = ux;uy = ty;Carnegie Mellon9Sign ExtensionTask:Given w-bit signed integer xConvert it to w+k-bit integer with same valueRule:Make k copies of sign bit:X = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0k copies of MSB• • •X X • • • • • •• • •wwkCarnegie Mellon10Sign Extension ExampleConverting from smaller to larger integer data typeC automatically performs sign extension short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y;Decimal Hex Binaryx152133B 6D 00111011 01101101ix1521300 00 3B 6D 00000000 00000000 00111011 01101101y-15213C4 93 11000100 10010011iy-15213FF FF C4 93 11111111 11111111 11000100 10010011Carnegie Mellon11Summary:Expanding, Truncating: Basic RulesExpanding (e.g., short int to int)Unsigned: zeros addedSigned: sign extensionBoth yield expected resultTruncating (e.g., unsigned to unsigned short)Unsigned/signed: bits are truncatedResult reinterpretedUnsigned: mod operationSigned: similar to modFor small numbers yields expected behavourCarnegie Mellon120 0U == unsigned-1 0 < signed-1 0U > unsigned2147483647 -2147483648 > signed2147483647U -2147483648 < unsigned-1 -2 > signed(unsigned) -1 -2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signedCasting SurprisesExpression EvaluationIf there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsignedIncluding comparison operations <, >, ==, <=, >=Examples for W = 32: TMIN = -2,147,483,648 , TMAX = 2,147,483,647Constant1Constant2Relation Evaluation0 0U-1 0-1 sizeof(int)2147483647-2147483647-1 2147483647U -2147483647-1 -1 -2 (unsigned)-1-2 2147483647 2147483648U 2147483647 (int) 2147483648UCarnegie Mellon13Why Should I Use Unsigned?Don’t Use Just Because Number NonnegativeEasy to make mistakesunsigned i;for (i = cnt-2; i >= 0; i--) a[i] += a[i+1];Can be very subtle#define DELTA sizeof(int)int i;for (i = CNT; i-DELTA >= 0; i-= DELTA) . . .Do Use When Performing Modular ArithmeticMultiprecision arithmeticDo Use When Using Bits to Represent SetsLogical right shift, no sign extensionCarnegie Mellon14A Programmer’s PerspectiveFixed Point FractionsDecimal:42.2510 = 101*4 + 100*2 + 10-1*2 + 10-2*5 = 10 *4 + 1 *2 + 0.1*2 + 0.01*5Binary:6.2510 = 110.012 = 22*1 + 21*1 + 20*0 + 2-1*0 + 2-2*1 = 4 *1 + 2 *1 + 1 *0 + ½*0 + ¼ *1What are the problems with this type of representation of fractions?Floating Point●The idea is to store a fractional number in a more compact form, similar to using scientific notation in decimal.●In base 10, we can store very large numbers and very small numbers using powers of 10.4.505 * 1015 HUGE3.768 * 10-25 tinySimilarly, we may store Binary numbers in this way:1011000 * 2201010110 * 2-15Say we want to store a really small number, like 0.00000001012We can normalize it by moving the decimal place over so it begins with a 1.This becomes 0.00000001012 = 1.012 * 2-8Just like in Base 10, think of 2-8 as moving the decimal place over 8 spots.In general, the Value V = (-1)S * M * 2Ewhere S = Sign (+), M = Mantissa (1.012), E = Exponent (-8)Floating PointFloating Point - Normalized- exponent is not all 0s and not all 1s. Leading 1 assumed. Mantissa = 1.f●Normalized is the most common case for floating point numbers.●s is a 0 or 1 for a positive or negative number.●f represents the fraction without the leading 1 (which is assumed).○e.g. 0.00010111 is 1.0111 * 2-4○So here our Mantissa is 1.0111 and Exponent is -4○But we assume there is a leading 1 in the Mantissa (because normalized), so we save a bit of memory and only store bits 0111 as f.●e represents the exponent with a “Bias” added on, called a signed integer in biased form.○We add 2k-1-1 to the exponent where k is the number of bits allocated for the exponent, then store that number. For single precision, this is 28-1-1 = 127○For the above example, our exponent is -4, so we would add 127 to that and store 123 in unsigned binary form.Thus our normalized representation of 0.00010111 is0 01111011 01110000000000000000000eFormula for converting decimal to normalized floating point numberExample number = 3/321. Convert Base 10 number to base 2 number3/32 = 1/16 + 1/32 = 2-4 + 2-5 = 0.000112. Is your number positive or
View Full Document