Download Our first step in moving inside computer Understand how to

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Representing Information
Our first step in moving inside computer
Understand how to represent information
Way that computer can understand
Must be able to express
Numbers
Symbols or characters
Letters
Control
Computers represent information as collections of 0’s and 1’s
Expressed by state of electronic signals
Called bits
Bit only stores one thing
Stored in
Memory
RAM
Registers
Like small memories
In computer or any digital system
Working with bits
Only have limited number of bits
Can define specific groupings of bits
Collections grouped in binary increments
2, 4, 8, 16...
Bytes - 8
Words - 16, 32, 64...
Numbers
First thing important to represent is numbers
2 kinds
Integers
Floating point
Examine each in turn
Integer Numbers
Unsigned Integers
Most computer systems use weighted number system
Means we have collection of symbols
Let’s call this set S
Because it’s familiar
Let’s see how decimal works
Decimal
Our set of symbols
S = {0..9}
To express number
Assign weight to symbol based upon its position in number
Sum of weights * symbol value = number
Example
789
7
Assign weight 102
Value = 7 * 102
8
Assign weight 101
Value = 8 * 101
9
Assign weight 100
Value = 9 * 10
Total - 700 + 80 + 9 = 789
Thus in general
n −1
value =
∑ s × 10
i =0
i
i
Binary
S = {0..1}
To express number
Assign weight to symbol based upon its position in number
Sum of weights * symbol value = number
Example
101
1
Assign weight 22
Value = 1 * 22
0
Assign weight 21
Value = 0 * 21
1
Assign weight 20
Value = 1 * 2
Total - 4 + 0 + 1= 5
Thus in general
n −1
value =
∑s × 2
i
i =0
i
How Big
We can now determine how large number can be represented
Decimal
With 3 decimal digits
Largest number
999
Can also express as
103 - 1
Observe 3 is number of digits
Or in general
10n - 1
Binary
With 3 binary digits
Largest number
111
Can also express as
23 - 1
Observe 3 is number of digits
Or in general
2n - 1
In Machine
If machine uses 8 bit words
Largest integer
28 - 1 = 255
If machine uses 16 bit words
Largest integer
216 - 1 = 65K
Signed Integers
Let’s work with a machine that uses 32bit words
Signed number will be represented as
Sign bit on LHS
0 - positive
1 - negative
31
30
Sign
0
Number
In C, C++, or Java declaration
int a = 10;
Places integer value 10 in binary
Into word in memory
We can view memory as a bookshelf or two dimensional array
Each shelf holds a word
Each book represents a bit
Book is right side up
Represents a 1
Book is upside down
Represents a 0
By switching to signed numbers
We now have a range
+2n-1 to -2n-1
We loose one number
Floating Point Numbers
Clearly we need to be able to express numbers larger than 65K
Done using floating point numbers
Thus
In C, C++, or Java declaration
float a = 10.9;
Places floating point value 10.9 in binary
Into word in memory
In machine still use 32 bit word
Interpret bits differently
31
30
Sign
23 22
Exponent
0
Number
First remember scientific notation for decimals from physics
If we have number 11.287593
We write as
0.11287593 x 10+2
Our rule is to move the decimal point
To the left of the left most digit
Multiply by the appropriate amount
Same rule applies in binary
Our rule is to move the binary point
To the left of the left most bit
Multiply by the appropriate amount
Now can expressive power given as
± (223 -1) x 2exp(28 -1)
Someone recognized
Since always storing 1 as msb
Why store
Move 1 bit to right and store those
We now are storing 1 additional bit
Example
Let’s consider that we can only store 8 bits
For the binary number 1011.1101101
1. First convert to
0.10111101101 x 2+4
2. Since we can only store 8 bits our number is
0.10111101 x 2+4
We loose the last 3 bits - 101
2a. Since we know that the msb will be a 1 instead we can store
0.01111011 x 2+4
We now have stored one extra bit of precision
By using hidden bit
Can increase to
± (224 -1) x 2exp(28 -1)
Characters
Once again use same 16 bits
Modify interpretation once again
Each character represented by 8 bits - 1 byte
Express character in any of several standard codes
ASCII
EBDCIC
BDCIC
Rarely any more
8 bits
Permit one to express
255 combinations
Approximately half
31
23 22
16 15
87
0
Used to express
Character 3
Character 2 Character 1
Character 0
Printing
Non printing or control
32 bit word permits storage of 4 characters
In C declaration
char a = ‘a’;
Places ASCII value of character a
Into word in memory
Will be stored as
00 00 00 0a16
For a total of 32 bits
Addresses
Let’s now consider what else can be stored in a word
That is what other interpretation can we place on same bits
In computer
Information stored in memory
Each piece placed at an address
Information accessed in memory by giving its address
Addresses begin at 0 and end at number based upon word size
Recall with 16 bits
Largest number 65K
Range between 0 - 65K
31
If those values represent addresses
Can address upto 65K locations
For a 32 bit word we have
We can address approximately 4 M
0
4M
0
Address
In C or C++declarations
Java doesn’t think they have address – they really do
int a = 10;
int* aPtr = &a;// take the address of a and assign to the pointer variable aPtr
Place integer value 10 in binary
Into word in memory
Lets say at address 3000
Sets aside another memory word to hold address of a
Puts 3000 into that address
The value at address points to 3000
Instructions
Another way to interpret bits in memory word
Instructions
When viewed as instructions
Several different formats
Depending upon kind of instruction
Let’s consider several
Before doing so must consider kinds of things instruction must contain
Operation
Identifies what instruction is to do
Implemented in the hardware
For speed
Computer may have
Few or many
16-128 for example
CISC
Often many
RISC
Often few
Let’s choose up to 64
We select a binary number
Location of Operands
These are addresses of operands used in instruction
Let’s look at several kinds of instructions
Code
Fragment
int a = 10;
int b = 20;
int c;
c = a + b;
Operand 1
Operand 2
Result
Type
a
b
c
3 address
int a = 10;
int b = 20;
b = a + b;
a
b
b
2 address
int a = 10;
a = a + 20;
a
20,
immediate
operand
a
1 address
We now ask
Where are these operands stored
Several answers
Long term
Memory somewhere
Short term
Registers
Registers
Special kind of memory
Storage element large one word
16 bits
32 bits
Faster than hard drive or RAM memory
Depending upon architecture
Computer may have a few or lots
CISC
16 - 256 or so
RISC
Order or 1000
Let’s assume we have 32
We select a binary number
We now can take a first look at how instruction appears upto this point
Note
Instruction size less than 32 bits
Will use remaining bits for other things
6 Bits
Operation
5 Bits
Operand1
5 Bits
5 Bits
Operand2
Operand3
Three Address Instruction
6 Bits
Operation
5 Bits
Operand1
5 Bits
5 Bits
Operand2
Two Address Instruction
6 Bits
Operation
5 Bits
Operand1
10 Bits
Immediate
One Address Instruction
na
Field
Operation or OP Code field
Upto 64 possible op codes
ADD
SUB
LOAD
STORE
etc.
Operands 1-3
Upto 32 different registers
Immediate
Max value
211 - 1
Instructions in Action
Let’s look at several examples of how instructions might appear
We’ll use 80196kd instructions
Arithmetic
Using C fragment from above
c = a + b;
Assume the following register assignments
a - R1
b - R2
c - R3
Assembler
add R3, R1, R2
add contents of R1 to R2 and put into R3
Machine Language
44 3 1 2
44 - op code for add
3 - identifies R3
1 - identifies R1
2 - identifies R2
Data Movement
Using a C fragment
a* = 10;
Assume the following register assignments
10 in R1
&a - R2
Assembler
st R1, *R2
store contents of R1 to location pointed to by R2
Machine Language
C0 2 1
C0 - op code for store word - st
2 - identifies R2
1 - identifies R1