Chapter 2 Data Representation.

Slides:



Advertisements
Similar presentations
The Binary Numbering Systems
Advertisements

©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
©Brooks/Cole, 2003 Chapter 9 Programming Languages.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, Java Version, Third Edition.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
©Brooks/Cole, 2003 Chapter 5 Computer Organization.
Chapter 4 Operations on Bits
Assembly Language and Computer Architecture Using C++ and Java
Chapter 5 Computer Organization ( 計算機組織 ). Distinguish between the three components of a computer hardware. List the functionality of each component.
5.1 Chaper 4 Central Processing Unit Foundations of Computer Science  Cengage Learning.
Number Representation Rizwan Rehman, CCS, DU. Convert a number from decimal to binary notation and vice versa. Understand the different representations.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
Chapter 3 Number Representation. Convert a number from decimal 、 hexadecimal,octal to binary notation and vice versa. Understand the different representations.
Chapter 2 Data and Number Representations
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Data Representation.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 Understand the concept of an algorithm. Define and use the three constructs for developing.
Foundations of Computer Science Chapter 4 Operations on Bits.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 5 Computer Organization. Distinguish between the three components of a computer hardware. List the functionality of each component. Understand.
Chapter 2 Data Representation.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Computer Organization. The digital computer is a digital system that performs various computational tasks Digital computer use binary number system which.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
Chapter 8 Algorithms.
©Brooks/Cole, 2003 Chapter 5 Computer Organization.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Main Memory Main memory – –a collection of storage locations, –each with a unique identifier called the address. Word- –Data are transferred to and from.
Chapter 5 Computer Organization ( 計算機組織 ). Distinguish between the three components of a computer hardware. List the functionality of each component.
Unit 1 Introduction Number Systems and Conversion.
Data Representation COE 308 Computer Architecture
Computer Science: An Overview Eleventh Edition
Invitation to Computer Science, C++ Version, Fourth Edition
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Chapter 4 Operations on Bits.
Assembly Language (CSW 353)
3.1 Denary, Binary and Hexadecimal Number Systems
Topics Introduction Hardware and Software How Computers Store Data
Chapter 3 Data Storage.
Invitation to Computer Science, Java Version, Third Edition
Chapter 1 Data Storage.
Data Representation COE 301 Computer Organization
Computer Organization
Computer Organization
Topics Introduction Hardware and Software How Computers Store Data
Chapter 2 Data Representation.
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter Four Data Representation in Computers By Bezawit E.
Data Representation ICS 233
Chapter 5 Computer Organization
Teori Bahasa dan Automata Lecture 13: Algorithm
Binary & Hex Review.
OBJECTIVES After reading this chapter, the reader should be able to :
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Data Representation COE 308 Computer Architecture
Presentation transcript:

Chapter 2 Data Representation

OBJECTIVES After reading this chapter, the reader should be able to: Define data types. Visualize how data are stored inside a computer. Understand the differences between text, numbers, images, video, and audio. Work with hexadecimal and octal notations.

2.1 DATA TYPES

Different types of data Figure 2-1 Different types of data

Note: The computer industry uses the term “multimedia” to define information that contains numbers, text, images, audio, and video.

2.2 DATA INSIDE THE COMPUTER

Figure 2-2 Bit pattern

Examples of bit patterns Figure 2-3 Examples of bit patterns

2.3 REPRESENTING DATA

Representing symbols using bit patterns Figure 2-4 Representing symbols using bit patterns

Table 2.1 Number of symbols and bit pattern length 4 8 16 … 128 256 65,536 Bit Pattern Length --------------------- 1 2 3 4 … 7 8 16

Representation of the word Figure 2-5 Representation of the word “BYTE” in ASCII code

Unicode 16 bits e.g. 趙(8D99) 坤(5764) 茂(8302)

http://www.unicode.org/

http://www.cns11643.gov.tw/seeker/chinese/search.jsp

http://www-atm.physics.ox.ac.uk/user/iwi/charmap.html

Image representation methods Figure 2-6 Image representation methods

Bitmap graphic method of a black-and-white image Figure 2-7 Bitmap graphic method of a black-and-white image

Representation of color pixels Figure 2-8 Representation of color pixels

Figure 2-9 Audio representation

2.4 HEXADECIMAL NOTATION

Note: A 4-bit pattern can be represented by a hexadecimal digit, and vice versa.

Table 2.2 Hexadecimal digits Bit Pattern ------------ 0000 0001 0010 0011 0100 0101 0110 0111 Hex Digit ------------ 1 2 3 4 5 6 7 Bit Pattern ------------ 1000 1001 1010 1011 1100 1101 1110 1111 Hex Digit ------------ 8 9 A B C D E F

Binary to hexadecimal and hexadecimal to binary transformation Figure 2-10 Binary to hexadecimal and hexadecimal to binary transformation

Example 1 Show the hexadecimal equivalent of the bit pattern 1100 1110 0010. Solution Each group of 4 bits is translated to one hexadecimal digit. The equivalent is xCE2.

Show the hexadecimal equivalent of the bit pattern 0011100010. Example 2 Show the hexadecimal equivalent of the bit pattern 0011100010. Solution Divide the bit pattern into 4-bit groups (from the right). In this case, add two extra 0s at the left to make the number of bits divisible by 4. So you have 000011100010, which is translated to x0E2.

Example 3 What is the bit pattern for x24C? Solution Write each hexadecimal digit as its equivalent bit pattern to get 001001001100.

2.5 OCTAL NOTATION

A 3-bit pattern can be represented by an octal digit, and vice versa. Note: A 3-bit pattern can be represented by an octal digit, and vice versa.

Table 2.3 Octal digits Bit Pattern ------------ 000 001 010 011 Oct Digit ------------ 1 2 3 Bit Pattern ------------ 100 101 110 111 Oct Digit ------------ 4 5 6 7

