Module6.ppt.

advertisement
Program Translation
Module 6
Mid-Term Results
Mid-term
• Above 93/100 is A.
Mid-term
• 93/100 or above is A.
• 60/100 or below is fail.
Mid-term
• Above 93/100 is A.
• 60/100 or below is fail.
• For students who got 120/200 or less at midterm attendance in make-up is mandatory.
Mid-term
• Above 93/100 is A.
• 60/100 or below is fail.
• For students who got 120/200 or less at midterm attendance in make-up is mandatory.
• Note that April 10th 2008 is the course
withdrawal deadline.
Mid-term
• Above 93/100 is A.
• 60/100 or below is fail.
• For students who got 120/200 or less at mid-term
attendance in make-up is mandatory.
• Note that April 10th 2008 is the course withdrawal
deadline.
• I will send a private email to those students who
are below the fail threshold so far and let them
know how good they should perform from now
(future exams and HWs) on to avoid being failed.
Questions
• Mid-term?
• Assignment 3?
• Assignment 3 is due mid-night April 4th.
Program Translation
Module 6
Introduction
• Computers are not smart.
• They do only what we ask them to do.
Introduction
• Computers are not smart.
• They do only what we ask them to do.
• That is they can do whatever we know how to
order it to perform.
• But we use computers because they are fast
and precise.
The Binary Machine
• A modern computer can run programs written
in Java, C, C++, Lisp, JavaScript, Cobol and
many others.
• So the computers appear to be Multilingual.
The Binary Machine
• A modern computer can run programs written
in Java, C, C++, Lisp, JavaScript, Cobol and
many others.
• So the computers appear to be Multilingual.
• But every computer ever built “understands”
only one language:
The Binary Machine
• A modern computer can run programs written
in Java, C, C++, Lisp, JavaScript, Cobol and
many others.
• So the computers appear to be Multilingual.
• But every computer ever built “understands”
only one language: the machine language it is
wired to use.
The Binary Machine
• A modern computer can run programs written
in Java, C, C++, Lisp, JavaScript, Cobol and
many others.
• So the computers appear to be Multilingual.
• But every computer ever built “understands”
only one language: the machine language it is
wired to use.
• Thus, the machine language of an Intel PC is
different than an IBM personal computer or
Sun Workstation.
The Binary Machine
• If we want to use a language like JavaScript to
write a program, first we need to have a tool
to translate the program to machine language
which our computer understands.
Program Translation
• The notion of translation is central to our
discussion.
• A modern programming language is designed
with two audiences in mind:
Program Translation
• The notion of translation is central to our
discussion.
• A modern programming language is designed
with two audiences in mind:
• The programmers who use the language to write
programs for computers.
• The computer, which needs to execute (run) the
instructions in the language.
Program Translation
Machine Language
Here is a sample of a machine language for a
hypothetical computer:
0000010010000000
0000000010000001
0000111100000000
Binary Presentation
• The instructions of a machine language are
represented using just 0s and 1s.
Binary Presentation
• The instructions of a machine language are
represented using just 0s and 1s.
• In the next module we’ll see that all
computers represent all the information in
binary notation.
Binary Presentation
• The instructions of a machine language are
represented using just 0s and 1s.
• In the next module we’ll see that all
computers represent all the information in
binary notation because they can only
distinguish between two values (low voltage
level and high voltage level), which we label as
0 and 1.
Binary Presentation
• The instructions of a machine language are
represented using just 0s and 1s.
• In the next module we’ll see that all
computers represent all the information in
binary notation because they can only
distinguish between two values (low voltage
level and high voltage level), which we label as
0 and 1.
• Binary notation: a notation which uses only
two symbols, 0 and 1, to present information.
Binary Presentation
• If the computer can work only in terms of 0
and 1, how are we going to represent, store
and manipulate the information?
Binary Presentation
• If the computer can work only in terms of 0
and 1, how are we going to represent, store
and manipulate the information?
• We can use some sort of binary notation.
Binary Presentation
• For example we can have an easy form of
binary representation called binary-coded
decimal (BCD) notation, in which we use a
table for converting a number to this notation
and vice versa.
Binary Presentation
• For example we can have an easy form of
binary representation called binary-coded
decimal (BCD) notation, in which we use a
table for converting a number to this notation
and vice versa. Here is the table:
Digit
Binary Code
Digit
Binary Code
0
0000
5
0101
1
0001
6
0110
2
0010
7
0111
3
0011
8
1000
4
0100
9
1001
Examples of BCD
Convert number 5683 to BCD format.
Examples of BCD
Convert number 5683 to BCD format.
5
6
8
3
Examples of BCD
Convert number 5683 to BCD format.
5
6
8
3
By looking at BCD table and inserting the BCD
code for each digit we have:
0101 0110 1000 0011
Examples of BCD
Convert number 5683 to BCD format.
5
6
8
3
By looking at BCD table and inserting the BCD
code for each digit we have:
0101 0110 1000 0011
So the BCD equivalent is:
0101011010000011
Examples of BCD
Convert the BCD code 0100100000010000 to a
decimal (base 10) number.
Examples of BCD
Convert the BCD code 0100100000010000 to a
decimal (base 10) number.
1- Divide the sequence into chunks of length 4.
Why?
0100 1000 0001 0000
Examples of BCD
Convert the BCD code 0100100000010000 to a
decimal (base 10) number.
1- Divide the sequence into chunks of length 4.
Why?
0100 1000 0001 0000
2- Use the BCD table to find the
corresponding digit for each chunk.
4
8
1
0
Examples of BCD
Convert the BCD code 0100100000010000 to a
decimal (base 10) number.
1- Divide the sequence into chunks of length 4.
Why?
0100 1000 0001 0000
2- Use the BCD table to find the
corresponding digits for each chunk.
4
8
1
0
3- Put the numbers together in the same order:
4810
Do it yourself?
a. Convert the number 873 into BCD format.
b. Convert 0111000010010101 to a decimal
number.
Do it yourself?
a. Convert the number 873 into BCD format.
answer: 100001110011
b. Convert 0111000010010101 to a decimal
number.
answer: 7095
BCD notation Disadvantages
1. For each decimal number of length n we
have a BCD code of length 4n.
Example: 211 and 001000010001
2. Arithmetic on numbers in BCD format is
complicated.
Base-10 Notation
• In everyday life we represent numbers in base
10 Positional Notation.
– positional: the value of a digit within a number
depends on its position.
Example: 342
and
432
– base 10 : the value of any position is a multiple of
a power of 10.
• The digits we use in this notation are:
{0, 1, 2, 3, 4, 5, 6, 7,8 ,9 }
Base-10 Notation
Example: the number 7689 is
interpreted as:
Base-10 Notation
Example: the number 129 is interpreted
as:
Base 2 Notation
• There is nothing special about base 10
notation.
• We can use any base to represent numbers.
But since computers only understand 0 and 1
we use base 2 notation.
Base 2 Notation
• In base two notation we have only two digits:
{0, 1}
• Each digit in this notation is a multiple of a
power of 2 just as in base 10 where each digit
is a multiple of a power of 10.
• 11010101 is an example of a number in base-2
format.
Base-2 Example
The number 11010101 is
interpreted as :
Base 2 Example
• 213 in base 10 = 11010101 in base 2
Do it yourself
• What base-10 number the following base-2
number represents?
10010
Do it yourself
• What base-10 number the following base-2
number represents?
10010
Answer: 18
How to Convert Base-10 to Base-2
• We can convert a decimal number to base-2
using a simple algorithm.
• Lets see the algorithm through examples.
Example 1
• Convert decimal number 29 to binary.
Example 1
• Convert decimal number 29 to binary.
1. 29 / 2
quotient = 14 remainder = 1
Example 1
• Convert decimal number 29 to binary.
1. 29 / 2
2. 14 / 2
quotient = 14 remainder = 1
quotient = 7 remainder = 0
Example 1
• Convert decimal number 29 to binary.
1. 29 / 2
2. 14 / 2
3. 7 / 2
quotient = 14 remainder = 1
quotient = 7 remainder = 0
quotient = 3 remainder = 1
Example 1
• Convert decimal number 29 to binary.
1.
2.
3.
4.
29 /
14 /
7 /
3 /
2
2
2
2
quotient = 14
quotient = 7
quotient = 3
quotient = 1
remainder = 1
remainder = 0
remainder = 1
remainder = 1
Example 1
• Convert decimal number 29 to binary.
1.
2.
3.
4.
5.
29 /
14 /
7 /
3 /
1 /
2
2
2
2
2
quotient = 14
quotient = 7
quotient = 3
quotient = 1
quotient = 0
remainder = 1
remainder = 0
remainder = 1
remainder = 1
remainder = 1 (Stop)
Example 1
• Convert decimal number 29 to binary.
1.
2.
3.
4.
5.
6.
29 / 2 quotient = 14 remainder = 1
14 / 2 quotient = 7 remainder = 0
7 / 2 quotient = 3 remainder = 1
3 / 2 quotient = 1 remainder = 1
1 / 2 quotient = 0 remainder = 1 (Stop)
Sequence the reminders starting from last step
to first: 11101 .
This is the binary (base-2) representation of 29.
Another view of the algorithm
Converting decimal number 29 to binary 11101
Example 2
Convert decimal 213 to binary:
1.
2.
3.
4.
5.
6.
7.
8.
9.
213 / 2 quotient = 106 Remainder = 1
106 / 2 quotient = 53 Remainder = 0
53 / 2 quotient = 26 Remainder = 1
26 / 2 quotient = 13 Remainder = 0
13 / 2 quotient = 6 Remainder = 1
6 / 2 quotient = 3 Remainder = 0
3 / 2 quotient = 1 Remainder = 1
1 / 2 quotient = 0
Remainder = 1 Stop.
Sequence from last to first: 11010101
Do it yourself
• Find the binary presentation of decimal 89.
How to Present Text in Binary
Format
• Ok, we know how to represent numbers in
binary format, but how about a piece of text?
How to Present Text in Binary
Format
• Ok, we know how to represent numbers in
binary format, but how about a piece of text?
• Like BCD notation we can have a table which
maps each character to an integer code in
binary.
How to Present Text in Binary
Format
• Ok, we know how to represent numbers in
binary format, but how about a piece of text?
• Like BCD notation we can have a table which
maps each character to an integer code in
binary.
• This coding may vary from machine to
machine. But many computers use a well
known coding for representing characters
known as ASCII (American Standard Code for
Informatio Interchange).
Examples of ASCII Entries
Character
Integer code in decimal
Integer code in binary
space
32
00100000
$
36
00100100
2
50
00110010
3
51
00110011
?
63
00111111
“
34
00100010
A
65
01000001
B
66
01000010
a
97
01100001
b
98
01100010
z
122
01111010
Encoding Instructions
• Computers understand only binary.
Encoding Instructions
• Computers understand only binary.
• We need to feed the instructions to computer
in binary format. But how do we do it?
Encoding Instructions
• Computers understand only binary.
• We need to feed the instructions to computer
in binary format. But how do we do it?
• Again we need to have a table (mapping) for
having a code in binary for each instruction.
Encoding Instructions
• Computers understand only binary.
• We need to feed the instructions to computer
in binary format. But how do we do it?
• Again we need to have a table (mapping) for
having a code in binary for each instruction.
• For example we can define an 8-bit operation
code followed by 8 more bits which provides
details about the instruction.
Bit and Byte
• What is a bit?
Bit and Byte
• What is a bit?
• A bit is a unit of storage which can store only 0
or 1.
Bit and Byte
• What is a bit?
• A bit is a unit of storage which can store only 0
or 1.
A bit storage:
0
1
Bit and Byte
• What is a bit?
• A bit is a unit of storage which can store only 0
or 1.
A bit storage:
0
1
• Every 8-bit is called a byte.
Bit and Byte
• What is a bit?
• A bit is a unit of storage which can store only 0
or 1.
A bit storage:
0
1
• Every 8-bit is called a byte.
1
0
0
1
1
0
1
1
Bit and Byte
• What is a bit?
• A bit is a unit of storage which can store only 0
or 1.
A bit storage:
0
1
• Every 8-bit is called a byte.
1
0
0
1
1
0
1
1
• Every 1024 byte is a kilo-byte.
• Every 1024 kilo-byte is a mega-byte
• Every 1024 mega-byte is a giga-byte.
Encoding Instructions
• We can have a 8-bit (one byte) operation code
followed by 8 more bits (another byte) which
gives details about the instruction.
Encoding Instructions
• We can have a 8-bit (one byte) operation code
followed by 8 more bits (another byte) which
gives details about the instruction.
Example:
00000100
00110110
The architecture of our Computer
• Our hypothetical computer has an 8-bit (one
byte) Accumulator.
The architecture of our Computer
• Our hypothetical computer has an 8-bit (one
byte) Accumulator.
• Accumulator is a special location used for
storing intermediate results of computation.
The architecture of our Computer
• Our hypothetical computer has an 8-bit (one
byte) Accumulator.
• Accumulator is a special location used for
storing intermediate results of computation.
• For Example you want to calculate:
97 + (210 – 113) you might write the result of
subtraction on a piece of paper before adding
the result with 97.
The architecture of our Computer
• Also our architecture has 256 bytes of
memory labeled (addressed) with 0 to 255.
The architecture of our Computer
• Also our architecture has 256 bytes of
memory labeled (addressed) with 0 to 255.
• Memory (In computer terminology): Place for
storing data in binary format. Smallest unit of
memory is a bit.
The architecture of our Computer
• Also our architecture has 256 bytes of
memory labeled (addressed) with 0 to 255.
• Memory (In computer terminology): Place for
storing data in binary format. Smallest unit of
memory is a bit.
• The size of the memory of today’s computers
is something of magnitude of giga-bytes.
The Architecture of Our
Hypothetical Computer
Example of Instructions of our
Computer
Instruction
Information
Code
Explanation
LOD
X
00000100 Loads accumulator with contents of memory
location X
STO
X
00000101 Stores accumulator value into memory
location X
HLT
X
00001111 Halts (Stops) Execution
ADD
X
00000000 Adds contents of memory location X to
accumulator
Example
Now the piece of machine language we saw
previously should have a meaning to you:
0000010010000000
0000000010000001
0000111100000000
Example
[1] 0000010010000000
[2] 0000000010000001
[3] 0000111100000000
We separate the sequences into bytes (chunks
of length 8):
Example
[1] 0000010010000000
[2] 0000000010000001
[3] 0000111100000000
We separate the sequences into bytes (chunks
of length 8):
[1] 00000100 10000000
[2] 00000000 10000001
[3] 00001111 00000000
Example
[1] 00000100 10000000
[2] 00000000 10000001
[3] 00001111 00000000
First byte is the code for instruction. So we must
look for the corresponding instruction in the
table:
Example
[1] 00000100 10000000
[2] 00000000 10000001
[3] 00001111 00000000
First byte is the code for instruction. So we must
look for the corresponding instruction in the
table:
LOD 10000000
ADD 10000001
HLT 00000000
Example
The second byte represents details about
Instruction which is an integer in binary format:
LOD 10000000
ADD 10000001
HLT 00000000
10000000 represents 128 in decimal, 10000001
represents 129 in decimal and 00000000 is 0.
Example
Putting all together we have :
LOD 128
ADD 129
HLT 0
This is a piece of language called assembly
Language.
Assembler
• A program that takes as its input a program
written in assembly and outputs the same
program written in Machine Language.
Assembler
• A program that takes as its input a program
written in assembly and outputs the same
program written in Machine Language.
LOD
128
Sourcex
ADD 129
HLT 0
Assembler
0000010010000000
0000000010000001
0000111100000000
Going Further
Now assume a programmer writes a program in a high level
language such as JavaScript. The programmer needs to programs
to make her/his program understandable to computer: Compiler
and Assembler.
Going Further
Now assume a programmer writes a program in a high level
language such as JavaScript. The programmer needs to programs
to make her/his program understandable to computer: Compiler
and Assembler.
….
If(x == 4)
Document.
write(<b>);
….
Compiler
LOD 128
ADD 129
HLT 0
Assembler
0000010010000000
0000000010000001
0000111100000000
Assembly
• There are many programmers who write their
programs directly in assembly as opposed to
any high level language such as Java, because
Assembly
• There are many programmers who write their
programs directly in assembly as opposed to
any high level language such as Java, because
– It gives the programmer a precise control of the
computer’s functionalities.
– In most cases the programs written directly in
assembly language are faster than programs
written in high level languages.
Back to our piece of Assembly code
LOD 128
ADD 129
HLT 0
Back to our piece of Assembly code
LOD 128
ADD 129
HLT 0
LOD 128 : Loads the contents of memory
location (a byte) 128 into accumulator.
Back to our piece of Assembly code
LOD 128
ADD 129
HLT 0
LOD 128 : Loads the contents of memory
location (a byte) 128 into accumulator.
ADD 129: Adds the number 129 to the number
already in accumulator and puts the result in
accumulator.
Back to our piece of Assembly code
LOD 128
ADD 129
HLT 0
LOD 128 : Loads the contents of memory
location (a byte) 128 into accumulator.
ADD 129: Adds the number 129 to the number
already in accumulator and puts the result in
accumulator.
HLT : Stops the process.
Another Example
LOD 213
ADD 214
ADD 215
ADD 216
ADD 217
STO 213
HLT
What does this program do?
The assembly Instructions for our
Machine
Conventions:
1. All variables refer to 8-bit binary number.
The assembly Instructions for our
Machine
Conventions:
1. All variables refer to 8-bit binary number.
ex: LOD X X is an 8-bit number ( [0-255])
The assembly Instructions for our
Machine
Conventions:
1. All variables refer to 8-bit binary number.
ex: LOD X X is an 8-bit number ( [0-255])
2. Variables can represent a number or an address.
A number is indicated by a leading pound sign
(#).
The assembly Instructions for our
Machine
Conventions:
1. All variables refer to 8-bit binary number.
ex: LOD X X is an 8-bit number ( [0-255])
2. Variables can represent a number or an address.
A number is indicated by a leading pound sign
(#).
ex: LOD 123 and LOD #123
LOD 123  Load the contents of memory
location 123 into the accumulator
LOD #123  Load number 123 into the
accumulator.
The assembly Instructions for our
Machine
Another ex:
ADD 212 & ADD #212
ADD 212 : Add the contents of memory location
212 to accumulator.
ADD #212 : Add number 212 to accumulator.
3. We use letter A to refer to the accumulator.
Data Flow Instructions
Instruction Variable Explanation
LOD
X
Load the contents of memory location specified with address X
into accumulator.
,,
#X
Load the number X into accumulator.
STO
X
Store the contents of memory location specified with address X
Into accumulator.
,,
#X
Store the number X into accumulator.
Arithmetic Instructions
Instruction
Variable
Explanation
ADD
X
Add the contents of memory location specified by address X to
contents of accumulator.
,,
#X
Add the number X to contents of accumulator.
SUB
X
Subtract the contents of memory location specified by address X
from contents of accumulator.
,,
#X
Subtract number X from contents of accumulator.
MUL
X
Multiply the contents of accumulator by the contents of memory
location specified by address X.
,,
#X
Multiply the contents of accumulator by number X.
Control Instructions
• A program is a sequence of instructions.
Control Instructions
• A program is a sequence of instructions.
• In normal condition during the execution of
the program (assembly program in our case) ,
the computer starts with execution of first
instruction, followed by second, third and all
the way to the last instruction.
Control Instructions
• A program is a sequence of instructions.
• In normal condition during the execution of
the program (assembly program in our case) ,
the computer starts with execution of first
instruction, followed by second, third and all
the way to the last instruction.
• But sometimes we want to change this
normal flow of instruction executions.
Control Instructions
• A program is a sequence of instructions.
• In normal condition during the execution of
the program (assembly program in our case) ,
the computer starts with execution of first
instruction, followed by second, third and all
the way to the last instruction.
• But sometimes we want to change this
normal flow of instruction executions.
• We use control instructions to direct the flow
of instruction execution in a way that we
have in mind.
Control Instructions
An example of situation where we want to change
the flow of execution:
We have sequence of three instructions:
instruction 1
instruction 2
instruction 3
and we need to run this sequence 20 times.
Control Instructions
One way to write such program:
instruction 1
–
instruction 2
instruction 3
instruction 1
instruction 2
instruction 3
.
.
.
instruction 1
instruction 2
instruction 3
That is to repeat the sequence of three instructions 20 times in our program.
Control Instructions
Smarter algorithm:
Step 1: Set memory location X to 20
Step 2: if the contents of memory location X is not
zero execute the sequence:
instruction
instruction 2
instruction 3
Subtract the contents of memory location X by 1
Jump to step 2
else HLT
Control Instructions
Instruction Variable Explanation
JMP
Y
Jump to instruction number Y (Go to Y-th instruction in the
program )
JMZ
Y
Jump to instruction number Y if the contents of accumulator is 0.
Otherwise , go to next instruction.
HLT
Halt execution (Stop the execution).
Don’t do anything more.
Example 1
LOD 213
ADD 214
ADD 215
ADD 216
ADD 217
STO 213
HLT
Example 2
LOD 212 ;Load the memory location 212 into accumulator
ADD #1 ;Add one to accumulator
STO 213 ;Store the contents of accumulator into memory
ADD #1 ;location 213
STO 214
ADD #1
STO 215
HLT
Comment
• Comments are little explanatory notes for
programmers inside the program.
Comment
• Comments are little explanatory notes for
programmers inside the program.
• In our assembly language, a comment is
anything from the semicolon (;) to the end of
the line.
Comment
• Comments are little explanatory notes for
programmers inside the program.
• In our assembly language, a comment is
anything from the semicolon (;) to the end of
the line.
• These notes are ignored by assembler. That is
they have no effect on what the programs do.
Do it yourself.
What number in binary format will be in memory location 197 at
the end of the program?
LOD #2 ;Load the number 2 into accumulator .
MUL #2
MUL #2
MUL #2
MUL #2 ;Multiply by 2 the contents of accumulator.
ADD #2
ADD #2
SUB #2 ;Subtract 2 from the contents of the accumulator.
STO 197
HLT
Using Control Instructions
• Here is a program which uses some of the control instructions.
[1] LD #0
[2] STO 123
[3] SUB 210
[4] JMZ 9 ; Jump to instruction 9 if the contents of acc. is 0
[5] LD 123
[6] ADD #1
[7] STO 123
[8] JMP 3 ;Jump to instruction 3
[9] HLT
• Can you write a simpler program which does the same job?
• What will the program do if we change number 210 to 55 in line
3?
Download