Introduction CS 104: Applied C++ What is Programming? For some given problem: __________ a solution for it -- identify, organize & store the problem's __________ -- develop _______________ (procedures) to process it; ______ this solution as a program; ______ the program; __________ the program (fix it, upgrade it, …). What is a program? — a collection of statements that — implement the design plan, and — are written in a programming language — a language that the computer can understand. (Read Chap
What kinds of statements do computers understand? 2 A computer only understands a language specially designed for it called _______________ language. Machine-language statements are stored in a computer’s memory, which is a sequence of two-state devices (on-off switches). They are retrieved from memory and executed one at a time. The "character set" for machine language: ___ representing "off" ___ representing "on" RISC CISC 2
Machine language programs thus consists of strings of _________ (____________________) 3 Example (hypothetical)
Bits are usually grouped into _________ (8 bits) and __________ (e.g., 32 or 64 bits). Each machine language instruction might be stored in one word. 4 opcod e instruction #1 first operandsecond operand e.g., "Store" 1 in memory location with this address 4
So a sequence of machine language instructions might be stored in a sequence of consecutive words instruction #1 #4 #3 #2 5
A Real Machine-Language Example 6 SPARC executable machine code … this goes on for another 1600 lines... Intel Pentium executable machine code … this goes on for another 74 lines... int main() { int x, y; x = 1; y = x + 2; return 0; } C++ 6
Early Computers Required a programmer to write in machine language –Very easy to make mistakes! –And they were hard to find! –Programs were not ___________ They could only be run on one kind of machine! Result: programming was very difficult and programs weren't widely used. 7 7
An Early Innovation Devise a set of mnemonics (abbreviations), one for each machine language instruction; this was called an _____________ language Create a machine-language program called an ___________to input each assembly language instruction and translate it into machine language LOAD x ADD 2 STORE y Assembler 8 8
9 The Real Example Intel Pentium assembly language: _main: pushl %ebp movl %esp,%ebp subl $24,%esp call ___main movl $1,-4(%ebp) movl -4(%ebp),%eax addl $2,%eax movl %eax,-8(%ebp) xorl %eax,%eax jmp L2.align 4 L2: movl %ebp,%esp popl %ebp ret Intel Assembler SPARC assembly language: main: save%sp, -120, %sp mov1, %o0 st%o0, [%fp-20] ld[%fp-20], %o0 add%o0, 2, %o1 st%o1, [%fp-24] mov0, %i0 b.LL2 nop mov0, %i0 b.LL2 nop.LL2: ret restore Sun Assembler 9
Assembly Languages Allowed a programmer to use mnemonics, which were more natural than binary. +Much easier to read programs +Much easier to find and fix mistakes –Still ______________ to different machines –Still quite difficult to write, read, and debug programs 10
To improve on assembly language: Devise a set of statements called a ____________ language that are closer to __language and methods of writing expressions and A program called a ___________to translate them into machine language. 11 Why not just use human language? It’s too complex and ambiguous; e.g., “Time flies like an arrow” Next Major Advance: High Level Languages & Compilers 11
Compilers vs. Assemblers An assembler translates one assembly-language statement into ______ machine-language statement. A compiler translates one high-level statement into ______________ machine-language statements so it is much more difficult to write a correct compiler than an assembler. LOAD b ADD c STORE temp1 LOAD a MULT temp1 STORE z Assembler z = a * (b + c); Compiler 12
With programming in high-level languages (e.g., C++): +Programs are much easier to read. +Mistakes are much easier to find and fix. +Programs are (or can be) ___________from one machine to another (provided they conform to the language standard). Just need a compiler for that language written in the machine language of that machine. Not so simple that just anyone can use them (otherwise this course wouldn't exist) 13 Advantages of High-Level Languages 13
Objectives in Programming A program to solve a problem should be: +correct (it actually solves the problem) +readable (understandable by another person) +user-friendly (designed in a way that is easy for its user to use). 14 Grading criteria Later +efficient (doesn’t waste time or space) 14