Lecture12 - FSU Computer Science Department

Download Report

Transcript Lecture12 - FSU Computer Science Department

COP3502: Introduction to Computer Science
Program Translation
Yashas Shankar
Second part of this course

We will spend 4 classes on Program Translation (chapter
6 from your AE textbook) and 3 classes on Hardware
(chapter 7 from your AE textbook)



You need to have the textbook
You won’t have to do programming on program.cs.fsu.edu (but
you still have to write some assembly programs on papers)
We will spend 4 classes on Ethics


Textbook is optional, but you may need it to do extra credits
We will also spend some times reviewing programming during
this period
Extra credit

There will be 4 extra credits



2 for homework
2 for quizzes
For each extra credit, write a 5-page summary of one
chapter from your “ethics” textbook
Alternative extra credit for homework




Do the homework that you want to have your score
increase
Put your homework on your webpage
Write a 2-page summary of one chapter in Ethics
textbook and put it on your webpage
15 points will come from your homework and 10 points
from the summary. Grading policy on homework part will
be tougher.Your homework has to be 100% correct or
you will get at least 5 points off.
Alternative extra credit for quiz



Design a 10-question quiz related to the subject in the
quiz that you want to increase your score. Put a
“correct” answer for each question.
Put your quiz & solution on your webpage
Your base score will be 8 points. If your quiz is good, you
will get 9 or 10. If it is bad you will get 0-7 points
Extra credit submission


You have to state what each extra credit is for, e.g. for
quiz#4, asg#12, etc.
Normal 5-page summary extra credits


Alternative extra credits



Print out and hand them to me
Put on your webpage (and notify me)
You can start submitting your extra credit when we start
“ethics for computer science” part
You have to submit your extra credit before the final
exam
Program translation

Today lecture

Binary representation & machine language
Binary representation & machine
language

Human language


“hello my name is pitch”
Machine language


“0001 1000 1111 1100 ………………0101”
Machine language has only 0 and 1
How machine language work?
Human: add one and two
Machine:
Code for ADD
1
2
129
1
2
1000 0001
0000 0001
0000 0010
Binary representation


Machine only understand 0 and 1
We need to convert numbers that we understand to
what machines understand (and vise versa)
0
0000
8
1000
1
0001
9
1001
2
0010
10
1010
3
0011
11
1011
4
0100
12
1100
5
0101
13
1101
6
0110
14
1110
7
0111
15
1111
How to adds two numbers in binary
representation?
0
0000
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
0001
0001 +
0010
0010
0010 +
0100
0000
0010 +
0010
0011
0010 +
0101
0011
0011 +
0110
0101
0011 +
1000
What can you notice here?
8
1000
9
1001
10
1010
11
1011
12
1100
13
1101
14
1110
15
1111
16
10000
Binary representation – power of two
bits
0
0000
0
0000 0000
1
0001
1
0000 0001
2
0010
2
0000 0010
4
0100
4
0000 0100
8
1000
8
0000 1000
16
10000
16
0001 0000
32
100000
32
0010 0000
64
1000000
64
0100 0000
128
10000000
128
1000 0000
Binary representation – power of two
bits
0
0000 0000
1
0000 0001
2
0000 0010
4
0000 0100
8
0000 1000
16
0001 0000
32
0010 0000
64
0100 0000
128
1000 0000
0010
0010 +
0100
0100
0100 +
1000
1100 0000 = 1000 0000 + 0100 000
= 128 + 64 = 192
01000
01000 +
10000
How to convert binary number to
decimal number?
10110011
= 128 + 32 + 16 + 2 + 1 =179
1
0
1
1
0
0
1
1
128
64
32
16
8
4
2
1
32
16
2
1
128
= 179
How to convert decimal number to
binary number?
198 = ?
198 = 128 + 70
= 128 + 64 + 6
= 128 + 64 + 4 + 2
= 11000010
128
64
32
16
8
4
2
1
1
1
0
0
0
0
1
0
Hexadecimal representation
Decimal
Binary
Hex
Decimal
Binary
Hex
0
0000
0
8
1000
8
1
0001
1
9
1001
9
2
0010
2
10
1010
A
3
0011
3
11
1011
B
4
0100
4
12
1100
C
5
0101
5
13
1101
D
6
0110
6
14
1110
E
7
0111
7
15
1111
F
How to convert binary to hexadecimal
(and vise versa)?
0001 0010 1111 1100 = 12FC
101111 = 0010 1111 = 2F
10 0011 1100 = 0010 0011 1100 = 23C
1001 1101 11 = 0010 0111 0111 = 277
2F = 0010 1111
6CA3 = 0110 1100 1010 0011
Hexadecimal representation




