Midterm 1 topics (in one slide) The C language Functions, variables, and types Branches and loops Arrays, pointers, and structures CSci 2021: Review Lecture 1 Number representation Bits and bitwise operators Stephen McCamant Unsigned and signed integers University of Minnesota, Computer Science & Engineering Floating point numbers Machine-level code representation Instructions, operands Arithmetic and addressing modes Outline C compared to other languages C language topics Predecessor of C++, Java, other more modern Exam logistics languages Topics in number representation No objects, for instance functions and no methods Number representation problem Most features have a direct translation to Topics in machine code machine code Machine code problems C numeric types Kinds of variables and allocation ❝❤❛r , s❤♦rt , ✐♥t , and ❧♦♥❣ are 8, 16, 32, or Local variables exist in one function execution, 64 bits on x86-64 and go away when it is over Even if you think you have a pointer to it! Unsigned integers are ✕ ✵ Global variables can be accessed from any Mixed operands upgraded to larger size and function, and last for the whole program unsigned For more control, allocate memory with ♠❛❧❧♦❝ ❢❧♦❛t and ❞♦✉❜❧❡ are 32-bit and 64-bit and get a pointer floating point
C strings C pointers Instead of a real string type, C programs pass Pointers hold addresses, and the compiler pointers to characters knows their type Usually, length of string is indicated by a ❭✵ Create a pointer to a variable with ✫ terminator Dereference a pointer with ✯ Transform strings by writing loops over Pointer arithmetic uses the element size, like an characters array Programmer needs to be explicit about In fact, ❛❬①❪ is the same as ✯✭❛ ✰ ①✮ allocation and sharing More about pointers C structures A str✉❝t groups several related values Pointer parameters implement pass by together reference Similar to objects with features removed The null pointer doesn’t point at anything Commonly structs are accessed with pointers, So don’t dereference it fields with ✲❃ When using pointers, pay attention to data For instance, to implement linked lists and trees lifetime and sharing ♠❛❧❧♦❝ with the structure size is like ♥❡✇ For instance, HA1 search tree Outline C language topics Every search tree node is a str✉❝t Exam logistics Each allocated with ♠❛❧❧♦❝ Topics in number representation Choices for string storage: Struct has char pointer, can reuse slurped storage Number representation problem Struct has char array, use str❝♣② Struct has char pointer, use str❞✉♣ Topics in machine code Optionally, remember string length Machine code problems
Exam rules Exam strategy suggestions Writing implement: mechanical pencil plus good Begins promptly at 3:35, ends promptly at 4:25 eraser Open-book, open-notes, any paper materials OK Make a summary sheet to save flipping though No electronics: no laptops, smartphones, notes or textbook calculators, etc. Show your work when possible No arithmetic on big numbers needed Do the easiest questions first Leave at least one seat between students Allow time to answer every question Outline Bits and bitwise operations C language topics Exam logistics Base 2 (binary) and base 16 (hex) generalize from base 10 (decimal) Topics in number representation And, or, xor, not Number representation problem Left shift, two kinds of right shift Similarity to multiply/divide by ✷ ❦ Topics in machine code Machine code problems Unsigned and signed integers Floating point numbers Unsigned: plain base 2, non-negative Represent fractions and larger numbers using Overflow is like operations modulo ✷ ♥ binary scientific notation Signed: two’s complement with a sign bit Fractions whose denominator is a power of two Sign bit counts for negative place value All others must be rounded Overflow possible in both directions Limited precision gradually loses information Comparing the two Rounding: examine thrown-away bits Ranges partially overlap ✰ , ✲ , ✯ (same size output), ❁❁ , ❂❂ , narrowing are the Special cases for +/- 0, +/- ✶ , NaN same ✴ , ✪ , ❃❃ , ❁ , ✯ (high output bits), and widening are Ordering properties but fewer algebraic different properties Algebra properties exist despite overflow
Normalized and denormalized Outline C language topics All but the smallest finite numbers are Exam logistics normalized Represent as ✶✿① ✁ ✷ ❡ Topics in number representation (Leading 1 is not stored) For smallest numbers, special denormalized Number representation problem form Smallest ❡①♣ encoding: same ❊ as smallest normal Topics in machine code Leading 0 is not stored Machine code problems Overflow Overflow Which of these combinations can describe the Which of these combinations can describe the addition of the same bits? If possible, give an addition of the same bits? If possible, give an example with 4-bit ints. example with 4-bit ints. No unsigned OF, no signed OF: No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: Unsigned OF, no signed OF: Unsigned OF, positive OF: Unsigned OF, positive OF: Unsigned OF, negative OF: Unsigned OF, negative OF: No unsigned OF, positive OF: No unsigned OF, positive OF: No unsigned OF, negative OF: No unsigned OF, negative OF: Overflow Overflow Which of these combinations can describe the Which of these combinations can describe the addition of the same bits? If possible, give an addition of the same bits? If possible, give an example with 4-bit ints. example with 4-bit ints. No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: Unsigned OF, negative OF: No unsigned OF, positive OF: No unsigned OF, positive OF: No unsigned OF, negative OF: No unsigned OF, negative OF:
Overflow Overflow Which of these combinations can describe the Which of these combinations can describe the addition of the same bits? If possible, give an addition of the same bits? If possible, give an example with 4-bit ints. example with 4-bit ints. No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: can’t happen Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: ✶✵✵✵ + ✶✵✵✵ = ✵✵✵✵ Unsigned OF, negative OF: ✶✵✵✵ + ✶✵✵✵ = ✵✵✵✵ No unsigned OF, positive OF: No unsigned OF, positive OF: ✵✶✵✵ + ✵✶✵✵ = ✶✵✵✵ No unsigned OF, negative OF: No unsigned OF, negative OF: Overflow Outline C language topics Which of these combinations can describe the addition of the same bits? If possible, give an Exam logistics example with 4-bit ints. Topics in number representation No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Number representation problem Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: ✶✵✵✵ + ✶✵✵✵ = ✵✵✵✵ Topics in machine code No unsigned OF, positive OF: ✵✶✵✵ + ✵✶✵✵ = ✶✵✵✵ No unsigned OF, negative OF: can’t happen Machine code problems Instructions and operands Addressing modes General form: disp(base,index,scale) Displacement is any constant, scale is 1, 2, 4 or 8 Assembly language ✩ machine code Base and index are registers Sequence of instructions, encoded in bytes Formula: mem[disp ✰ base ✰ index ✁ scale] All but base are optional An instruction reads from or writes to operands Missing displacement or index: 0 x86: usually at most one memory operand Missing scale: 1 AT&T: destination is last operand Drop trailing (but not leading) commas AT&T shows operand size with b/w/l/q suffix Do same computation, just put address in register: ❧❡❛
Outline Working with ordering C language topics Exam logistics Which of these conditions are the same? ① ❁ ② ① ❃ ② ① ❁❂ ② ① ❃❂ ② Topics in number representation ② ❁ ① ② ❃ ① ② ❁❂ ① ② ❃❂ ① ✦✭① ❁ ②✮ ✦✭① ❃ ②✮ ✦✭① ❁❂ ②✮ ✦✭① ❃❂ ②✮ Number representation problem ✦✭② ❁ ①✮ ✦✭② ❃ ①✮ ✦✭② ❁❂ ①✮ ✦✭② ❃❂ ①✮ Topics in machine code Machine code problems Working with ordering Which of these conditions are the same? Col. 1 Col. 2 Col. 3 Col. 4 A: ① ❁ ② B: ① ❃ ② C: ① ❁❂ ② D: ① ❃❂ ② B: ② ❁ ① A: ② ❃ ① D: ② ❁❂ ① C: ② ❃❂ ① D: ✦✭① ❁ ②✮ C: ✦✭① ❃ ②✮ B: ✦✭① ❁❂ ②✮ A: ✦✭① ❃❂ ②✮ C: ✦✭② ❁ ①✮ D: ✦✭② ❃ ①✮ A: ✦✭② ❁❂ ①✮ B: ✦✭② ❃❂ ①✮
Recommend
More recommend