CS 105 Tour of the Black Holes of Computing Computer Systems Introduction Geoff Kuenning Spring 2010 Topics 1 Staff text and policies Lecture topics and assignments Lab rationale CS 105 Course Theme Abstraction is good but don t forget reality Many CS Courses emphasize abstraction Abstract data types Asymptotic analysis These abstractions have limits Especially in the presence of bugs Need to understand underlying implementations Useful outcomes Become more effective programmers Able to find and eliminate bugs efficiently Able to tune program performance Prepare for later systems classes in CS Compilers Operating Systems Networks Computer 2 Architecture Robotics etc CS 105 Textbooks Randal E Bryant and David R O Hallaron Computer Systems A Programmer s Perspective Prentice Hall 2003 Brian Kernighan and Dennis Ritchie The C Programming Language Second Edition Prentice Hall 1988 Larry Miller and Alex Quilici 3 The Joy of C Wiley 1997 CS 105 Syllabus 4 Syllabus on Web http www cs hmc edu geoff cs105 Calendar defines due dates Labs cs105submit for some others have specific directions CS 105 Notes Work groups You must work in pairs on all labs Honor code violation to work without your partner Corollary showing up late doesn t harm only you Handins Check calendar Electronic submissions only Grading Characteristics Lab scores tend to be high Serious handicap if you don t hand a lab in Tests quizzes typically have a wider range of scores I e they re primary determinant of your grade but not the ONLY one 5 Do your share of lab work and reading or bomb tests CS 105 Facilities Assignments will use Intel computer systems Not all machines are created alike Some Macs are PowerPCs Even Intel Macs aren t necessarily compatible Knuth is a 64 bit server Wilkes x86 Linux specifically set up for this class Log in on a Mac then ssh to Wilkes If you want fancy programs start X11 first Directories are cross mounted so you can edit on Knuth or your Mac and Wilkes will see your files 6 or ssh into Wilkes from your dorm All programs must run on Wilkes that s where we grade CS 105 CS 105 Tour of the Black Holes of Computing Integers Topics Numeric Encodings Unsigned two s complement Programming Implications C promotion rules Basic operations Addition negation multiplication Programming Implications Consequences of overflow Using shifts to perform power of 2 multiply divide CS 105 C Puzzles Taken from old exams Assume machine with 32 bit word size two s complement integers For each of the following C expressions either Argue that it is true for all argument values Give example where it is not true x 0 Initialization x 2 0 ux 0 x 7 7 x 30 0 int x foo ux 1 int y bar x y unsigned ux x x x 0 unsigned uy y x 0 y 0 x y 0 8 x y x 0 x 0 x 0 x 0 CS 105 Encoding Integers Unsigned B2U X w 1 xi 2 Two s Complement i B2T X xw 1 2 i 0 w 1 w 2 xi 2 i i 0 short int x 15213 short int y 15213 Sign Bit C short 2 bytes long x y Decimal 15213 15213 Hex 3B 6D C4 93 Binary 00111011 01101101 11000100 10010011 Sign Bit For 2 s complement most significant bit indicates sign 0 for nonnegative 1 for negative 9 CS 105 Encoding Integers Cont x y 10 Weight 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 Sum 15213 00111011 01101101 15213 11000100 10010011 15213 1 1 0 0 1 4 1 8 0 0 1 32 1 64 0 0 1 256 1 512 0 0 1 2048 1 4096 1 8192 0 0 0 0 15213 15213 1 1 1 2 0 0 0 0 1 16 0 0 0 0 1 128 0 0 0 0 1 1024 0 0 0 0 0 0 1 16384 1 32768 15213 CS 105 Numeric Ranges Unsigned Values UMin 0 000 0 UMax 111 1 Two s Complement Values TMin 2w 1 100 0 2w 1 TMax 011 1 2w 1 1 Other Values Minus 1 111 1 Values for W 16 UMax TMax TMin 1 0 11 Decimal 65535 32767 32768 1 0 Hex FF FF 7F FF 80 00 FF FF 00 00 Binary 11111111 11111111 01111111 11111111 10000000 00000000 11111111 11111111 00000000 00000000 CS 105 Values for Different Word Sizes Observations TMin C Programming TMax 1 Asymmetric range UMax 2 TMax 1 include limits h K R Appendix B11 Declares constants e g ULONG MAX LONG MAX LONG MIN 12 Values platform specific CS 105 Unsigned Signed Numeric Values 13 X 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 B2U X 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B2T X 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 Equivalence Same encodings for nonnegative values Uniqueness Every bit pattern represents unique integer value Each representable integer has unique bit encoding CS 105 Casting Signed to Unsigned C Allows Conversions from Signed to Unsigned short int x 15213 unsigned short int ux unsigned short x short int y 15213 unsigned short int uy unsigned short y Resulting Value No change in bit representation Nonnegative values unchanged ux 15213 Negative values change into large positive values uy 50323 14 CS 105 Relation Between Signed Unsigned 15 uy Weight 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 Sum 15213 1 1 1 2 0 0 0 0 1 16 0 0 0 0 1 128 0 0 0 0 1 1024 0 0 0 0 0 0 1 16384 1 32768 15213 y 2 32768 50323 1 1 1 2 0 0 0 0 1 16 0 0 0 0 1 128 0 0 0 0 1 1024 0 0 0 0 0 0 1 16384 1 32768 50323 y 65536 CS 105 Signed vs Unsigned in C Constants By default are considered to be signed integers Unsigned if have U as suffix 0U 4294967259u Casting Explicit casting between signed unsigned same as U2T and T2U int tx ty unsigned ux uy tx int ux uy unsigned ty Implicit casting also occurs via assignments and procedure calls tx ux uy ty 16 CS 105 Casting Surprises Expression Evaluation If mix unsigned and signed in single expression signed values implicitly cast to unsigned Including comparison operations Examples for W 32 Constant1 Constant2 0 0u 1 0 1 0u 2147483647 2147483648 2147483647u 2147483648 1 2 Relation Evaluation unsigned 1 2 17 2147483647 2147483648u 2147483647 int 2147483648u CS 105 Casting Surprises Expression Evaluation If you mix unsigned and signed in single expression signed values are implicitly cast to unsigned Including comparison operations Examples for W 32 Constant1 0 0 1 Constant2 0u 0 0U unsigned 1 1 0u 0 signed 2147483647 1 2147483648 0U unsigned 2147483647u 2147483647 2147483648 2147483648 signed 2147483647U 2 2147483648 unsigned 1 signed unsigned unsigned 1 unsigned 1 …
View Full Document
Unlocking...