eel3801-1-basics

Download Report

Transcript eel3801-1-basics

EEL 3801
Part I
Computing Basics
EEL 3801C
Data Representation
Digital computers are binary in nature.
They operate only on 0’s and 1’s.
• Everything must be expressed in terms of 0’s
& 1’s - Instructions, data, memory locs.
• 1 = “on”  voltage at output of electronic
device is high (saturated).
• 0 = “off”  voltage at output of electronic
device is zero.
EEL 3801C
Data Representation
• The smallest element of information is a “bit”
– 0 or 1. But by itself a bit does not convey
much information. Therefore:
• 8 bits in succession make a “byte”, the
smallest addressable binary piece of
information
• 2 bytes (16 bits in succession) make a
“word”
EEL 3801C
Data Representation
• 2 words (32 bits in succession) make a
“double word”.
• We can easily understand base 10 numbers.
So we need to learn how to convert between
binary and decimal numbers.
• Decimal numbers are not useful to the
computer: a compromise – base 16 numbers
(hexadecimal) and base 8 nos., or “octal”.
EEL 3801C
Binary Numbers
• Each position in a binary number, starting
from the right and going left, stands for the
power of the number 2 (the base).
• The rightmost position represents 20, which = 1.
• The second position from the right represents 21= 2.
• The third position from the right represents 22 = 4.
• The fourth position from the right represents 23 = 8.
EEL 3801C
Binary Numbers
• The value of an individual binary bit is
multiplied by the corresponding base and
power of 2, and all the resulting values for all
the digits are added together. For ex.
00101001
= 0*27 + 0*26 + 1*25 + 0*24 + 1*23 + 0*22 +
0*21 + 1*20
= 0 + 0 + 32 + 0 + 8 + 0 + 0 +1 = 41
EEL 3801C
Binary Numbers
• Conversion from decimal to binary - Two
methods:
– Division by power of 2:
• Find the value of the largest power of 2 that fits into
decimal number.
• Set 1 for position bit corresponding to that power.
• Subtract that value from the decimal number.
• Go to the first step, and re-do procedure until
remainder is 0
EEL 3801C
Binary Numbers
• Example:
• decimal number = 146
• Largest value of power of 2 that fits into 146 is 128,
or 27
• Set 8th bit (power of 7) to 1
• Subtract 128 from 146 = 18
• Largest value that fits into 18 is 16 or 24
• Set 5th bit (power of 4) to 1
• 18 - 16 = 2
EEL 3801C
Binary Numbers
• Example (continued):
• Largest power of 2 that fits into 2 is 2, or 21
• Set second bit (power of 1) to 1
• Remainder is now 0
• Set all other bits to zero
– The binary equivalent = 1 0 0 1 0 0 1 0
EEL 3801C
Binary Numbers
• Second Method
– Division by 2
• Integer divide the decimal number by 2.
• Note the remainder (if even, 0; if odd, 1)
• The remainder represents the bit
• Integer divide the quotient again by 2 and note
remainder.
• Continue until quotient = 0
EEL 3801C
Binary Numbers
• Example
• Decimal number = 146.
• 146/2 = 73, remainder = 0
• 73/2 = 36, remainder = 1
• 36/2 = 18, remainder =0
• 18/2 = 9, remainder = 0
• 9/2 = 4, remainder = 1
• 4/2 = 2, remainder = 0
• 2/2 = 1, remainder = 0
EEL 3801C
Binary Numbers
• 1/2 = 0, remainder = 1
• 0/2 = 0, remainder = 0
• Starting from the last one, the binary number is now
the string of remainders:
010010010
Since the 0 to the left does not count, we can lop
it off
10010010
EEL 3801C
Hexadecimal Numbers
• Large binary numbers are cumbersome
to read.
• ==> hexadecimal numbers are used to
represent computer memory and
instructions.
• Hexadecimal numbers range from 0 to
15 (total of sixteen).
• Octal numbers range from 0 to 7 (total
of 8)
EEL 3801C
Hexadecimal Numbers
• The letters of the alphabet are used to
the numbers represent 10 through 15.
– where A=10, B=11, C=12, D=13, E=14,
and F=15
• But why use hexadecimal numbers?
• 4 binary digits (half a byte) can have a
maximum value of 15 (1111), and a
minimum value of 0 (0000).
EEL 3801C
Hexadecimal Numbers
• If we can break up a byte into halves, the
upper and lower halves, each half would
have 4 bits.
• A single hexadecimal digit between 0 and F
could more concisely represent the binary
number represented by those half-bytes.
• A byte could then be represented by two
hexadecimal digits, rather than 8 bits
EEL 3801C
Hexadecimal Numbers
• The advantage becomes more evident for
larger binary numbers:
• 00010110 00000111 10010100 11101010
•
A
1
6
0
7
•  160794DAh
EEL 3801C
9
4
D
Numbers
• A radix is placed after the number to indicate
the base of the number.
• These are always in lower case.
• If binary, the radix is a “b”;
• if hexadecimal, “h”,
• if octal, “o” or “q”.
• Decimal is the default, so if it has no indication, it is
assumed to be decimal. A “d” can also be used.
EEL 3801C
Hexadecimal to Decimal
Conversion
• Similarly to binary-to-decimal conversion,
each digit (position from right to left) of the
hex number represents a power of the base
(16), starting with power of 0.
– 2 F 5 B  2*163 + 15*162 + 5*161 + 11*160 =
2*4096 + 15*256 + 5*16 + 11*1
–
= 8192 + 3840 + 80 + 11 = 12,123
EEL 3801C
Binary Conversion into
Hexadecimal
• Binary to hex is somewhat different, because
we in reality, take each 4 bits starting from
the right, and convert it to a decimal number.
• We then take the hexadecimal equivalent of
the decimal number (i.e., 10 = A, 11 = B,
etc.) and assign it to each 4 bit sequence.
• Each digit in a hex number = “hexadized”
decimal equivalent of 4 binary bits.
EEL 3801C
Hexadecimal Conversion into
Binary
• This conversion is also rather simple.
• Each hex digit represents 4 bits. The
corresponding 4-bit binary sequence replaces
the hex digit. For example:
•
26AF  0010 0110 1010 1111
EEL 3801C
Signed and Unsigned Integers
• Integers are typically represented by one
byte (8 bits) or by one word (16 bits).
• There exist two types of binary integers:
signed and unsigned
EEL 3801C
Unsigned Integers
• Unsigned integers are easy – they use all 8
or 16 bits in the byte or word to represent
the number.
• If a byte, the total range is 0 to 255
(00000000 to 11111111).
• If a word, the total range is 0 to 65,535
(0000000000000000 to 1111111111111111).
EEL 3801C
Signed Integers
• Are slightly more complicated, as they can
only use 7 or 15 of the bits to represent the
number. The highest bit is used to indicate
the sign.
• A high bit of 0  positive number
• A high bit of 1  negative number counter intuitive, but more efficient.
EEL 3801C
Signed numbers (cont.)
• The range of a signed integer in a byte is
therefore, -128 to127, remembering that the
high bit does not count.
• The range of a signed integer in a word is –
32,768 to 32,767.
EEL 3801C
The One’s Complement
• The one’s complement of a binary number is
when all digits are reversed in value.
• For example,
00011011
has a one’s complement of
11100100
EEL 3801C
Storage of Numbers
• Unsigned numbers and positive signed
numbers are stored as described above.
• Negatively signed numbers, however, are
stored in a format called the “Two’s
complement” which allows it to be added to
another number as if it was positive.
EEL 3801C
Storage of Numbers (cont.)
• The Two’s Complement of a number is
obtained by adding 1 to the lowest bit of the
one’s complement of the number.
• The Two’s Complement is perfectly reversible
– TC (TC (number)) = number.
EEL 3801C
Storage of Numbers (cont.)
• Therefore, if the high bit is set (to 1), the
number is a negatively signed integer.
• But, its decimal value can only be obtained
by taking the two’s complement, and then
converting to decimal.
• If the high bit is not set (= 0), then the
number can be directly converted into
decimal.
EEL 3801C
Example
• 0 0 0 0 1 0 1 0 is a positive number, as the
high bit is 0.
• 0 0 0 0 1 0 1 0 can be easily converted to 10
decimal in a straightforward fashion.
• 0 0 0 0 1 0 1 0 = 10 decimal
EEL 3801C
Example (cont.)
• 1 0 0 0 1 0 1 0 is a negative number
because of the high bit being set.
• 1 0 0 0 1 0 1 0, however, is not –10, as we
first have to determine its two’s complement.
EEL 3801C
Example (cont.)
• One’s Complement of (1 0 0 0 1 0 1 0)  (0
1 1 1 0 1 0 1),
• add 1  (0 1 1 1 0 1 1 0)
•  64 + 32 + 16 + 4 + 2 = -118
EEL 3801C
Character Representation – ASCII
• How do we represent non-numeric
characters as well as the symbols for the
decimal digits themselves if we want to get
an alphanumeric combination?
• Typically, characters are represented using
only one byte minus the high bit (7-bit
code).
EEL 3801C
Character Representation – ASCII
(cont.)
• Bits 00h to 7Fh represent the possible
values. The ASCII table maps the binary
number designated to be a character with a
specific character. The back inside cover of
the textbook contains that mapping.
• If the eighth bit is used (as is done in the
IBM PC to extend the mapping to Greek and
graphics symbols), then the hex numbers
used are 80h to FFh.
EEL 3801C
Character Representation – ASCII
(cont.)
• The programmer has to keep track of what a
binary number in a program stands for.
– It is not inherent in the hardware or the
operating system.
• High level languages do this by forcing you
to declare a variable as being of a certain
type.
– Different data types have different lengths
EEL 3801C