Download Bits, Data Types, and Operations

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

Electronic paper wikipedia , lookup

Transcript
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Agenda
1.
2.
3.
4.
5.
6.
7.
Chapter 2
Bits, Data Types,
and Operations
Data Types
Unsigned & Signed Integers
Arithmetic Operations
Logical Operations
Shifting
Hexadecimal & Octal Notation
Other Data Types
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1. Data Types
Computer is a binary digital system.
How do we represent data in a computer?
At the lowest level, a computer is an electronic machine.
Digital system:
• finite number of symbols
Binary (base two) system:
• has two states: 0 and 1
• works by controlling the flow of electrons
Easy to recognize two conditions:
1. presence of a voltage – we’ll call this state “1”
2. absence of a voltage – we’ll call this state “0”
Basic unit of information is the binary digit, or bit.
Values with more than two states require multiple bits.
• A collection of two bits has four possible states:
00, 01, 10, 11
Could base state on value of voltage,
but control and detection circuits more complex.
• A collection of three bits has eight possible states:
000, 001, 010, 011, 100, 101, 110, 111
• compare turning on a light switch to
measuring or regulating voltage
COMPSCI210 S1C 2009
• A collection of n bits has 2n possible states.
3
COMPSCI210 S1C 2009
4
1
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
What kinds of data do we need to represent?
2. Unsigned & Signed Integers
Decimal Numbers
• Numbers – signed, unsigned, integers, floating point,
complex, rational, irrational, …
• Text – characters, strings, …
• Images – pixels, colors, shapes, …
• Sound
• Logical – true, false
• Instructions
• …
“decimal” means that we have ten digits to use in our
representation (the symbols 0 through 9)
What is 3,546?
• it is three thousands plus five hundreds plus four tens plus six ones.
• i.e. 3,546 = 3.103 + 5.102 + 4.101 + 6.100
How about negative numbers?
• we use two more symbols to distinguish positive and negative:
Data type:
+ and –
Note that not everyone uses base 10
• representation and operations within the computer
We’ll start with numbers…
• Vigesimal (base 20)http://en.wikipedia.org/wiki/Vigesimal
COMPSCI210 S1C 2009
5
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Unsigned Integers
Unsigned Binary Integers
Non-positional notation
Y = “abc” = a.22 + b.21 + c.20
• could represent a number (“5”) with a string of ones (“11111”)
• problems?
(where the digits a, b, c can each take on the values of 0 or 1 only)
3-bits
5-bits
8-bits
0
000
00000
00000000
1
001
00001
00000001
2
010
00010
00000010
3
011
00011
00000011
4
100
00100
00000100
N = number of bits
Weighted positional notation
Range is:
0  i < 2N - 1
• like decimal numbers: “329”
• “3” is worth 300, because of its position, while “9” is only worth 9
329
102 101 100
3x100 + 2x10 + 9x1 = 329
COMPSCI210 S1C 2009
most
significant
22
101
21
least
significant
Problem:
• How do we represent
negative numbers?
20
1x4 + 0x2 + 1x1 = 5
7
COMPSCI210 S1C 2009
8
2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Unsigned Binary Arithmetic
Signed Integers
Base-2 addition – just like base-10!
With n bits, we have 2n distinct values.
• add from right to left, propagating carry
• assign about half to positive integers (1 through 2n-1)
and about half to negative (- 2n-1 through -1)
• that leaves two values: one for 0, and one extra
carry
Positive integers
• just like unsigned – zero in most significant (MS) bit
00101 = 5
Negative integers
• sign-magnitude – set MS bit to show negative,
other bits are the same as unsigned
10101 = -5 -1 * (0×23 + 1×22 + 0×21 + 1×20)
• one’s complement – flip every bit to represent negative
11010 = -5 (-00101)
• in either case, MS bit indicates sign: 0=positive, 1=negative
Subtraction, multiplication, division,…
COMPSCI210 S1C 2009
9
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
One’s Complement
Signed Magnitude
Leading bit is the sign bit
Y = “abc” = (-1)a (b.21 + c.20)
Range is:
-2N-1 + 1 <= i <= 2N-1 - 1
E.g. 3 bits: -3 <= i <= 3
Problems:
• How do we do addition/subtraction?
• We have two numbers for zero (+/-)!
COMPSCI210 S1C 2009
10
-3
111
Invert all bits
-3
100
-2
110
-2
101
-1
101
-1
110
-0
100
+0
000
+1
001
+2
010
+3
011
If msb (most significant bit) is 1 then the
number is negative (same as signed
magnitude)
Range is:
-2N-1 + 1 <= i <= 2N-1 - 1
E.g. 3 bits: -3 <= i <= 3
-0
111
+0
000
+1
001
+2
010
+3
011
Problems:
•Same as for signed magnitude
11
COMPSCI210 S1C 2009
12
3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Two’s Complement
Two’s Complement Representation
Problems with sign-magnitude and 1’s complement
If number is positive or zero,
• two representations of zero (+0 and –0)
• arithmetic circuits are complex
How to add two sign-magnitude numbers?
• normal binary representation, zeroes in upper bit(s)
If number is negative,
• start with positive number
• flip every bit (i.e., take the one’s complement)
• then add one
– e.g., try 2 + (-3)
How to add to one’s complement numbers?
– e.g., try 4 + (-3)
Two’s complement representation developed to make
circuits easy for arithmetic.
• for each positive number (X), assign value to its negative (-X),
such that X + (-X) = 0 with “normal” addition, ignoring carry out
(5)
(-5)
(0)
(9)
(-9)
(0)
COMPSCI210 S1C 2009
13
(1’s comp)
(-5)
(-9)
14
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Two’s Complement Signed Integers
Two’s Complement Shortcut
MS bit is sign bit – it has weight –2n-1.
Range of an n-bit number: -2n-1 through 2n-1 – 1.
To take the two’s complement of a number:
• copy bits from right to left until (and including) the first “1”
• flip remaining bits to the left
COMPSCI210 S1C 2009
(9)
(1’s comp)
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
(1’s comp)
(5)
(flip)
• The most negative number (-2n-1) has no positive counterpart.
(copy)
15
-23
22
21
20
0
0
0
0
0
0
0
1
0
0
1
0
0
0
-23
22
21
20
0
1
0
0
0
-8
1
1
0
0
1
-7
0
2
1
0
1
0
-6
1
1
3
1
0
1
1
-5
1
0
0
4
1
1
0
0
-4
0
1
0
1
5
1
1
0
1
-3
0
1
1
0
6
1
1
1
0
-2
0
1
1
1
7
1
1
1
1
-1
To
COMPSCI210 S1C 2009
go from n to –n: Two’s Complement conversion
16
4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Applet: Bin2Dec.html
Converting Binary (2’s C) to Decimal
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
More Examples
1. If leading bit is one, take two’s
complement to get a positive number.
2. Add powers of 2 that have “1” in the
corresponding bit positions.
3. If original number was negative,
add a minus sign.
n
0
1
2
3
4
5
6
7
8
9
10
X = 01101000two
= 26+25+23 = 64+32+8
= 104ten
2n
1
2
4
8
16
32
64
128
256
512
1024
Assuming 8-bit 2’s complement numbers.
X = 00100111two
= 25+22+21+20 = 32+4+2+1
= 39ten
X =
-X =
=
=
X=
n 2n
0
1
2
3
4
5
6
7
8
9
10
11100110two
00011010
24+23+21 = 16+8+2
26ten
-26ten
Assuming 8-bit 2’s complement numbers.
COMPSCI210 S1C 2009
17
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
COMPSCI210 S1C 2009
Applet: Dec2Bin.html
18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Converting Decimal to Binary (2’s C)
Converting Decimal to Binary (2’s C)
First Method: Division
Second Method: Subtract Powers of Two
1. Find magnitude of decimal number.
2. Subtract largest power of two
less than or equal to number.
3. Put a one in the corresponding bit position.
4. Keep subtracting until result is zero.
5. Append a zero as MS bit;
if original was negative, take two’s complement.
1. Find magnitude of decimal number. (Always positive.)
2. Divide by two – remainder is least significant bit.
3. Keep dividing by two until answer is zero,
writing remainders from right to left.
4. Append a zero as the MS bit;
if original number was negative, take two’s complement.
X = 104ten
X = 01101000two
COMPSCI210 S1C 2009
104/2
52/2
26/2
13/2
6/2
3/2
1
2
4
8
16
32
64
128
256
512
1024
=
=
=
=
=
=
52 r0
26 r0
13 r0
6 r1
3 r0
1 r1
1/2 = 0 r1
bit 0
bit 1
bit 2
bit 3
bit 4
bit 5
X = 104ten
bit 6
19
X = 01101000two
COMPSCI210 S1C 2009
104 - 64 = 40
40 - 32 = 8
8-8 = 0
n 2n
0
1
2
3
4
5
6
7
8
9
10
1
2
4
8
16
32
64
128
256
512
1024
bit 6
bit 5
bit 3
20
5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Examples: (Decimal -> Binary)
Examples: (Binary -> Decimal)
Convert –2310 to binary numbers in
Convert 10101010 to Decimal if the number is
represented as
• 8-bit, Signed Magnitude
10010111
• 8-bit, Signed Magnitude
10101010 -> Negative numbers
= -42
• 8-bit, One’s complement
23 -> Binary: 00010111
One’s complement -> 11101000
• Signed 8-bit in two’s complement
10101010 -> Negative numbers
=> Complement it = 01010101 = 85
=> Add 1 = 86
= -86
• 8-bit, Two’s complement:
23 -> Binary: 00010111
One’s complement -> 11101000
Two’s complement -> 11101001
COMPSCI210 S1C 2009
• Unsigned 8-bit
10101010 = 170
21
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
22
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Exercises (Decimal -> Binary)
Exercises (Binary -> Decimal)
Convert Decimal –17 to 8-bit binary numbers in
Convert 10110011 to Decimal if the number is represented
as
• Signed Magnitude
• Signed Magnitude
• One’s complement:
• Signed 8-bit two’s complement
• Two’s complement:
• Unsigned 8-bit
COMPSCI210 S1C 2009
23
COMPSCI210 S1C 2009
24
6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Applet: NumOps.html
3. Arithmetic Operations
Addition
Recall:
a data type includes representation and operations.
We now have a good representation for signed integers,
so let’s look at some arithmetic operations:
As we’ve discussed, 2’s comp. addition is just
binary addition.
• Addition
• Subtraction
• Sign Extension
• assume all integers have the same number of bits
• ignore carry out
• for now, assume that sum fits in n-bit 2’s comp. representation
We’ll also look at overflow conditions for addition.
Multiplication, division, etc., can be built from these
basic operations.
(104)
(-16)
(98)
(-10)
(-9)
(-19)
Assuming 8-bit 2’s complement numbers.
COMPSCI210 S1C 2009
25
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
26
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Subtraction
Exercise
Negate subtrahend (2nd no.) and add.
01101011 – 00010110
• assume all integers have the same number of bits
• ignore carry out
• for now, assume that difference fits in n-bit 2’s comp.
representation
(104)
(16
(104)
(-16)
(88)
00101100 – 01101001
(-10)
(-9)
(-10)
(9)
(-1)
Assuming 8-bit 2’s complement numbers.
COMPSCI210 S1C 2009
27
COMPSCI 105
28
7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Sign Extension
Sign extension: How it works
To add two numbers, we must represent them
with the same number of bits.
If we just pad with zeroes on the left:
4-bit
8-bit
The sign bit is replicated:
(4)
(-4)
0x9876: 1001 1000 0111 0110: -2^15 + 2^12+……
0xff9876: 1111 1111 1001 1000 0111 0110:
-2^23+2^22+2^21+2^20+2^19+…+2^15+2^12+…. =
-2^22 +2^21+………………………………… =
-2^21 +2^20+………………………….. =
(still 4)
(12, not -4)
2^16+2^15+2^12+………. =
-2^15+2^12+………. = 0x9876
By the same way you can demonstrate that the quadword 0xff…ff9876 is
equal to the word 0x9876. Using 2’s representation convention you can
also see that the quadword 0xff…ff9876 (which is a negative number) is
equal to the word –0x678a
Instead, replicate the MS bit -- the sign bit:
4-bit
8-bit
(4)
(-4)
(still 4)
(still -4)
COMPSCI210 S1C 2009
29
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
30
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Overflow
Examples (4-bit)
If operands are too big, then sum cannot be represented
as an n-bit 2’s comp number.
Assuming 4-bit 2’s complement numbers
(8)
(9)
(-15)
• Range - 8 <= x <= 7
6
(-8)
(-9)
(+15)
6 + 7 = 13
0110
(outside the range)
7
+ 0111
01100 carries
01101 (4-bit) => 1101
Answer = 3
Invalid answer
Answer = -3
Invalid answer
We have overflow if:
• signs of both operands are the same, and
• sign of sum is different.
-2
-2 + 3 = 1
1110
3
+ 0011
Answer = 1
11100 carries valid answer
10001 (4-bit)=0001
Another test -- easy for hardware:
• carry into MS bit does not equal carry out
COMPSCI210 S1C 2009
-6 + -7 = -13
1010 -6 (outside the range)
-7
+ 1001
10000 carries
10011(4-bit)= 0011
31
COMPSCI210 S1C 2009
2
2+3=5
0010
3
+0011
0100carries
0101
Answer = 5
Valid answer
32
8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Valid or invalid?
Examples
Overflow occurs if
• Look at the carries into and out of the sign bit. If they
are not equal, there is an overflow.
• +ve number add +ve number => -ve number
• -ve number add –ve number => +ve number
Example:
Carry into
the sign bit
• 6 + 7 = -3 (actual answer = 13)
• -6 + -7 = 3 (actual answer = -13)
A better way:
1110
0011
11100 carries
10001
Carry into &
Carry out +
• Look at the carries into and out of the sign bit. If they are not
equal, there is an overflow.
• Not equal: -> overflow
• Equal = No Overflow
COMPSCI210 S1C 2009
COMPSCI 105
No carry
1010
+ 1001
10000 carries
10011
0010
+ 0011
00100 carries
0101
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Exercises
1100
+ 0111
Carry out of
the sign bit
33
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
0111
+ 0101
0110
+ 0111
01100 carries
01101
Applet: BinOps.html
4. Logical Operations
Logical operations are also useful:
1001
+ 1100
•
•
•
•
AND
OR
NOT
XOR
1001
+ 0011
35
COMPSCI210 S1C 2009
36
9
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Logical Operations
Examples of Logical Operations
Operations on logical TRUE or FALSE
AND
• two states -- takes one bit to represent: TRUE=1, FALSE=0
B A OR B
0
0
1
1
0
1
A B A AND B
A B A XOR B
1
1
0 0
0
0 0
0
0 1
0
0 1
1
1 0
0
1 0
1
1 1
1
1 1
0
View n-bit number as a collection of n logical values
A
0
1
NOT A
1
0
A
0
0
1
1
• useful for clearing bits
AND with zero = 0
AND with one = no change
AND
OR
• useful for setting bits
OR with zero = no change
OR with one = 1
OR
• operation applied to each bit independently
COMPSCI210 S1C 2009
37
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
38
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Examples of Logical Operations
Exercises
NOT
• unary operation -- one argument
• flips every bit
NOT
XOR
• Same = 0
• Different = 1
XOR
COMPSCI210 S1C 2009
39
COMPSCI 105
Handout 04
40
10
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
5. Shifting
Sll <<
Shift operators move bits to the left or to the right.
Examples
• 1101 0101 << 1
• 0001 0101 << 2
• 0000 0101 << 3
• Used to shift the bit patterns left and right.
• Shift corresponds to division/multiplication by powers of 2 (no
overflow problem)
• Three shift instructions: “ sll” (shift left logical), “ sra” (shift right
arithmetic), and “srl” (shift right logical), corresponding to <<, >>
and >>>.
• The shift logical instructions fill the vacated bits with 0
• The shift right arithmetic instruction fills the vacated
bits with the sign bit.
• These instructions can be used to extract fields out of a
bit pattern, and interpret them as either unsigned or
signed numbers.
COMPSCI210 S1C 2009
41
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
SRA (sign fill) >>
Examples
• 1101 0101 >> 1
• 0001 0101 >> 2
• 1001 0011 >> 3
1
0
1
0
1
0
1
1
1
1
0
1
0
1
0
0
0
0
1
0
1
0
1
21
x
0
Exercise
• 1010 0001 >> 3
0
0
0
0
1
0
5
1
1
0
1
0
1
0
1
1
0
1
0
1
0
1
0
-86
0
21
0
0
0
1
0
1
0
0
1
0
1
0
1
-14
1
-13
-13.625
5
5.25
COMPSCI210 S1C 2009
0
0
1
0
0
1
1
0
0
1
0
1
1
0
0
5
0
0
0
0
0
1
0
1
40
0
0
1
0
1
0
0
COMPSCI210 S1C 2009
0
42
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
x 1
1
0
1
0
1
0
1
0
1
1
0
1
0
1
0
Example
• 11010101 >>> 1
• 0001 0101 >>> 2
• 1001 0011 >>> 2
Exercise
• 1010 0001 >>> 3
22
0
x
0
0
0
0
0
0
1
0
0
1
1
0
0
1
-109
x 1
1
84
SRL (zero fill) >>>
-22
-43
1
Exercise
• 0000 1100 << 3
-43
x 1
x
1
5
-109
1
x 1
-14
0
0
0
1
0
0
1
0
0
1
0
0
1
0
1
36
6
43
COMPSCI210 S1C 2009
44
11
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
6. Hexadecimal & Octal Notation
6. Hexadecimal & Octal Notation
Hexadecimal Notation
It is often convenient to write binary (base-2) numbers
as hexadecimal (base-16) numbers instead.
Octal Notation
It is often convenient to write binary (base-2) numbers
as octal (base-8) numbers instead.
• fewer digits -- four bits per hex digit
• less error prone -- easy to corrupt long string of 1’s and 0’s
Binary
Hex
Decimal
Binary
Hex
Decimal
0000
0001
0010
0011
0100
0101
0110
0111
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
1000
1001
1010
1011
1100
1101
1110
1111
8
9
A
B
C
D
E
F
8
9
10
11
12
13
14
15
COMPSCI210 S1C 2009
• fewer digits – three bits per octal digit
45
Binary
Octal
Decimal
000
001
010
011
100
101
110
111
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
46
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Converting from Decimal to Octal/Hexadecimal
Converting from Octal/Hexadecimal to Decimal
Decimal -> Octal
Octal to Decimal
• 122
• 1278
• 220
• 2358
= 1 x 82 + 2 x 81 + 7 x 80
= 64 + 16 + 7 = 87
= 2 x 82 + 3 x 81 + 5 x 80
= 128 + 24 + 5 = 157
Decimal -> Hexadecimal
• 1940
Hexadecimal to Decimal
• 1A716
= 1 x 162 + 10 x 161 + 7 x 160
= 256 + 160 + 7 = 423
• 175
• 23E16
= 2 x 162 + 3 x 161 + 14 x 160
= 512 + 48 + 14 = 574
COMPSCI210 S1C 2009
47
COMPSCI 105
Handout 02
48
12
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Exercise
Binary -> Octal/Hexadecimal
Decimal -> Octal
Binary -> Octal
• 1234
•
•
•
•
•
Decimal -> Hexadecimal
• 689
Every three bits is a octal digit
Start grouping from right-hand side
0101 1111
1101 0001
Exercise: 101101001
Octal to Decimal
Binary -> Hexadecimal
• 151
•
•
•
•
•
Hexadecimal to Decimal
• 15B
COMPSCI210 S1C 2009
49
Every four bits is a hex digit
Start grouping from right-hand side
0101 1111
1101 0001
Exercise: 101001101
COMPSCI210 S1C 2009
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
50
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Octal -> Binary/Hexadecimal
Hexadecimal -> Binary/Octal
Octal -> Binary
Hexadecimal -> Binary
• 363
• 247
• Exercise: 123
• EA3
• 2A7
• Exercise: 1F3
Hexadecimal -> Octal
Octal -> Hexadecimal
• EA3
• 2A7
• Exercise: 1F3
• 363
• 247
• Exercise: 123
COMPSCI210 S1C 2009
51
COMPSCI210 S1C 2009
52
13
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Interesting Properties of ASCII Code
Text: ASCII Characters
•What is relationship between a decimal digit ('0', '1', …)
and its ASCII code?
ASCII: Maps 128 characters to 7-bit code.
• both printable and non-printable (ESC, DEL, …) characters
•What is the difference between an upper-case letter
('A', 'B', …) and its lower-case equivalent ('a', 'b', …)?
•Given two ASCII characters, how do we tell which comes first in
alphabetical order?
Minimal memorizing effort allows you to retain more than 50
character ASCII code!!
Are 128 characters enough?
(http://www.unicode.org/)
No new operations -- integer arithmetic and logic.
COMPSCI210 S1C 2009
53
COMPSCI210 S1C 2009
54
Hexadecimal characters ASCII code
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Other Data Types
LC-3 Data Types
Text strings
Some data types are supported directly by the
instruction set architecture.
• sequence of characters, terminated with NULL (0)
• typically, no hardware support
Image
For LC-3, there is only one hardware-supported data type:
• array of pixels
monochrome: one bit (1/0 = black/white)
color: red, green, blue (RGB) components (e.g., 8 bits each)
other properties: transparency
• hardware support:
typically none, in general-purpose processors
MMX -- multiple 8-bit operations on 32-bit word
• 16-bit 2’s complement signed integer
• Operations: ADD, AND, NOT
Other data types are supported by interpreting
16-bit values as logical, text, fixed-point, etc.,
in the software that we write.
Sound
• sequence of fixed-point numbers
COMPSCI210 S1C 2009
55
COMPSCI210 S1C 2009
56
14