Binary to octal and octal to binary transformation Figure 2-11 Binary to octal and octal to binary transformation

Example 4 Show the octal equivalent of the bit pattern 101110010. Solution Each group of 3 bits is translated to one octal digit. The equivalent is 0562, o562, or 5628.

Show the octal equivalent of the bit pattern 1100010. Example 5 Show the octal equivalent of the bit pattern 1100010. Solution Divide the bit pattern into 3-bit groups (from the right). In this case, add two extra 0s at the left to make the number of bits divisible by 3. So you have 001100010, which is translated to 1428.

Write each octal digit as its equivalent bit pattern to get 010100. Example 6 What is the bit pattern for 248? Solution Write each octal digit as its equivalent bit pattern to get 010100.

一個小小的問題 Oct-這個字根代表8; Dec-這個字根代表10,為什麼October 不是八月而是十月?為什麼 December不是十月而是十二月?

因為插入了七月和八月 July源於凱撒(Julius Caesar) 之名,凱撒原是一位有名的羅馬將領,後來當了羅馬皇帝。在這之前就有曆法,那時是以March 為一年的開端,而July是第十五個月;到了凱撒當皇帝便修改曆法,將一年的開始訂為January,而將July 提升到第七位,這個改變一直沿用至今。 凱撒大帝之後,他的兒子奧古斯都(Augustus)繼承王位,人們尊稱他為Augustus,其意義乃代表高貴 (Noble),於是他學凱撒將他的幸運月,以自己的封號命名,並從二月挪一天過來,變成三十一日。 那二月為什麼只有二十八天呢?

二月被砍過兩天 二月為什麼通常只有二十八天? 凱撒(Julius Caesar) 修改曆法時,本來規定每年十二個月裡,逢單是大月三十一日,逢雙是小月三十日,但是這樣算下來,一年就變成三百六十六日,所以必須設法在一年中扣去一天。那時候判處死刑的人犯均在二月分執行,因此人們認為二月是不吉利的月分,既然要扣除一天,那麼就由二月分來扣掉,讓不吉利的日子減少一天,因此二月分就成了二十九日。 後來奧古斯都(Augustus)繼凱撒之後當了羅馬皇帝,他發現凱撒是七月生的,而七月是逢單為大月三十一日;他不服氣,為了表示自己也偉大,就把自己八月出生的月分改為大月三十一日。糟了又多出一天怎麼辦?那還是由二月分來扣除,因此結果二月分就變成二十八日。

Number Representation Chapter 3 Number Representation

OBJECTIVES After reading this chapter, the reader should be able to : Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer inside a computer: unsigned, sign-and-magnitude, one’s complement, and two’s complement. Understand the Excess system that is used to store the exponential part of a floating-point number. Understand how floating numbers are stored inside a computer using the exponent and the mantissa.

3.1 DECIMAL AND BINARY

Figure 3-1 Decimal system

Figure 3-2 Binary system

3.2 CONVERSION

Binary to decimal conversion Figure 3-3 Binary to decimal conversion

Convert the binary number 10011 to decimal. Example 1 Convert the binary number 10011 to decimal. Solution Write out the bits and their weights. Multiply the bit by its corresponding weight and record the result. At the end, add the results to get the decimal number. Binary 1 0 0 1 1 Weights 16 8 4 2 1 ------------------------------------- 16 + 0 + 0 + 2 + 1 Decimal 19

Convert the decimal number 35 to binary. Example 2 Convert the decimal number 35 to binary. Solution Write out the number at the right corner. Divide the number continuously by 2 and write the quotient and the remainder. The quotients move to the left, and the remainder is recorded under each quotient. Stop when the quotient is zero. 0  1  2  4  8  17  35 Dec. Binary 1 0 0 0 1 1

Decimal to binary conversion Figure 3-4 Decimal to binary conversion

Changing fractions to binary Figure 3-7 Changing fractions to binary

Transform the fraction 0.875 to binary Example 17 Transform the fraction 0.875 to binary Solution Write the fraction at the left corner. Multiply the number continuously by 2 and extract the integer part as the binary digit. Stop when the number is 0.0. 0.875  1.750  1.5  1.0  0.0 0 . 1 1 1

Transform the fraction 0.4 to a binary of 6 bits. Example 18 Transform the fraction 0.4 to a binary of 6 bits. Solution Write the fraction at the left cornet. Multiply the number continuously by 2 and extract the integer part as the binary digit. You can never get the exact binary representation. Stop when you have 6 bits. 0.4  0.8  1.6  1.2  0.4  0.8  1.6 0 . 0 1 1 0 0 1

3.4 INTEGER REPRESENTATION

Figure 3-5 Range of integers

Figure 3-6 Taxonomy of integers

Table 3.1 Range of unsigned integers # of Bits --------- 8 16 Range ------------------------------------- 0 255 0 65,535

Store 7 in an 8-bit memory location. Example 3 Store 7 in an 8-bit memory location. Solution First change the number to binary 111. Add five 0s to make a total of N (8) bits, 00000111. The number is stored in the memory location.

Store 258 in a 16-bit memory location. Example 4 Store 258 in a 16-bit memory location. Solution First change the number to binary 100000010. Add seven 0s to make a total of N (16) bits, 0000000100000010. The number is stored in the memory location.

Table 3.2 Example of storing unsigned integers in two different computers (一個為8-bit; 另一個為16-bit) Decimal ------------ 7 234 258 24,760 1,245,678 8-bit allocation ------------ 00000111 11101010 overflow 16-bit allocation ------------------------------ 0000000000000111 0000000011101010 0000000100000010 0110000010111000 overflow

Using the procedure shown in Figure 3.3 , the number in decimal is 43. Example 5 Interpret 00101011 in decimal if the number was stored as an unsigned integer. Solution Using the procedure shown in Figure 3.3 , the number in decimal is 43.

