Introduction What is Programming? For some given problem:

advertisement
CS 104: Applied C++
(Read Chap. 1
Introduction
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
1
1
language that the computer can
understand.
What kinds of statements
do computers understand?
RISC
• A computer only understands a
CISC
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"
2
2
Machine language programs thus consists of strings
of _________ (____________________)
Example (hypothetical)
000100000000000000000100000000101001001
000000000000001000000000100100011000000
000000010000000010000100010000000000000
100000000110001000000000011010001000000
000000100100000000000000010000011001001
000110000000000000100000000100001000100
000000100001000000001100011100000000000
0010...
3
3
Bits are usually grouped into _________ (8 bits) and
__________ (e.g., 32 or 64 bits). Each machine
language instruction might be stored in one word.
e.g.,
"Store"
in memory
location with
this address
1
opcod
e
first operand
second operand
000100000000000000010100000000000001001
000000000000001000000000100100011000000
000000010000000010000100010000000000000
100000000110001000000000011010001000000
instruction
000000100100000000000000010000011001001
•instruction#1
#1
000110000000000000100000000100001000100
000000100001000000001100011100000000000
4
0010...
4
So a sequence of machine language instructions
might be stored in a sequence of consecutive
words.
instruction #1
#2
000100000000000000010100000000000001001
#3
000000000000001000000000100100011000000
#4
000000010000000010000100010000000000000
100000000110001000000000011010001000000
000000100100000000000000010000011001001
000110000000000000100000000100001000100
000000100001000000001100011100000000000
0010...
5
5
A Real Machine-Language Example
SPARC executable machine code
Intel Pentium executable machine code
000001110111010100000101 000001000110000100000110
000000000000010000000010 000000000000010000000000
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000000000
000000000000000000000010 000000000000000000000010
000000000000000000000000 000000000000000000000001
000000000000000000000001 000000000011010000110000
000000000000000000000000 000000000000000001100100
000000000000000000000000 000000010011000100100100
000000000000000000000000 000000000000000000000000
000000000000000001100100 000000000000000001000000
000000000000000000000101 000000000000000001010000
000000000000000000110011 000000000000000000110001
000000000000000000000000 000000000000000000000110
000000000000000000000000 000000000000000001100100
000000000000000000000001 000000000000000001100100
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000001001000000
000000000000000000000000 000000000000001001000000
000000000000000000000000 000000000000000000000101
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000000011
000000000000000000000000 000000000000001100100100
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000100001
000000100111000000010001 000001100100010101000110
000001100010010101010100 000000100001000000010001
000001100111000101010001 000001110001000101100100
000000100111000101010111 000000100001000101000011
000000000101000000010101 000001100001010101000111
000000110001000101000011 000001100001010100110111
000001100110010101010111 000001100100010101100000
000001100010010101010100 000000100111000101000100
000000000110010001110010 000001010111010000010010
000001010111010100110111 000001100111000101000111
000001010111010101100101 000001100111010101000011
000001110000000101010101 000001100110000101010001
000001100010000101000101 000001100001010100110111
000000000110010001110010 000000000100010000010010
000001100010000001010110 000001100011000101000101
000001010111010000010001 000001010111010100110111
000001100000010101010101 000001100111000101010001
000000000100010001110011 000001110001010001010110
000001100110000101000011 000000110001000000010001
000000000100010001110011 000001110010000001010110
000001110000000101110001 000000000100010101000101
000000110001000001100011 000000000100010001110011
000001100010010001010110 000001100010000101010110
000001100011000101000101 000000000101000000010101
000001110010000001010110 000001110100000101000101
000000000110010101100100 000000000100010000010010
… this goes on for another 1600 lines...
C++
int main()
{
int x, y;
x = 1;
y = x + 2;
return 0;
}
… this goes on for another 74 lines...
6
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
1000000010101001
1100010000000010
1000000110111100
8
8
The Real Example
SPARC assembly language:
main:
save
mov
st
ld
add
st
mov
b
nop
mov
b
nop
.LL2:
ret
restore
Intel Pentium assembly language:
_main:
%sp, -120, %sp
1, %o0
%o0, [%fp-20]
[%fp-20], %o0
%o0, 2, %o1
%o1, [%fp-24]
0, %i0
Sun
.LL2
Assembler
0, %i0
.LL2
000000100111000000010001 000001100100010101000110
000001100010010101010100 000000100001000000010001
000001100111000101010001 000001110001000101100100
000000100111000101010111 000000100001000101000011
000000000101000000010101 000001100001010101000111
000000110001000101000011 000001100001010100110111
000001100110010101010111 000001100100010101100000
000001100010010101010100 000000100111000101000100
000000000110010001110010 000001010111010000010010
000001010111010100110111 000001100111000101000111
000001010111010101100101 000001100111010101000011
000001110000000101010101 000001100110000101010001
000001100010000101000101 000001100001010100110111
000000000110010001110010 000000000100010000010010
000001100010000001010110 000001100011000101000101
000001010111010000010001 000001010111010100110111
000001100000010101010101 000001100111000101010001
000000000100010001110011 000001110001010001010110
000001100110000101000011 000000110001000000010001
000000000100010001110011 000001110010000001010110
000001110000000101110001 000000000100010101000101
000000110001000001100011 000000000100010001110011
000001100010010001010110 000001100010000101010110
000001100011000101000101 000000000101000000010101
000001110010000001010110 000001110100000101000101
000000000110010101100100 000000000100010000010010
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
9
Intel
Assembler
000001110111010100000101 000001000110000100000110
000000000000010000000010 000000000000010000000000
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000000000
000000000000000000000010 000000000000000000000010
000000000000000000000000 000000000000000000000001
000000000000000000000001 000000000011010000110000
000000000000000000000000 000000000000000001100100
000000000000000000000000 000000010011000100100100
000000000000000000000000 000000000000000000000000
000000000000000001100100 000000000000000001000000
000000000000000000000101 000000000000000001010000
000000000000000000110011 000000000000000000110001
000000000000000000000000 000000000000000000000110
000000000000000000000000 000000000000000001100100
000000000000000000000001 000000000000000001100100
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000001001000000
000000000000000000000000 000000000000001001000000
000000000000000000000000 000000000000000000000101
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000000011
000000000000000000000000 000000000000001100100100
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000000000
000000000000000000000000 000000000000000000100001
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
10
Next Major Advance:
High Level Languages & Compilers
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 .
Why not just use human language?
It’s too complex and ambiguous; e.g.,
11
“Time flies like an arrow”
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.
1000000000110101
z = a * (b + c);
LOAD b
ADD c
STORE temp1
LOAD a
MULT temp1
STORE z
Compiler
1100010000110110
1000000101000001
1000000000110100
1100011001000001
1000000100110111
Assembler
12
12
Advantages of High-Level Languages
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
13
Objectives in Programming
A program to solve a problem should be:
Grading
criteria
+ correct (it actually solves the problem)
+efficient (doesn’t waste time or space)
Later
+ readable (understandable by another person)
+ user-friendly (designed in a way that is easy for
its user to use).
14
14
Download