Lecture 11 Important Reminder! 2/19/04 13:36

advertisement
Lecture 11
2/19/04 13:36
Important Reminder!
• We hope you now have a clear
understanding of what CS 100 is about and
what is expected of you
• We hope that you have discovered that, by
applying yourself, you can learn many
interesting ideas from computer science
• However, if this is not the course for you,
we remind you that you can still withdraw
(until February 23)
Lecture 11
System Software
& Assembly Language
(S&G, §§6.1
–6.3)
§§6.1–
2/19/04
CS 100 - Lecture 11
1
2/19/04
CS 100 - Lecture 11
2
Putting Clothes
on the Naked Machine
Read S&G §6.4
(Operating Systems)
for Thursday
• Imagine if we had to use the “naked” Von
Neumann machine discussed last week
–
–
–
–
Must program purely in binary (1’s and 0’s)
Must not make mistakes
Must manually load instructions into memory
Must manually load address of first instruction into PC
• Bottom line: Von Neumann machine designed
from perspective of machine, not humans!
2/19/04
CS 100 - Lecture 11
3
2/19/04
CS 100 - Lecture 11
4
(slide < C. Hundhausen)
Putting Clothes
on the Naked Machine (2)
Putting Clothes
on the Naked Machine (3)
• The Von Neumann Machine needs a user
interface
Example: Carry out the computation:
if (a > b) then
set c to a + b
else
set c to a – b
write value of c to disk
– Hide messy details of hardware from user
– Communicate with user using the user’s language, not
binary
– Provide easy access to computer’s resources
– Prevent user from damaging hardware
• System software provides just such an interface;
think of it as a well-designed dashboard
2/19/04
CS 100 - Lecture 11
(slide adapted < C. Hundhausen)
CS 100
5
2/19/04
CS 100 - Lecture 11
6
(slide adapted < C. Hundhausen)
1
Lecture 11
2/19/04 13:36
Putting Clothes
on the Naked Machine (4)
System Software
• Imagine if you were responsible for
–
–
–
–
Fetching the values a, b from memory
Loading them into ALU registers
Putting the result into memory location c
Writing c to proper sector on a disk
• Should a programmer really have to worry about
such things as registers and the ALU? No!
• System software provides a virtual machine that’s
much friendlier than the Von Neumann machine
2/19/04
CS 100 - Lecture 11
7
Types of system software:
• Language translators
– Assemblers
• Translate assembly language into machine language
– Compilers
• Translate high-level programming languages into assembly
language or machine language
2/19/04
(slide adapt. < C. Hundhausen)
CS 100 - Lecture 11
8
(slide adapt. < C. Hundhausen)
System Software (2)
System Software (3)
Types of system software (cont.)
• Memory managers
– Allocate and manage memory space for programs and data; load
data and programs into memory
•
• We do not have time to cover all types of
system software in a single week
• In order to get a taste of virtual machines
and systems software, we will focus on two
specific types of system software
File systems
– Allow us to read from and write to to mass storage devices like
hard disks and CD-ROMs
•
Scheduler
– Schedules programs to be executed; maintains priority queue so
that higher priority programs are executed first
•
Utilities
–
–
–
–
2/19/04
– Assemblers (this lecture)
– Operating systems (next lecture)
Text editors
Graphics libraries
Graphical user interface libraries
Debuggers
CS 100 - Lecture 11
9
2/19/04
(slide < C. Hundhausen)
CS 100 - Lecture 11
10
(slide < C. Hundhausen)
Assembly Language
•
As we have learned, it is difficult to write programs in
machine language
– We don’t deal well with 1’s and 0’s
– We don’t deal well with numeric variable names
– Machine language is difficult to change
Assemblers and
Assembly Language
•
Assembly language is a first step toward a more userfriendly programming language
– Supports English command set (e.g., LOAD, STORE)
• Each command translates directly into a single machine language
command
– Supports mnemonic variable naming (e.g., “COUNT”)
– Supports “pseudo-ops” for data generation
– Supports comments
2/19/04
CS 100 - Lecture 11
11
2/19/04
CS 100 - Lecture 11
12
(slide adapt. < C. Hundhausen)
CS 100
2
Lecture 11
2/19/04 13:36
Assembly Language (cont.)
Example: Output Sum of 1, …, 10
Locating assembly language on the
programming language continuum:
2/19/04
CS 100 - Lecture 11
13
2/19/04
(slide < C. Hundhausen)
clear sum
load one
store count
load count
compare eleven
jumpeq line 12
add sum
store sum
increment count
jump line 4
out sum
halt
2/19/04
000000000000
000000000001
000000000010
000000000011
000000000100
000000000101
000000000110
000000000111
000000001000
000000001001
000000001010
000000001011
000000001100
000000001101
000000001110
000000001111
000000010000
1101
0000
0001
0000
0111
1010
0011
0001
0100
1000
1110
1111
0000
0000
0000
0000
0000
000000001111
000000001101
000000010000
000000010000
000000011110
000000001010
000000001111
000000001111
000000010000
000000000011
000000001111
000000000000
000000000000
000000000001
000000001011
000000000000
000000000000
CS 100 - Lecture 11
14
(zero)
(one)
(eleven)
(sum)
(count)
15
• Binary op codes are difficult to remember
• Uses binary addresses for data, but humans
are used to symbols (PI, LENGTH, etc.)
• Hard to tell the difference between data and
instructions
2/19/04
CS 100 - Lecture 11
16
(slide < S. Levy)
Labeling Principle
Tedious and Error-Prone
• Labeling Principle:
• Editing code is unbelievably difficult
– Avoid arbitrary sequences more than a few
items long
– Do not require the user to know the absolute
position of an item in a list
– Instead, associate a meaningful label with each
item and allow the items to occur in any order
• Example: symbolic labels for instructions &
data
CS 100 - Lecture 11
CS 100 - Lecture 11
Problems With Machine Language
(slide < S. Levy)
2/19/04
clear sum
load one
store count
load count
compare eleven
jumpeq line 12
add sum
store sum
increment count
jump line 4
out sum
halt
(slide < S. Levy)
Example: Output Sum of 1, …, 10
1
2
3
4
5
6
8
9
10
11
12
13
1
2
3
4
5
6
8
9
10
11
12
13
Set sum to 0
Set count to 1
While count ≤ 10 do
Set sum to sum + count
Increment count
Output sum
17
– Must shift instructions and data in memory
– Must recalculate and edit addresses of operands
• Some errors can go unnoticed
– The operand of a jump can be the address of a
datum
– The operand of a data manipulation can be the
address of an instruction
– The HALT instruction might not exist
2/19/04
CS 100 - Lecture 11
18
(slide < S. Levy)
CS 100
3
Lecture 11
2/19/04 13:36
Assembly Language
Automation Principle
• Automation Principle: Automate
mechanical, tedious, or error-prone
activities.
• Examples:
• Provides built-in symbols for op codes
(LOAD, STORE, etc.)
• Address fields must also be programmernamed symbols (PI, ENDIF, etc.)
• Special syntax picks out data from
instructions
– Translation of decimal numbers and character
data to binary
– Translation of symbolic operation names to
binary op codes
2/19/04
CS 100 - Lecture 11
19
2/19/04
CS 100 - Lecture 11
20
(slide < S. Levy)
Opcode Symbols
Binary Opcode
Operand Symbols
Assembly Language Symbol
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
LOAD
STORE
CLEAR
ADD
INCREMENT
SUBTRACT
DECREMENT
COMPARE
JUMP
JUMPGT
JUMPEQ
JUMPLT
JUMPNEQ
IN
OUT
HALT
2/19/04
CS 100 - Lecture 11
• Can be any symbol except those in the op
code set
• Two types:
– Labels of data (serve as variables and constants)
– Labels of instructions (destinations of jumps)
21
2/19/04
CS 100 - Lecture 11
(slide < S. Levy)
Data Labels
Instruction Labels
Data labels name variables and are declared
with the .data directive
Instruction labels name instructions which are
the destinations of jumps
Format of data label:
Format of instruction label:
<any but jump opcode> <a label>
…
<a label>: .data <a decimal integer>
<jump opcode> <a label>
…
<a label>: <another instruction>
Example:
Example:
jumpgt okay
…
okay: out answer
load n
…
n: .data 50
2/19/04
CS 100 - Lecture 11
(slide < S. Levy)
CS 100
22
(slide < S. Levy)
23
2/19/04
CS 100 - Lecture 11
24
(slide < S. Levy)
4
Lecture 11
2/19/04 13:36
Example: The Simplest Assembly
Language Program
Example: Declaring Data Labels
Instructions
.begin
halt
.end
000000000000 1111 000000000000
Data
.begin
out eleven
halt
zero:
.data 0
one:
.data 1
eleven: .data 11
.end
000000000000
000000000001
000000000010
000000000011
000000000100
1110
1111
0000
0000
0000
000000000100
000000000000
000000000000
000000000001
000000001011
This program executes one instruction and halts.
This program reserves three cells of memory for data.
.begin and .end are not instructions. They are
directives that tell the computer where the program
begins and ends.
The computer initializes these cells with the given
numbers before any instructions are executed.
Format:
2/19/04
CS 100 - Lecture 11
25
<symbol>: .data <decimal integer>
2/19/04
CS 100 - Lecture 11
(slide < S. Levy)
Example: Output the Sum of Two
Input Numbers
.begin
in first
in second
load first
add second
store sum
out sum
halt
first: .data 0
second: .data 0
sum:
.data 0
.end
2/19/04
000000000000
000000000001
000000000010
000000000011
000000000100
000000000101
000000000110
000000000111
000000001000
000000001001
26
(slide < S. Levy)
1101
1101
0000
0011
0001
1110
1111
0000
0000
0000
High-level pseudocode
// Absolute value
Input X
If X < 0
Set X to –X
Output X
000000000111
000000001000
000000000111
000000001000
000000001001
000000001001
000000000000
000000000000
000000000000
000000000000
CS 100 - Lecture 11
Full Example: Output Absolute Value
Declaration
of label
27
2/19/04
(slide < S. Levy)
Assembly language
-- Absolute value
.begin
in x
load zero
compare x
jumpgt endif
subtract x
store x
endif: output x
halt
x: .data 0
zero: .data 0
.end
Comment
Use of label
CS 100 - Lecture 11
28
(slide < S. Levy)
Full Example: Output Sum of 1 .. 10
Set sum to 0
Set count to 1
While count ≤ 10 do
Set sum to sum + count
Increment count
Output sum
2/19/04
CS 100 - Lecture 11
(slide < S. Levy)
CS 100
Translation to Machine Code
.begin
clear sum
load one
store count
while: load count
compare eleven
jumpeq endwhile
add sum
store sum
increment count
jump while
endwhile: out sum
halt
one:
.data 1
eleven: .data 11
sum:
.data 0
count: .data 0
.end
• A program called an assembler translates an
assembly language program to an
equivalent program in machine code
• The assembler keeps track of all the binary
op codes and addresses
• The assembler also catches some errors
29
2/19/04
CS 100 - Lecture 11
30
(slide < S. Levy)
5
Lecture 11
2/19/04 13:36
Example Syntax Errors
Program Development
1. width has not been
declared as a data label
2. The halt instruction
occurs below the data
declaration section
Editor
Not OK (error messages)
Create assembly
language program
Assembler
OK
Run-time
system
Check for syntax
errors and translate
to machine code
Execute the machine
language program
2/19/04
CS 100 - Lecture 11
31
The assembler prevents this incorrect program from running
2/19/04
CS 100 - Lecture 11
(slide < S. Levy)
(slide < S. Levy)
Security Principle
Benefits of Assembly Language
• Security Principle: No program that violates
the definition of the language, or its own
intended structure, should escape detection.
– C. A. R. Hoare
– Detect undefined symbolic labels
– Detect multiple definition of symbolic labels
CS 100 - Lecture 11
32
• Maintenance is much easier – just edit and
assemble
• Code is much easier to understand
• Some errors are caught automatically before
execution
• Examples:
2/19/04
.begin
in width
in second
load first
add second
store sum
out sum
first: .data 0
second: .data 0
sum:
.data 0
halt
.end
33
2/19/04
CS 100 - Lecture 11
34
(slide < S. Levy)
Problems With
Assembly Language
Problems With
Assembly Language (2)
Still reflects underlying machine architecture
and operations
– Uses load, add, store instead of algebraic
notation for arithmetic expressions (x = y + z)
– No structured control statements like while
and if/else
– Data are typeless (no int, char, String)
2/19/04
CS 100 - Lecture 11
(slide < S. Levy)
CS 100
35
• Each machine has a different architecture
and instruction set, so it needs a different
dialect of assembly language – Tower of
Babel!
• Must rewrite programs for each new
machine that comes along
2/19/04
CS 100 - Lecture 11
36
(slide < S. Levy)
6
Lecture 11
2/19/04 13:36
A Better Programming Language
Portability Principle
• We would like one standard language with
translators for different machines
• This language should express the logic of problem
solving more than the logic of a machine’s
architecture
• This language should be more like pseudocode
Avoid feature or facilities that are
dependent on a particular computer or
a small class of computers
2/19/04
CS 100 - Lecture 11
37
2/19/04
CS 100 - Lecture 11
38
(slide < S. Levy)
High-Level Languages
Examples
• Code looks more like pseudocode, allows
logic of problem solving to be expressed in
syntax
• One standard for each language (“write
once, run anywhere”)
• Compilers translate code to machine
language of various target machines
• Error handling is much more extensive
2/19/04
CS 100 - Lecture 11
•
•
•
•
•
39
2/19/04
(slide < S. Levy)
– You have to another computer with this software
– You end up with an assembler that runs on the
other computer, not yours
CS 100
40
Bootstrapping
• An Assembler is a program. What is it written
in?
• You can write it in machine language for your
computer, but that’s very difficult
• You can write it in a language implemented by
an assembler or compiler on another computer,
but:
CS 100 - Lecture 11
CS 100 - Lecture 11
(slide < S. Levy)
Implementing an Assembler
2/19/04
BASIC (1960s)
Pascal (1970s)
C++ (1980s)
Java (1990s)
?
41
• This sort of problem can be solved by
“bootstrapping”
• Program the first version of the assembler in itself
• Hand translate this program to machine language
• Run and debug it, but correct errors in both the
machine language and assembly language versions
• Once it is debugged, it ought to be able to
assemble itself
• You can throw away the ML version, and from
now on edit the AL version
• New assembler version is always assembled by
the previous version
2/19/04
CS 100 - Lecture 11
42
7
Lecture 11
2/19/04 13:36
Translating Assembly Language
Program into Machine Language
Translating Assembly Language (2)
• Pass 2: Create machine language object file
Open new object file
Get first Assembly language instruction
While the instruction is not .END
• Four basic tasks performed by assembler:
– Convert symbolic op codes and addresses to binary
– Do pseudo-ops
– Write machine language program to file
If instruction is .DATA
Create binary value corresponding to value in address field
Store the value in object file with address where to load it
Else
Look up the op code of the instruction
Get binary value of op code
Look up address field in symbol table
Build machine language instruction and write to object file
• Assembler does this in two passes
• Pass 1: Build symbol table that maps labels to
addresses; this will enable us to translate those
labels to actual addresses in the second pass
2/19/04
CS 100 - Lecture 11
(slide < C. Hundhausen)
CS 100
Endif
Get next instruction
EndWhile
43
2/19/04
CS 100 - Lecture 11
44
(slide adapt. < C. Hundhausen)
8
Download