------------------------------------------------------- Table 3.3 Range of sign-and-magnitude integers # of Bits ---------- 8 16 32 Range ------------------------------------------------------- -127 -0 -32767 -0 -2,147,483,647 -0 +0 +127 +0 +32767 +0 +2,147,483,647

Note: In sign-and-magnitude representation, the leftmost bit defines the sign of the number. If it is 0, the number is positive.If it is 1, the number is negative.

There are two 0s in sign-and- magnitude Note: There are two 0s in sign-and- magnitude representation: positive and negative. In an 8-bit allocation: +0  00000000 -0  10000000

Example 6 Store +7 in an 8-bit memory location using sign-and-magnitude representation. Solution First change the number to binary 111. Add four 0s to make a total of N-1 (7) bits, 0000111. Add an extra zero because the number is positive. The result is: 00000111

Example 7 Store –258 in a 16-bit memory location using sign-and-magnitude representation. Solution First change the number to binary 100000010. Add six 0s to make a total of N-1 (15) bits, 000000100000010. Add an extra 1 because the number is negative. The result is: 1000000100000010

------------------------------ Table 3.4 Example of storing sign-and-magnitude integers in two computers Decimal ------------ +7 -124 +258 -24,760 8-bit allocation ------------ 00000111 11111100 overflow 16-bit allocation ------------------------------ 0000000000000111 1000000001111100 0000000100000010 1110000010111000

Example 8 Interpret 10111011 in decimal if the number was stored as a sign-and-magnitude integer. Solution Ignoring the leftmost bit, the remaining bits are 0111011. This number in decimal is 59. The leftmost bit is 1, so the number is –59.

There are two 0s in one’s complement Note: There are two 0s in one’s complement representation: positive and negative. In an 8-bit allocation: +0  00000000 -0  11111111

One’s complement (一位補數法) If the sign is positive (0), no more action is needed; If the sign is negative, every bit is complemented.

------------------------------------------------------- Table 3.5 Range of one’s complement integers # of Bits --------- 8 16 32 Range ------------------------------------------------------- -127 -0 -32767 -0 -2,147,483,647 -0 +0 +127 +0 +32767 +0 +2,147,483,647

Note: In one’s complement representation, the leftmost bit defines the sign of the number. If it is 0, the number is positive.If it is 1, the number is negative.

Example 9 Store +7 in an 8-bit memory location using one’s complement representation. Solution First change the number to binary 111. Add five 0s to make a total of N (8) bits, 00000111. The sign is positive, so no more action is needed. The result is: 00000111

Example 10 Store –258 in a 16-bit memory location using one’s complement representation. Solution First change the number to binary 100000010. Add seven 0s to make a total of N (16) bits, 0000000100000010. The sign is negative, so each bit is complemented. The result is: 1111111011111101

------------------------------ Table 3.6 Example of storing one’s complement integers in two different computers Decimal ------------ +7 -7 +124 -124 +24,760 -24,760 8-bit allocation ------------ 00000111 11111000 01111100 10000011 overflow 16-bit allocation ------------------------------ 0000000000000111 1111111111111000 0000000001111100 1111111110000011 0110000010111000 1001111101000111

Example 11 Interpret 11110110 in decimal if the number was stored as a one’s complement integer. Solution The leftmost bit is 1, so the number is negative. First complement it . The result is 00001001. The complement in decimal is 9. So the original number was –9. Note that complement of a complement is the original number.

Note: One’s complement means reversing all bits. If you one’s complement a positive number, you get the corresponding negative number. If you one’s complement a negative number, you get the corresponding positive number. If you one’s complement a number twice, you get the original number.

Note: Two’s complement is the most common, the most important, and the most widely used representation of integers today.

Two’s complement If the sign is positive, no further action is needed; If the sign is negative, leave all the rightmost 0s and the first 1 unchanged. Complement the rest of the bits e.g. 0000000000101000 1111111111011000 變成負數

Two’s complement的另類看法 0000000000101000 1111111111011000 0000000000101000 1111111111010111 2. 216-0000000000101000 10000000000000000 -) 0000000000101000 1111111111011000 One’s complement +1

------------------------------------------------------- Table 3.7 Range of two’s complement integers # of Bits --------- 8 16 32 Range ------------------------------------------------------- -128 -32,768 -2,147,483,648 0 +127 0 +32,767 0 +2,147,483,647

Note: In two’s complement representation, the leftmost bit defines the sign of the number. If it is 0, the number is positive. If it is 1, the number is negative.

Example 12 Store +7 in an 8-bit memory location using two’s complement representation. Solution First change the number to binary 111. Add five 0s to make a total of N (8) bits, 00000111.The sign is positive, so no more action is needed. The result is: 00000111

Example 13 Store –40 in a 16-bit memory location using two’s complement representation. Solution First change the number to binary 101000. Add ten 0s to make a total of N (16) bits, 0000000000101000. The sign is negative, so leave the rightmost 0s up to the first 1 (including the 1) unchanged and complement the rest. The result is: 1111111111011000

------------------------------ Table 3.8 Example of storing two’s complement integers in two different computers Decimal ------------ +7 -7 +124 -124 +24,760 -24,760 8-bit allocation ------------ 00000111 11111001 01111100 10000100 overflow 16-bit allocation ------------------------------ 0000000000000111 1111111111111001 0000000001111100 1111111110000100 0110000010111000 1001111101001000

There is only one 0 in two’s complement: Note: There is only one 0 in two’s complement: In an 8-bit allocation: 0  00000000

Example 14 Interpret 11110110 in decimal if the number was stored as a two’s complement integer. Solution The leftmost bit is 1. The number is negative. Leave 10 at the right alone and complement the rest. The result is 00001010. The two’s complement number is 10. So the original number was –10.

Note: Two’s complement can be achieved by reversing all bits except the rightmost bits up to the first 1 (inclusive). If you two’s complement a positive number, you get the corresponding negative number. If you two’s complement a negative number, you get the corresponding positive number. If you two’s complement a number twice, you get the original number.

