Midterm 1 topics (in one slide) The C language Functions, - - PDF document

midterm 1 topics in one slide
SMART_READER_LITE
LIVE PREVIEW

Midterm 1 topics (in one slide) The C language Functions, - - PDF document

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


slide-1
SLIDE 1

CSci 2021: Review Lecture 1

Stephen McCamant

University of Minnesota, Computer Science & Engineering

Midterm 1 topics (in one slide)

The C language

Functions, variables, and types Branches and loops Arrays, pointers, and structures

Number representation

Bits and bitwise operators Unsigned and signed integers Floating point numbers

Machine-level code representation

Instructions, operands Arithmetic and addressing modes

Outline

C language topics Exam logistics Topics in number representation Number representation problem Topics in machine code Machine code problems

C compared to other languages

Predecessor of C++, Java, other more modern languages No objects, for instance functions and no methods Most features have a direct translation to machine code

C numeric types

❝❤❛r, s❤♦rt, ✐♥t, and ❧♦♥❣ are 8, 16, 32, or 64 bits on x86-64 Unsigned integers are ✕ ✵ Mixed operands upgraded to larger size and unsigned ❢❧♦❛t and ❞♦✉❜❧❡ are 32-bit and 64-bit floating point

Kinds of variables and allocation

Local variables exist in one function execution, and go away when it is over

Even if you think you have a pointer to it!

Global variables can be accessed from any function, and last for the whole program For more control, allocate memory with ♠❛❧❧♦❝ and get a pointer

slide-2
SLIDE 2

C strings

Instead of a real string type, C programs pass pointers to characters Usually, length of string is indicated by a ❭✵ terminator Transform strings by writing loops over characters Programmer needs to be explicit about allocation and sharing

C pointers

Pointers hold addresses, and the compiler knows their type Create a pointer to a variable with ✫ Dereference a pointer with ✯ Pointer arithmetic uses the element size, like an array In fact, ❛❬①❪ is the same as ✯✭❛ ✰ ①✮

More about pointers

Pointer parameters implement pass by reference The null pointer doesn’t point at anything

So don’t dereference it

When using pointers, pay attention to data lifetime and sharing

C structures

A str✉❝t groups several related values together

Similar to objects with features removed

Commonly structs are accessed with pointers, fields with ✲❃

For instance, to implement linked lists and trees

♠❛❧❧♦❝ with the structure size is like ♥❡✇

For instance, HA1 search tree

Every search tree node is a str✉❝t

Each allocated with ♠❛❧❧♦❝

Choices for string storage:

Struct has char pointer, can reuse slurped storage Struct has char array, use str❝♣② Struct has char pointer, use str❞✉♣ Optionally, remember string length

Outline

C language topics Exam logistics Topics in number representation Number representation problem Topics in machine code Machine code problems

slide-3
SLIDE 3

Exam rules

Begins promptly at 3:35, ends promptly at 4:25 Open-book, open-notes, any paper materials OK No electronics: no laptops, smartphones, calculators, etc.

No arithmetic on big numbers needed

Leave at least one seat between students

Exam strategy suggestions

Writing implement: mechanical pencil plus good eraser Make a summary sheet to save flipping though notes or textbook Show your work when possible Do the easiest questions first Allow time to answer every question

Outline

C language topics Exam logistics Topics in number representation Number representation problem Topics in machine code Machine code problems

Bits and bitwise operations

Base 2 (binary) and base 16 (hex) generalize from base 10 (decimal) And, or, xor, not Left shift, two kinds of right shift

Similarity to multiply/divide by ✷❦

Unsigned and signed integers

Unsigned: plain base 2, non-negative

Overflow is like operations modulo ✷♥

Signed: two’s complement with a sign bit

Sign bit counts for negative place value Overflow possible in both directions

Comparing the two

Ranges partially overlap ✰, ✲, ✯ (same size output), ❁❁, ❂❂, narrowing are the same ✴, ✪, ❃❃, ❁, ✯ (high output bits), and widening are different

Algebra properties exist despite overflow

Floating point numbers

Represent fractions and larger numbers using binary scientific notation Fractions whose denominator is a power of two

All others must be rounded Limited precision gradually loses information

Rounding: examine thrown-away bits Special cases for +/- 0, +/- ✶, NaN Ordering properties but fewer algebraic properties

slide-4
SLIDE 4

Normalized and denormalized

All but the smallest finite numbers are normalized

Represent as ✶✿① ✁ ✷❡ (Leading 1 is not stored)

For smallest numbers, special denormalized form

Smallest ❡①♣ encoding: same ❊ as smallest normal Leading 0 is not stored

Outline

C language topics Exam logistics Topics in number representation Number representation problem Topics in machine code Machine code problems

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: Unsigned OF, no signed OF: Unsigned OF, positive OF: Unsigned OF, negative OF: No unsigned OF, positive OF: No unsigned OF, negative OF:

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: Unsigned OF, positive OF: Unsigned OF, negative OF: No unsigned OF, positive OF: No unsigned OF, negative OF:

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: Unsigned OF, negative OF: No unsigned OF, positive OF: No unsigned OF, negative OF:

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: No unsigned OF, positive OF: No unsigned OF, negative OF:

slide-5
SLIDE 5

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: ✶✵✵✵ + ✶✵✵✵ = ✵✵✵✵ No unsigned OF, positive OF: No unsigned OF, negative OF:

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: ✶✵✵✵ + ✶✵✵✵ = ✵✵✵✵ No unsigned OF, positive OF: ✵✶✵✵ + ✵✶✵✵ = ✶✵✵✵ No unsigned OF, negative OF:

Overflow

Which of these combinations can describe the addition of the same bits? If possible, give an example with 4-bit ints.

No unsigned OF, no signed OF: ✵✵✵✵ + ✵✵✵✵ = ✵✵✵✵ Unsigned OF, no signed OF: ✶✶✶✶ + ✵✵✵✶ = ✵✵✵✵ Unsigned OF, positive OF: can’t happen Unsigned OF, negative OF: ✶✵✵✵ + ✶✵✵✵ = ✵✵✵✵ No unsigned OF, positive OF: ✵✶✵✵ + ✵✶✵✵ = ✶✵✵✵ No unsigned OF, negative OF: can’t happen

Outline

C language topics Exam logistics Topics in number representation Number representation problem Topics in machine code Machine code problems

Instructions and operands

Assembly language ✩ machine code Sequence of instructions, encoded in bytes An instruction reads from or writes to operands

x86: usually at most one memory operand AT&T: destination is last operand AT&T shows operand size with b/w/l/q suffix

Addressing modes

General form: disp(base,index,scale)

Displacement is any constant, scale is 1, 2, 4 or 8 Base and index are registers Formula: mem[disp ✰ base ✰ index ✁ scale]

All but base are optional

Missing displacement or index: 0 Missing scale: 1 Drop trailing (but not leading) commas

Do same computation, just put address in register: ❧❡❛

slide-6
SLIDE 6

Outline

C language topics Exam logistics Topics in number representation Number representation problem Topics in machine code Machine code problems

Working with ordering

Which of these conditions are the same? ① ❁ ② ① ❃ ② ① ❁❂ ② ① ❃❂ ② ② ❁ ① ② ❃ ① ② ❁❂ ① ② ❃❂ ① ✦✭① ❁ ②✮ ✦✭① ❃ ②✮ ✦✭① ❁❂ ②✮ ✦✭① ❃❂ ②✮ ✦✭② ❁ ①✮ ✦✭② ❃ ①✮ ✦✭② ❁❂ ①✮ ✦✭② ❃❂ ①✮

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:✦✭② ❃❂ ①✮