Data Representation - Department of Electronic and Electrical

advertisement
3D1-Microprocessor Systems 1
Lecture 6: Data Representation: integer, characters and signed representations
We have already noted that everything the computer processes must be represented in
binary, including all data. Also, the representation scheme used must be finite. Hence
computers cannot represent non-finite values such as irrational numbers with exact
precision. In this lecture we will examine how characters and numbers are represented, how
they are converted from one base to another, and how they are manipulated within the
computer.
Learning Outcomes:
On completion of this lecture, you will be able to:
 To define ASCII code for characters;
 to represent byte, word and longworld unsigned integer;
 to define and apply the 2’s complement representation format;
 to demonstrate the addition and subtraction of positive and negative numbers.
6.1
Data representation Schemes
Because of the ease with two-state logic can be manufactured and because of their
remarkably low cost, it was inevitable that computer designers chose the binary number
system. The smallest quantity of information that can be stored and manipulated inside a
computer is the bit. A bit is unique because it cannot be subdivided into any smaller unit of
information.
Numbers can be represented in one of many formats: unsigned and signed binary integer,
unsigned and signed BCD integer, binary floating point, etc.
The alpha-numeric characters of the English alphabet (A to Z, a to z, 0 to 9) and symbols
(*, -, +, !, ?, etc.) have assigned binary patterns so that they can be stores and
manipulated within the computer. One particular code, called the ASCII code (American
Standard Code for Information Interchange), is in widespread use throughout the computer
industry. This is also known as the ISO 7-bit character code, and represents characters by 7
bits, allowing a maximum of 27=128 different characters.
6-1
3D1-Microprocessor Systems 1
Our Data Representations:
 We will examine integer and character representations, and we will use bits for data.
 We will not examine floating point.
 The 68000 does not have floating point instructions -- they were added later in the
family.
6.2
Unsigned binary


Integers are represented using ‘ordinary’ (unsigned) binary.
Unsigned binary offers a fast and compact representation.
6.2.1 Sample Unsigned Byte
Bit Number
Weight
Bits



7
2 =128
1
6
2 =64
0
7
5
2 =32
1
6
5
4
2 =16
1
3
2 =8
0
4
2
2 =4
1
3
1
2 =2
1
2
0
2 =1
0
1
0
The weight of a binary digit is 2 (the bit number). Handy.
The smallest value is 00000000; the largest 11111111.
That is, 0 to 255, or 0 to 28-1.
6.2.2 Sample Unsigned Word
Bit Number
Weight
Bits



15
215=32768
1
14
214
0
13
213
1
12
212
1
11
211
0
10
210
1
9
29
1
8
28
0
7
27
1
6
26
0
5
25
1
4
24
1
3
23
0
2
22
1
1
21
1
0
20
0
Value: 1011 0110 1011 0111 = 46,775.
The smallest value is 0000 0000 0000 0000; the largest is 1111 1111 1111 1111, i.e.
0 to 65,535, or 0 to 216-1.
Binary is unwieldy, awkward for big numbers
6.2.3 Longword Unsigned Binary





6.3
Longword unsigned binary goes from
0000 0000 0000 0000 0000 0000 0000 0000 to
1111 1111 1111 1111 1111 1111 1111 1111, or:
$00000000 to $FFFFFFFF
i.e. 0 to 232-1: 0 to 4,294,967,295 (Four giga-) where a giga- is 230.
Hexadecimal







Hexadecimal is Base 16 number notation.
Preceded by a $ in assembly language.
There is a one-to-one bidirectional correspondence between a single hex digit and 4
binary digits. Thus, hexadecimal is a very useful shorthand for binary.
Hexadecimal uses sixteen digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
Same idea as decimal (base 10) or binary (base 2), it is a positional notation for
quantities: each digit has a weight that depends on its position in the number.
A digit’s weight is the base raised to the power of its position number, with the
rightmost position number being 0.
E.g. $A6 is A * 16 10  6 * 16 10  1010 * 16 10  6 10 * 110  16010 .
1
0
6-2
3D1-Microprocessor Systems 1
Decimal
Binary
Hex
0
0000
0