Contents of Memory ------------ Table 3.9 Summary of integer representation Contents of Memory ------------ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Unsigned ------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sign-and-Magnitude --------- +0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 One’s Complement --------- +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 Two’s Complement -------- +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1

3.5 EXCESS SYSTEM

Represent –25 in Excess_127 using an 8-bit allocation. Example 15 Represent –25 in Excess_127 using an 8-bit allocation. Solution First add 127 to get 102. This number in binary is 1100110. Add one bit to make it 8 bits in length. The representation is 01100110.

Example 16 Interpret 11111110 if the representation is Excess_127. Solution First change the number to decimal. It is 254. Then subtract 127 from the number. The result is decimal 127.

3.5 FLOATING-POINT REPRESENTATION

Table 3.10 Example of normalization Original Number ------------ +1010001.1101 -111.000011 +0.00000111001 -001110011 Original Number ------------ +1010001.1101 -111.000011 +0.00000111001 -0.001110011 Move ------------  6  2 6  3  Normalized ------------ +26 x 1.01000111001 -22 x 1.11000011 +2-6 x 1.11001 -2-3 x 1.110011

IEEE standards for floating-point representation Figure 3-8 IEEE standards for floating-point representation

Show the representation of the normalized number + 26 x 1.01000111001 Example 19 Show the representation of the normalized number + 26 x 1.01000111001 Solution The sign is positive. The Excess_127 representation of the exponent is 133. You add extra 0s on the right to make it 23 bits. The number in memory is stored as: 0 10000101 01000111001000000000000

Mantissa ------------------------------- Table 3.11 Example of floating-point representation Number ------------ -22 x 1.11000011 +2-6 x 1.11001 -2-3 x 1.110011 Sign ---- 1 Exponent ----------- 10000001 01111001 01111100 Mantissa ------------------------------- 11000011000000000000000 11001000000000000000000 11001100000000000000000

Interpret the following 32-bit floating-point number Example 20 Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000 Solution The sign is negative. The exponent is –3 (124 – 127). The number after normalization is -2-3 x 1.110011

3.6 HEXADECIMAL NOTATION

Chapter 4 Operations on Bits

OBJECTIVES After reading this chapter, the reader should be able to: Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits. Understand the applications of logical operations using masks. Understand the shift operations on numbers and how a number can be multiplied or divided by powers of two using shift operations.

Figure 4-1 Operations on bits

4.1 ARITHMETIC OPERATIONS

Number of 1s ------------ Table 4.1 Adding bits Number of 1s ------------ None One Two Three Result ------------ 1 Carry ------------ 1

Rule of Adding Integers in Two’s Complement Note: Rule of Adding Integers in Two’s Complement Add 2 bits and propagate the carry to the next column. If there is a final carry after the leftmost column addition, discard it.

簡要解釋為何two’s complement可以這樣做運算 假設是n bits 正數 + 正數 (和一般情況一樣) 負數(-x) + 負數(-y) -x在two’s complement表示值為 2n-x -y在two’s complement表示值為 2n-y 2n - x + 2n - y = 2n + (2n - (x+y)) Carry (進位) -(x+y)的two’s complement表示法

簡要解釋為何two’s complement可以這樣做運算 (續前頁) 正數 (x) + 負數 (-y) -y在two’s complement表示值為 2n-y 得 2n+x-y (1) x >= y x-y為正值; 2n為進位 (2) x <y 2n+x-y = 2n-(y-x)

Example 1 Add two numbers in two’s complement representation: (+17) + (+22)  (+39) Solution Carry 1 0 0 0 1 0 0 0 1 + 0 0 0 1 0 1 1 0 ---------------------------------- Result 0 0 1 0 0 1 1 1  39

Example 2 Add two numbers in two’s complement representation: (+24) + (-17)  (+7) Solution Carry 1 1 1 1 1 0 0 0 1 1 0 0 0 + 1 1 1 0 1 1 1 1 ---------------------------------- Result 0 0 0 0 0 1 1 1  +7

Example 3 Add two numbers in two’s complement representation: (-35) + (+20)  (-15) Solution Carry 1 1 1 1 1 0 1 1 1 0 1 + 0 0 0 1 0 1 0 0 ---------------------------------- Result 1 1 1 1 0 0 0 1  -15

Example 4 Add two numbers in two’s complement representation: (+127) + (+3)  (+130) Solution Carry 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 + 0 0 0 0 0 0 1 1 ---------------------------------- Result 1 0 0 0 0 0 1 0  -126 (Error) An overflow has occurred.

Range of numbers in two’s complement representation Note: Range of numbers in two’s complement representation - (2N-1) ---------- 0 ----------- +(2N-1 –1)

Two’s complement numbers visualization Figure 4-2 Two’s complement numbers visualization

Note: When you do arithmetic operations on numbers in a computer, remember that each number and the result should be in the range defined by the bit allocation.

Example 5 Subtract 62 from 101 in two’s complement: (+101) - (+62)  (+101) + (-62) Solution Carry 1 1 0 1 1 0 0 1 0 1 + 1 1 0 0 0 0 1 0 ---------------------------------- Result 0 0 1 0 0 1 1 1  39 The leftmost carry is discarded.

Example 6 Add two floats: 0 10000100 10110000000000000000000 0 10000010 01100000000000000000000 Solution The exponents are 5 and 3. The numbers are: +25 x 1.1011 and +23 x 1.011 Make the exponents the same. (+25 x 1.1011)+ (+25 x 0.01011)  +25 x 10.00001 After normalization +26 x 1.000001, which is stored as: 0 10000101 000001000000000000000000

4.2 LOGICAL OPERATIONS

Unary and binary operations Figure 4-3 Unary and binary operations

Figure 4-4 Logical operations

Figure 4-5 Truth tables

Figure 4-6 NOT operator

