CSC 2210: Midterm 1 review
Draft!
- Note 1: motivation for learning C, C++
- Note 2: basic C++
- Java design goal: run anywhere
- Type system: ensure computation gives same result regardless of
hardware
- Depend on just-in-time compiler to make things as efficient as possible
- What is the impact of this on run-time performance?
- Side-effect: runtime checks for array accesses, pointers, undefined
values
- C/C++ design goal: efficient computation at hardware level
- Type, runtime system: maximize efficiency, keeping programmers in
control of any resources used
- Simple input/output, basic types, basic operations
- declarations,
sizeof
,
scope
, lifetime, constants, arrays
- C++ functions, passing arrays
- overloaded functions, operators, optional arguments
Note 3: computer internals
- types of storage, CPU, memory, bus
- Moore's law, how speed of light is critical to computation
- register, bit, byte, word
- big-endian/byte ordering
- binary, 1s-complement, 2s-complement, hexadecimal
- Note 4: x86 assembly programming
- 8086, 8087, 80386, Core, I Series processors
- 8086, 80386, 64-bit register names (especially general purpose
registers, stack pointer, stack base pointer)
mov
, add
, sub
, imul
(2 operand),
and
, or
, xor
, not
,
neg
, push
, pop
.data
, .text
, .global
, ret
- Why x86 distinguish
between
movb
, movw
, movl
, movq
- Note 5: labels and addresses
- What is a label in assembly code?
jmp
cmp X Y
: know it computes Y - X
(same as sub
)
- ZF: Y - X == 0
- SF: sign of result of Y - X
- CF: carry flag (for now, just know it is set by arithmetic operations)
je
, jne
: jump on ZF
jg
, jge
, jl
, jle
:
if you need to write code with these, we will remind you which flags
are used
nop
, concept of a pseudo-instruction
- well-defined vs. undefined vs. random
- stack frame, address
(%rXX)
vs %rXX
- see additional concepts in review section to note 5
Exercises
- In C++, read a list of numbers (until end of file) and count the
number of 0s
- Convert your solution to the first exercise into a program that
reads the data into an array (in main) and then calls a function
to compute the number of 0s
- explain the elements of a compiled form of the program you wrote
above.
- Note: it's often easier to describe the code generated by
g++ -S -O prog.cpp
because the first level of optimization moves computations out of the stack
frame and into registers
- Why would unoptimized code rely heavily on the stack frame?
- Why would a programmer desire the compiler to use registers
instead?
- What is the cost of this optimization?