6.4
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
10
1010
A
11
1011
B
12
1100
C
13
1101
D
14
1110
E
15
1111
F
Very easy to convert between binary and hexadecimal — make groups of 4 bits from
least significant end, and look up their hex equivalent digit.
Groups of 4 bits (“nybbles”) fit very well into bytes etc.
Thus, we use hexadecimal very extensively as a shorthand for binary.
Note: hexadecimal is usually used as a shorthand for the binary pattern. It is not an
interpretation of the pattern’s meaning.
Signed Binary


Idea: to be able to represent the sign of a number, not just its magnitude, e.g. -234,
+1,635.
To be able to perform signed calculations, preferably without using much extra
hardware.
6.4.1 Sign and Magnitude
Use one bit to represent sign, the other bits to represent magnitude.
Recall that our basic goal is to represent both positive and negative integers using just 1’s
and 0’s. The most simple method, for N-bit capacity, is to use the most significant bit (msb),
xN-1, to designate the sign and the remaining (N-1) bits to designate the magnitude:
x 1 x N 2  x1 x0

N
 

and the usual convention is
sign
magnitude
x N 1  0  X positive
x N 1  1 

Bit Number
Weight
Bits
X
negative
E.g: 10110110 represents: -54
7
Sign(1 for “-”
0 for “+”)
1
6
26=64
5
25=32
4
24=16
3
23=8
2
22=4
1
21=2
0
20=1
0
1
1
0
1
1
0
Problems with Sign and Magnitude:
 Two versions of zero: +0 and -0;
 Extra hardware needed for calculations.
6.4.2 Two’s complement Notation
This widely used convention is defined as follows:
Let X denote an N-bit number whose most significant bit (msb) is zero,
X  0 x N 2  x1 x0
ie
then
This is




 X 2C
X
and
 X 2C
 2N  X
called ‘Two’s Complement’ notation:
If a number is positive, represent it with straight binary ;
If a number is negative, represent it in two’s complement form;
In a machine with fixed width number representation, ensure:
If positive, the MS bit must be 0;
6-3
3D1-Microprocessor Systems 1
 If negative, the MS bit must be 1.
Getting Twos Complement:
 To get the twos complement of a number:
 Size it to the correct width, introducing leading zeroes as necessary.;
 Flip each bit: 0 to 1 and 1 to 0 ;
 Add 1.
Features:
 One version of zero: +0
 Slight asymmetry of magnitude representation.
 Same hardware can be used.
Note:
(i)
Similar to the sign and magnitude system, in signed 2’s complement the msb denotes
the polarity of the number: x N 1  0  X positive;
x N 1  1  X negative
(ii)
If 2C[ ] denotes the 2’s complement operation, then 2C
 X 2C    X 2C
ie 2’s complementing a negative number yields the corresponding positive number.
6.5
Exercises:
1- Convert the following decimal numbers to binary and hex:
39, 147, 2911, 35793
2- Convert the following binary numbers to decimal:
101101,
1011101110,
001011010101000001,
11010010111010111111
3- Using the accompanying ASCII code table, convert the following 'character string'
into binary: This is a string.
6.6
Conclusion: Binary Representation Summary
Size
Byte
Byte
Word
Word
Longword
Longword
Representation
Unsigned Binary
Signed Binary
Unsigned Binary
Signed Binary
Unsigned Binary
Signed Binary
Range
0
255 (0 to 28-1)
-128
+127 (-27 to 27-1)
0
65535 (0 to 216-1)
-32768
32767 (-215 to 215-1)
0
4,294,967,295 (0 to 232-1)
-2,147,483,648 +2,147,483,647
(-231 to 231-1)
REFERENCES

Dr. Brian Foley; 2E6 – Digital Electronics – lecture 2: Signed Numbers – Representation
and Arithmetic; dept. of Electronic and Electrical Engineering, Trinity College Dublin.

Dr. Mike Brady, Microprocessor Systems 1, dept. of Computer Science, Trinity College
Dublin: http://www.tcd.ie/Engineering/Courses/BAI/JS_Subjects/3D1/.

Look on the Web at http://www.mee.tcd.ie/~assambc/3D1.
6-4
Download