Example 7 Use the NOT operator on the bit pattern 10011000 Solution Target 1 0 0 1 1 0 0 0 NOT ------------------ Result 0 1 1 0 0 1 1 1

Figure 4-7 AND operator

Example 8 Use the AND operator on bit patterns 10011000 and 00110101. Solution Target 1 0 0 1 1 0 0 0 AND 0 0 1 1 0 1 0 1 ------------------ Result 0 0 0 1 0 0 0 0

Inherent rule of the AND operator Figure 4-8 Inherent rule of the AND operator

Figure 4-9 OR operator

Example 9 Use the OR operator on bit patterns 10011000 and 00110101 Solution Target 1 0 0 1 1 0 0 0 OR 0 0 1 1 0 1 0 1 ------------------ Result 1 0 1 1 1 1 0 1

Inherent rule of the OR operator Figure 4-10 Inherent rule of the OR operator

Figure 4-11 XOR operator

Example 10 Use the XOR operator on bit patterns 10011000 and 00110101. Solution Target 1 0 0 1 1 0 0 0 XOR 0 0 1 1 0 1 0 1 ------------------ Result 1 0 1 0 1 1 0 1

Inherent rule of the XOR operator Figure 4-12 Inherent rule of the XOR operator

More about XOR 一連串的bits做 XOR, 若奇數個1, 則結果為1;若偶數個1則結果為0 1 XOR

Figure 4-13 Mask

Example of unsetting specific bits Figure 4-14 Example of unsetting specific bits

Example 11 Use a mask to unset (clear) the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 00000111. Target 1 0 1 0 0 1 1 0 AND Mask 0 0 0 0 0 1 1 1 ------------------ Result 0 0 0 0 0 1 1 0

Solution on the next slide. Example 12 Imagine a power plant that pumps water to a city using eight pumps. The state of the pumps (on or off) can be represented by an 8-bit pattern. For example, the pattern 11000111 shows that pumps 1 to 3 (from the right), 7 and 8 are on while pumps 4, 5, and 6 are off. Now assume pump 7 shuts down. How can a mask show this situation? Solution on the next slide.

Solution Use the mask 10111111 to AND with the target pattern. The only 0 bit (bit 7) in the mask turns off the seventh bit in the target. Target 1 1 0 0 0 1 1 1 AND Mask 1 0 1 1 1 1 1 1 ------------------ Result 1 0 0 0 0 1 1 1

Example of setting specific bits Figure 4-15 Example of setting specific bits

Example 13 Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 11111000. Target 1 0 1 0 0 1 1 0 OR Mask 1 1 1 1 1 0 0 0 ------------------ Result 1 1 1 1 1 1 1 0

Example 14 Using the power plant example, how can you use a mask to to show that pump 6 is now turned on? Solution Use the mask 00100000. Target 1 0 0 0 0 1 1 1 OR Mask 0 0 1 0 0 0 0 0 ------------------ Result 1 0 1 0 0 1 1 1

Example of flipping specific bits Figure 4-16 Example of flipping specific bits

Example 15 Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution Target 1 0 1 0 0 1 1 0 XOR Mask 1 1 1 1 1 0 0 0 ------------------ Result 0 1 0 1 1 1 1 0

4.3 SHIFT OPERATIONS

Figure 4-17 Shift operations

Example 16 Show how you can divide or multiply a number by 2 using shift operations. Solution If a bit pattern represents an unsigned number, a right-shift operation divides the number by two. The pattern 00111011 represents 59. When you shift the number to the right, you get 00011101, which is 29. If you shift the original number to the left, you get 01110110, which is 118.

Example 17 Use a combination of logical and shift operations to find the value (0 or 1) of the fourth bit (from the right). Solution Use the mask 00001000 to AND with the target to keep the fourth bit and clear the rest of the bits. Continued on the next slide

Solution (continued) Target a b c d e f g h AND Mask 0 0 0 0 1 0 0 0 ------------------ Result 0 0 0 0 e 0 0 0 Shift the new pattern three times to the right 0000e000  00000e00  000000e0  0000000e Now it is easy to test the value of the new pattern as an unsigned integer. If the value is 1, the original bit was 1; otherwise the original bit was 0.

Computer Organization (計算機組織) Chapter 5 Computer Organization (計算機組織)

OBJECTIVES After reading this chapter, the reader should be able to: Distinguish between the three components of a computer hardware. List the functionality of each component. Understand memory addressing and calculate the number of bytes for a specified purpose. Distinguish between different types of memories. Understand how each input/output device works. Continued on the next slide

OBJECTIVES (continued) Understand the systems used to connect different components together. Understand the addressing system for input/output devices. Understand the program execution and machine cycles. Distinguish between programmed I/O, interrupt-driven I/O and direct memory access (DMA). Understand the two major architectures used to define the instruction sets of a computer: CISC and RISC.

Computer hardware (subsystems) Figure 5-1 Computer hardware (subsystems)

5.1 CENTRAL PROCESSING UNIT (CPU)

Figure 5-2 CPU

5.2 MAIN MEMORY

Table 5.1 Memory units kilobyte megabyte gigabyte terabyte petabyte exabyte Exact Number of bytes ------------------------ 210 bytes 220 bytes 230 bytes 240 bytes 250 bytes 260 bytes Approximation ------------ 103 bytes 106 bytes 109 bytes 1012 bytes 1015 bytes 1018 bytes

Figure 5-3 Main memory

Memory addresses are defined using unsigned binary integers. Note: Memory addresses are defined using unsigned binary integers.

Example 1 A computer has 32 MB (megabytes) of memory. How many bits are needed to address any single byte in memory? Solution The memory address space is 32 MB, or 225 (25 x 220). This means you need log2 225 or 25 bits, to address each byte.