Computer doesn’t understand it
Save people a lot of writing
Easier for people to understand
0, 1, 2, …………, 9, A, B, C, D, E, F
Coverting Hex to Decimal and vise versta


Hex to Dec: HexBinaryDecimal
Dec to Hex: DecBinary Hex
Negative numbers in binary representation
8-bit positive numbers = {0,1,…… 255} = {0,1,…., (28 – 1)}
32-bit positive numbers = {0,1,…….. ,(232 – 1)}
8-bit numbers = {-128, - 127, ….. 0, 1, ….127}
32-bit numbers = {-231 , ……. 0, 1, …… (231 -1)}
Negative numbers in binary representation
8-bit representation: always have 8 bits
Positive number: start with 0 (left-most bit)
Negative number: start with 1 (left-most bit)
Example:
127
0111 1111
2
0000 0010
1
0000 0001
0
0000 0000
-1
1111 1111
-2
1111 1110
-128
1000 0000
How to represent negative number?




Use 2-complement system
Start from positive number
Reverse all ones and zeros (the output is said to be in onecomplement form)
Add one to the output to get 2-complement representation
Example: what is -5?
5 = 0000 0101
Reverse = 1111 1010
Add 1 = 1111 1011 = 2-complement form of -5
-5
= 1111 1011
How to subtract number?


Subtract = add negative number
Example: 19 – 5



Start from 5 and change to -5 by 2-complement method
Add 19 and -5 together
More detailed example: solve19 – 5 in binary
representation





Add 19 and (-5) together
Step 1: change 19 to binary representation
Step 2: change -5 to 2-complement form
Step 3: add them together
Step 4: change the number back to decimal
Computation within CPU

This course



Accumulator based (in your textbook)
Stack based (I added it)
Example: C = A + B;
Accumulator
Load A
Add B
Store C
Halt
Stack
Push A
Push B
Add
Pop C
Halt
More on Accumulator
C=A+B
Load A
Add B
Store C
Halt

C=A+B
Load 128
Add 129
Store 130
Halt

C=A+B
0000 0001
0000 0010
0000 0011
0000 0100

1000 0000
1000 0001
1000 0010
More on Stack
C=A+B
Push A
Push B
Add
Pop C
Halt

C=A+B
Push 128
Push 129
Add
Pop 130
Halt

C=A+B
0000 0001
0000 0001
0000 0010
0000 0011
0000 0100

1000 0000
1000 0001
1000 0010
More complex statement (Accumulator)
Example1:
E = A+(B*C) + D
Accumulator
Load B
Mul C
Add A
Add D
Store E
Example2:
E = A+(B*C) + (D*F)
Accumulator
Load B
Mul C
Add A
Store TMP
Load D
Mul F
Add TMP
Store E
More complex statement (stack)
Example1:
E = A+(B*C) + D
E = ABC*+D+
Push
Push
Push
Mul
Add
Push
Add
Pop
A
B
C
D
E
Example2:
E = A+(B*C) + (D*F)
E = ABC*+DF*+ Push A
Stack representation
Push
Push
Mul
Add
Push
Push
Mul
Add
Pop
B
C
D
F
E
Stack representation

What do u need to know?



How to solve equations represented in stack representation
How to translate stack representation to normal
representation and vise versa
Example:
- what is the result of 234+*562-*+?
Note: answer = 34
- what is 234+*562-*+ in normal representation?
Note: answer = (2 * (3+4)) + (5 * (6-2))
- what is 2*3+4*5 in stack representation
Note: answer = 23*45*+