Computer Science I CSCI Summer 2009 David E. Goldschmidt, Ph.D.
Hardware
Central Processing Unit (CPU) Arithmetic Logic Unit Control Unit CPU Instruction (input)Result (output) e.g. addition, subtraction, logical AND, OR, NOT executes machine language programs
Memory A section of memory is called a byte. A section of two or four bytes is often called a word. Main memory can be visualized as a column or row of cells. 0x000 0x001 0x003 0x002 0x004 0x005 0x006 0x007 A byte is made up of 8 bits
Low-Level Languages Machine language program (executable file) LDA #47 STA $570 DEX JSR $817 CPX #0 BNE #14 Assembly language program Translation program (Assembler)
Compiling a C/C++ Program #include int main() { float x; cout << “... C/C++ program C/C++ Compiler Machine language program (object “.obj” file) Precompiled Libraries (e.g. iostream) C/C++ Linker Machine language program (executable “.exe” file)
Fetch-Decode-Execute Cycle Fetch The CPU’s control unit fetches, from main memory, the next instruction in the sequence of program instructions. Decode The instruction is encoded in the form of a number. The control unit decodes the instruction and generates an electronic signal. Execute The signal is routed to the appropriate component of the computer (such as the ALU, a disk drive, or some other device). The signal causes the component to perform an operation. The CPU performs the fetch-decode- execute cycle to “run” program instructions
Numbering Systems We count using our fingers.... Base 10: Computers count using binary digits or bits.... Base 2:
Base Conversion A byte is a grouping of 8 bits Convert an unsigned binary number ( ) to decimal (base 10): What about negative integers? base 10 value = unsigned byte ranges from 0 to 255
Two’s Complement Conversion from binary to decimal is identical, except the leftmost bit always has a negative weight base 10 value +++ = two’s complement byte ranges from -128 to 127
Base Conversion (again) Convert from base 10 to base 2: Find the largest power of 2 that’s less than or equal to the number you’re trying to convert Subtract it from base 10 number and repeat... For example:
Algorithms What is an algorithm? A solution to a problem A recipe A step-by-step set of English instructions that describes how inputs are processed to produce expected outputs Write an algorithm to change a light bulb Write an algorithm to get driving directions from Albany to Cape Cod
Algorithms Write an algorithm to sum integers 1 to 100 Write an algorithm to convert an unsigned byte (e.g ) from base 2 to base 10 Write an algorithm to convert a signed two’s complement byte (e.g ) from base 2 to base 10