Example 2 A computer has 128 MB of memory. Each word in this computer is 8 bytes. How many bits are needed to address any single word in memory? Solution The memory address space is 128 MB, which means 227. However, each word is 8 (23) bytes, which means that you have 224 words. This means you need log2 224 or 24 bits, to address each word.

Memory Types RAM (Random access memory): ROM (Read only memory) SRAM (Static RAM) (flip-flop gates) DRAM (Dynamic RAM) ROM (Read only memory) PROM (programmable) EPROM (erasable programmable) EEPROM (electronically erasable programmable)

A simple flip-flop circuit Set Reset

Setting the output of a flip-flop to 1

Setting the output of a flip-flop to 1 (continued)

Setting the output of a flip-flop to 1

Another way of constructing a flip-flop

Figure 5-4 Memory hierarchy

Figure 5-5 Cache

5.3 INPUT / OUTPUT

Physical layout of a magnetic disk Figure 5-6 Physical layout of a magnetic disk

Surface organization of a disk Figure 5-7 Surface organization of a disk

Mechanical configuration of a tape Figure 5-8 Mechanical configuration of a tape

Surface organization of a tape Figure 5-9 Surface organization of a tape

Creation and use of CD-ROM Figure 5-10 Creation and use of CD-ROM

3,688,400 bytes per second 4,915,200 bytes per second Table 5.2 CD-ROM speeds Speed ------------ 1x 2x 4x 6x 8x 12x 16x 24x 32x 40x Data Rate ------------------------ 153,600 bytes per second 307,200 bytes per second 614,400 bytes per second 921,600 bytes per second 1,228,800 bytes per second 1,843,200 bytes per second 2,457,600 bytes per second 3,688,400 bytes per second 4,915,200 bytes per second 6,144,000 bytes per second Approximation ------------ 150 KB/s 300 KB/s 600 KB/s 900 KB/s 1.2 MB/s 1.8 MB/s 2.4 MB/s 3.6 MB/s 4.8 MB/s 6 MB/s

Figure 5-11 CD-ROM format

Figure 5-12 Making a CD-R

Figure 5-13 Making a CD-RW

single-sided, single-layer single-sided, dual-layer Table 5.3 DVD capacities Feature --------------------------------- single-sided, single-layer single-sided, dual-layer double-sided, single-layer double-sided, dual-layer Capacity ------------ 4.7 GB 8.5 GB 9.4 GB 17 GB

5.4 SUBSYSTEM INTERCONNECTION

Connecting CPU and memory using three buses Figure 5-14 Connecting CPU and memory using three buses

Connecting I/O devices to the buses Figure 5-15 Connecting I/O devices to the buses

(Small Computer System Interface) Figure 5-16 SCSI controller (Small Computer System Interface) Daisy Chain

FireWire controller (IEEE 1394) Figure 5-17 FireWire controller (IEEE 1394)

(Universal Serial Bus) Figure 5-18 USB controller (Universal Serial Bus)

Isolated I/O addressing Figure 5-19 Isolated I/O addressing

Memory-mapped I/O addressing Figure 5-20 Memory-mapped I/O addressing

5.5 PROGRAM EXECUTION

Figure 5-21 Steps of a cycle

Contents of memory and register before execution Figure 5-22 Contents of memory and register before execution

registers after each cycle Figure 5-23.a Contents of memory and registers after each cycle

registers after each cycle Figure 5-23.b Contents of memory and registers after each cycle

registers after each cycle Figure 5-23.c Contents of memory and registers after each cycle

Contents of memory and registers after each cycle Figure 5-23.d Contents of memory and registers after each cycle

Figure 5-24 Programmed I/O

Figure 5-25 Interrupt-driven I/O

DMA connection to the general bus Figure 5-26 DMA connection to the general bus

Figure 5-27 DMA input/output

5.6 TWO DIFFERENT ARCHITECTURES

Two different architectures CISC (Complex Instruction Set Computer) Intel RISC (Reduced Instruction Set Computer) PowerPC

Chapter 6 Computer Networks

OBJECTIVES After reading this chapter, the reader should be able to: Understand the rationale for the existence of networks. Distinguish between the three types of networks: LANs, MANs, and WANs. Understand the OSI model and TCP/IP. List different connecting devices and the OSI layers in which each device operates. Understand client-server models.

6.1 NETWORKS, LARGE AND SMALL (LAN, MAN, WAN)

6.2 OSI MODEL

Note: The Open Systems Interconnection model is a theoretical model that shows how any two different systems can communicate with each other.

Figure 6-1 The OSI model

Flow of data in the OSI model Figure 6-2 Flow of data in the OSI model User  network Coding methods Synchronization points Entire message Packet (logical address) Frames (node  node) Bit stream  signal

6.3 CATEGORIES OF NETWORKS

Categories of networks Figure 6-3 Categories of networks

Figure 6-4 LANs

Figure 6-5 MAN

Figure 6-6 WAN

6.4 CONNECTING DEVICES

Figure 6-7 Connecting devices

Figure 6-8 Repeater

Repeaters operate at the first layer of the OSI model. Note: Repeaters operate at the first layer of the OSI model.

Figure 6-9 Bridge

Bridges operate at the first two layers of the OSI model. Note: Bridges operate at the first two layers of the OSI model.

Figure 6-10 Switch

Routers operate at the first three layers of the OSI model. Note: Routers operate at the first three layers of the OSI model.

Figure 6-11 Routers in an internet

Connecting devices and the OSI model Figure 6-12 Connecting devices and the OSI model Router 和 Gateway 現在不太區別了

6.5 THE INTERNET AND TCP/IP

Figure 6-13 TCP/IP and OSI model

IP addresses in dotted-decimal notation Figure 6-14 IP addresses in dotted-decimal notation

Figure 6-15 Client-server model

Figure 6-16 FTP

Figure 6-17 SMTP

Figure 6-18 Email address

Figure 6-19 TELNET

Figure 6-20 URL

Figure 6-21 Browser

Categories of Web documents Figure 6-22 Categories of Web documents

Chapter 7 Operating Systems 作業系統 (電腦的管家婆)

OBJECTIVES After reading this chapter, the reader should be able to: Define the purpose and functions of an operating system. Understand the components of an operating system. Understand the concept of virtual memory. Understand the concept of deadlock and starvation. List some of the characteristics of popular operating systems such as Windows 2000, UNIX, and Linux.

Figure 7-1 Computer System

7.1 DEFINITION

Note: An operating system is an interface between the hardware of a computer and the user (program or human) that facilitates the execution of the other programs and the access to hardware and software resources.

7.2 EVOLUTION: Batch systems Time-sharing systems Personal systems Parallel systems Distributed systems 7.2

7.3 COMPONENTS

Components of an operating system Figure 7-2 Components of an operating system

Figure 7-3 Monoprogramming

Figure 7-4 Multiprogramming

Categories of multiprogramming Figure 7-5 Categories of multiprogramming

Figure 7-6 Partitioning

Figure 7-7 Paging

Figure 7-8 Virtual memory

State diagram with the boundaries between a program, a job, and a process Figure 7-9

Figure 7-10 Job scheduler

Figure 7-11 Process scheduler

Queues for process management Figure 7-12 Queues for process management

Figure 7-13 Deadlock

Figure 7-14 Deadlock on a bridge

Note: Deadlock occurs when the operating system does not put resource restrictions on processes.

Figure 7-15.a Starvation

Figure 7-15.b Starvation

Figure 7-15.c Starvation

Figure 7-16 Dining philosophers

POPULAR OPERATING SYSTEMS: 7.4 POPULAR OPERATING SYSTEMS: Unix; Linux; Windows

Chapter 8 Algorithms

OBJECTIVES After reading this chapter, the reader should be able to: Understand the concept of an algorithm. Define and use the three constructs for developing algorithms: sequence, decision, and repetition. Understand and use three tools to represent algorithms: flowchart, pseudocode, and structure chart. Understand the concept of modularity and subalgorithms. List and comprehend common algorithms.

8.1 CONCEPT

Informal definition of an algorithm Figure 8-1 Informal definition of an algorithm used in a computer

Finding the largest integer Figure 8-2 Finding the largest integer among five integers

Defining actions in FindLargest algorithm Figure 8-3 Defining actions in FindLargest algorithm

Figure 8-4 FindLargest refined

Generalization of FindLargest Figure 8-5 Generalization of FindLargest

8.2 THREE CONSTRUCTS

Figure 8-6 Three constructs

8.3 ALGORITHM REPRESENTATION

Flowcharts for three constructs Figure 8-7 Flowcharts for three constructs

Pseudocode for three constructs Figure 8-8 Pseudocode for three constructs

Write an algorithm in pseudocode that finds the average of two numbers Example 1 Write an algorithm in pseudocode that finds the average of two numbers Solution See Algorithm 8.1 on the next slide.

Algorithm 8.1: Average of two AverageOfTwo Input: Two numbers Add the two numbers Divide the result by 2 Return the result by step 2 End

Write an algorithm to change a numeric grade to a pass/no pass grade. Example 2 Write an algorithm to change a numeric grade to a pass/no pass grade. Solution See Algorithm 8.2 on the next slide.

Algorithm 8.2: Pass/no pass Grade Pass/NoPassGrade Input: One number if (the number is greater than or equal to 70) then 1.1 Set the grade to “pass” else 1.2 Set the grade to “nopass” End if Return the grade End

Write an algorithm to change a numeric grade to a letter grade. Example 3 Write an algorithm to change a numeric grade to a letter grade. Solution See Algorithm 8.3 on the next slide.

Algorithm 8.3: Letter grade LetterGrade Input: One number 1. if (the number is between 90 and 100, inclusive) then 1.1 Set the grade to “A” End if 2. if (the number is between 80 and 89, inclusive) then 2.1 Set the grade to “B” Continues on the next slide

Letter grade (continued) Algorithm 8.3: Letter grade (continued) 3. if (the number is between 70 and 79, inclusive) then 3.1 Set the grade to “C” End if 4. if (the number is between 60 and 69, inclusive) then 4.1 Set the grade to “D” Continues on the next slide

Letter grade (continued) Algorithm 8.3: Letter grade (continued) 5. If (the number is less than 60) then 5.1 Set the grade to “F” End if 6. Return the grade End

See Algorithm 8.4 on the next slide. Example 4 Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers. Solution See Algorithm 8.4 on the next slide.

Algorithm 8.4: Find largest FindLargest Input: A list of positive integers Set Largest to 0 while (more integers) 2.1 if (the integer is greater than Largest) then 2.1.1 Set largest to the value of the integer End if End while Return Largest End

Write an algorithm to find the largest of 1000 numbers. Example 5 Write an algorithm to find the largest of 1000 numbers. Solution See Algorithm 8.5 on the next slide.

Algorithm 8.5: Find largest of 1000 numbers FindLargest Input: 1000 positive integers Set Largest to 0 Set Counter to 0 while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while Return Largest End

8.4 MORE FORMA DEFINITION Ordered set Unambiguous steps Effectiveness Termination

8.5 SUBALGORITHMS

Concept of a subalgorithm Figure 8-9 Concept of a subalgorithm

Algorithm 8.6: Find largest FindLargest Input: A list of positive integers Set Largest to 0 while (more integers) 2.1 FindLarger End while Return Largest End

Subalgorithm: Find larger FindLarger Input: Largest and current integer if (the integer is greater than Largest) then 1.1 Set Largest to the value of the integer End if End

8.6 BASIC ALGORITHMS

Figure 8-10 Summation

Figure 8-11 Product

Figure 8-12 Selection sort

Example of selection sort Figure 8-13: part I Example of selection sort

Example of selection sort Figure 8-13: part II Example of selection sort

Figure 8-14 Selection sort algorithm

Figure 8-15 Bubble sort

Figure 8-16: part I Example of bubble sort

Figure 8-16: part II Example of bubble sort

Figure 8-17 Insertion sort

Example of insertion sort Figure 8-18: part I Example of insertion sort

Example of insertion sort Figure 8-18: part II Example of insertion sort

Figure 8-19 Search concept

Example of a sequential sort Figure 8-20: Part I Example of a sequential sort

Example of a sequential sort Figure 8-20: Part II Example of a sequential sort

Example of a binary sort Figure 8-21

8.1 RECURSION

Iterative definition of factorial Figure 8-22 Iterative definition of factorial

Recursive definition of factorial Figure 8-23 Recursive definition of factorial

Tracing recursive solution to factorial problem Figure 8-24 Tracing recursive solution to factorial problem

Algorithm 8.7: Iterative factorial Factorial Input: A positive integer num Set FactN to 1 Set i to 1 while (i is less than or equal to num) 3.1 Set FactN to FactN x I 3.2 Increment i End while Return FactN End

Algorithm 8.8: Recursive factorial Factorial Input: A positive integer num if (num is equal to 0) then 1.1 return 1 else 1.2 return num x Factorial (num – 1) End if End

Programming Languages Chapter 9 Programming Languages

OBJECTIVES After reading this chapter, the reader should be able to: Have a vision of computer language evolution. Distinguish between machine, assembly, and high-level languages. Understand the process of creating and running a program. Distinguish between the different categories of languages: procedural, object-oriented, functional, declarative, and special. Become familiar with elements of the procedural language C.

9.1 EVOLUTION

Evolution of computer languages Figure 9-1 Evolution of computer languages

Program 9.1 Program in machine language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 00000000 00000100 0000000000000000 01011110 00001100 11000010 0000000000000010 11101111 00010110 0000000000000101 11101111 10011110 0000000000001011 11111000 10101101 11011111 0000000000010010 01100010 11011111 0000000000010101 11101111 00000010 11111011 0000000000010111 11110100 10101101 11011111 0000000000011110 00000011 10100010 11011111 0000000000100001 11101111 00000010 11111011 0000000000100100 01111110 11110100 10101101 11111000 10101110 11000101 0000000000101011 00000110 10100010 11111011 0000000000110001 11101111 00000010 11111011 0000000000110100 00000100 0000000000111101

Note: The only language understood by a computer is machine language.

Program 9.2 Program in symbolic language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Entry main, ^m<r2> subl2 #12,sp jsb C$MAIN_ARGS movab $CHAR_STRING_CON pushal -8(fp) pushal (r2) calls #2,read pushal -12(fp) pushal 3(r2) calls #2,read mull3 -8(fp),-12(fp),- pushal 6(r2) calls #2,print clrl r0 ret

Program 9.3 Program in C++ language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* This program reads two integer numbers from the keyboard and prints their product. */ #include <iostream.h> int main (void) { // Local Declarations int number1; int number2; int result; // Statements cin >> number1; cin >> number2; result = number1 * number2; cout << result; return 0; } // main

9.2 BUILDING A PROGRAM

Figure 9-2 Building a program

9.3 PROGRAM EXECUTION

Figure 9-3 Program execution

9.4 CATEGORIES OF LANGUAGES

Categories of languages Figure 9-4 Categories of languages

Function in a functional language Figure 9-5 Function in a functional language

Extracting the third element of a list Figure 9-6 Extracting the third element of a list

Table 9.1 Common tags Beginning Tag ---------------- <HTML> <HEAD> <BODY> <TITLE> <Hi> <B> <I> <U> <SUB> <SUP> <CENTER> <BR> <OL> <UL> <LI> <IMG> <A> Ending Tag ---------------- </HTML> </HEAD> </BODY> </TITLE> </Hi> </B> </I> </U> </SUB> </SUP> </CENTER> </OL> </UL> </LI> </A> Meaning ---------------------------- document document head document body document title different header levels boldface Italic underlined subscript superscript centered line break ordered list unordered list an item in the list an image an address (hyperlink)

Program 9.4 HTML Program <HTML> <HEAD> <TITLE> Sample Document </TITLE> </HEAD> <BODY> This is the picture of a book: <IMG SRC="Pictures/book1.gif" ALIGN=MIDDLE> </BODY> </HTML>

9.5 A PROCEDURAL LANGUAGE: C

Figure 9-7 Variables

Table 9.2 Arithmetic operators ---------------- + - * / % ---------- ++ -- Definition ---------------- Addition Subtraction Multiplication Division (quotient) Division (remainder) ----------------------- Increment Decrement Example ---------------------- 3 + 5 2 - 4 Num * 5 Sum / Count Count % 4 ----------------------- Count ++ Count --

Table 9.3 Relational operators ---------------- < <= > >= == != Definition ---------------- Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Example ---------------------- Num1 < 5 Num1 <= 5 Num2 > 3 Num2 >= 3 Num1 == Num2 Num1 != Num2

Table 9.4 Logical operators ---------------- ! && || Definition ---------------- NOT AND OR Example ---------------------- ! ( Num1 < Num2 ) (Num1 < 5 ) && (Num2 > 10 ) (Num1 < 5 ) || (Num2 > 10 )

Table 9.5 Assignment operators ---------------- == += -= *= /= %= Example ---------------- Num = 5 Num += 5 Num -= 5 Num *= 5 Num /= 5 Num %= 5 Meaning ---------------------- Store 5 in Num Num = Num + 5 Num = Num - 5 Num = Num * 5 Num = Num / 5 Num = Num % 5

Figure 9-8 Statements

Side effect of a function Figure 9-9 Side effect of a function

Figure 9-10 Function declaration

Figure 9-11 if-else statement

Figure 9-12 switch statement

Figure 9-13 while loop

Figure 9-14 for loop

do-while loop Figure 9-15