Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes...

75
Computer Codes, Programming, and Operating Systems Number Systems Character Codes Machine Codes Programming Operating Systems

Transcript of Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes...

Page 1: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Chapter 2Computer Codes, Programming, and Operating Systems

Number SystemsCharacter CodesMachine CodesProgrammingOperating Systems

Page 2: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits: Binary Digits

A bit (binary digit) is the smallest unit of

information can have two values - 1 and 0. Can be stably implemented with

many physical devices

Binary digits, or bits, can represent data (numbers, character codes, …) or instructions (machine codes).

On Off

Page 3: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits as Numbers

Binary number system - a system that denotes all numbers and combinations of two digits.

The binary number system uses two digits to represent the numbers: 0 and 1.

N-bit (fixed length) binary number can represent the unsigned numbers 0 ~ 2N-1

Page 4: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Numbers (in fixed number of bits)

Unsigned vs. SignedInteger vs. Floating Point numbers

Page 5: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Number Systems: Unsigned Integer

Decimal: Base 10 0, 1, 2, 3, …, 9, 10, 11, … Use 10 digits (0~9) to represent numbers Positional Notation: prepend more digits to left

positions if greater than or equal to 10 ( 逢十進一 )Each position take a different weights (units, tens,

hundreds, …): 123 = 1* 100 + 2 * 10 + 3Radix points: for representing non-whole parts

• Decimal point, binary point, hexadecimal point• 0.25 = 2 * 10^ -1 + 5 * 10^ -2• = 0 * 2^ -1 + 1 * 2^ -2 = 0.01B

Page 6: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Number Systems: Unsigned Integer

Binary: Base 2 Use 2 digits, 0 ~ 1, to represent all numbers 0, 1, 10, 11, 100, 101, 110, 111, … Positional Notation: prepend more digits to left positions

Each position take a different weights (units, tens, hundreds, …) 110 = 1 * 2^2 + 1 * 2^1 + 0 * 2^0 = 1 * 4 + 1 * 2 + 0 * 1

00, 012, 10B, 11B Octal: Base 8

0, 1, 2, …, 7, 108, 11O, …, \027 Hexadecimal: Base 16

0, 1, 2, …, 9, a16, b16, …, f16, 10H, 11H, …, 0FFH, … BCD: Binary Coded Decimal (4-bit for one decimal number)

10010111 = 9710 (decimal) – packed format (two numbers in one byte) 97: 00001001 00000111 – unpacked format (one number in one byte)

n-digit base-r number:

N = i=0,n-1 di x ri

Page 7: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

FIGURE 2-3 (a) A seven-segment display is used to display (b) the BCD numbers 0 through 9; (c) a BCD to seven-segment decoder converts the BCD number into a code that activates the proper segments for that digit.

8

4

2

1

Page 8: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Number Systems: Unsigned Integer

Decimal Octal Hex. Binary BCD 0 0 0 0000 0000 0000 1 1 1 0001 0000 0001

2 2 2 0010 0000 0010 3 3 3 0011 0000 0011 4 4 4 0100 0000 0100 5 5 5 0101 0000 0101 6 6 6 0110 0000 0110 7 7 7 0111 0000 0111 8 10 8 1000 0000 1000 9 11 9 1001 0000 100110 12 a 1010 0001 0000

11 13 b 1011 0001 000112 14 c 1100 0001 001013 15 d 1101 0001 001114 16 e 1110 0001 010015 17 f 1111 0001 0101

binary-to-octal:rule of threes

Page 9: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Base Conversion: Bin/2Oct/8Hex/16

Binary to Octal (bi oj) The rule of threes: grouping binary digits three at a time

Every octal number can be represented by 3 bits (binary digits)

Binary to Hexadecimal (bi hj) The rule of fours: grouping binary digits four at a time

Every hexadecimal number can be represented by 4 bitsBCH: Binary Coded Hexadecimal Number

• 1101 0111 = D7H (Hexadecimal)

Octal to Hexadecimal Oct Bin Hex

Page 10: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Base Conversion: 2/8/1610

Binary/Oct/Hex to Decimal (bi D)

D = i=0,n-1 bi x 2i

D = i=0,n-1 oi x 8i

D = i=0,n-1 hi x 16i

bi oi or hi D, for large numbers

Page 11: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Base Conversion: 10 2/16

Decimal to Binary –the integer part D = bmx2m + bm-1x2m-1 + … + b0 bi = 0? 1? Repeated subtraction: subtract to see if 2m exists

D’ = i=0,m-1 bi x 2i = D - 2m

• bm=1, if positive or zero; bm=0, if not

D D’ & m m’ (m’: max exp. s.t. bm’=1) Long division (repeated division)

i=0, D’ = D mod 2 … bi & D D’, i++

Alternative method: Dec Hex (shorter) Bin (longer)

Page 12: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Base Conversion: 10 2/16

Decimal to Binary – the fractional part Repeated (Long) multiplication

Repeatedly multiply the fractional part by 2 and take the whole part as the next bit

Example:0.25 = A x 2-1 + B x 2-2 + C x 2-3 + …A=??, B=??, C=??

Solution:0.25 x 2 = 0.5 = A + (B x 2-1 + C x 2-2 + … ) => A=00.5 x 2 = 1.0 = B + (C x 2-1 + D x 2-2 + … ) => B =1

• & C = D = … = 0• 0.25 = 0.01 000000…. B

Page 13: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Representations ofNegative Numbers

Biased (shifted, offset, Excess-N/XS-N)Sign magnitude notation1’s complement2’s complement

Page 14: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Biased Numbers

decimal Binary/XS-4 biased0 000 -41 001 -32 010 -23 011 -14 100 05 101 16 110 27 111 3

as unsigned integers

as signed integers

Page 15: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Sign Magnitude Notation

sign magnitude-3 1 11-2 1 10-1 1 01-0 1 000 0 001 0 012 0 103 0 11

Page 16: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Complement for Base-R Numbers

For representing negative numbers Use complement of N to represent -N

For simplifying subtraction operationsComplement of complement restores to original

Infer magnitude of a negative number 9’s in all n-digit

n-digit base-r number (0~rn-1)carry for base-r number

9’s complement

10’s complement

carry

Page 17: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Complement for Base-R Numbers

Base-r complements: two systems per base r r’s complement & (r-1)’s complement for ‘N’

r’s: rn – N (if N not zero), or 0 (if N=zero; truncated to n-bit)(r-1)’s: = (rn – 1) – N (for integer, i.e., no fractional part, m=0) = rn – r -m – N (n: #integer digits, m: #fractional digits)

Binary (base r=2): 2’s & 1’s Decimal (base r=10): 10’s & 9’s

Complement of complement restores to original N => N’ = rn – r-m –N => (N’)’ = rn–r-m – (rn–r-m – N) = N Infer magnitude of a negative number

Page 18: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Complement for Base-R Numbers

Binary (r=2): 2’s & 1’s 2’s: 2n – N (= 1’s + 1) [2n =‘1 00…0’ (carry + n-zeros)] 1’s: 2n – 1 – N (for integer numbers) [2n – 1=‘11…1’]

= Bitwise NOT/Complement over all digits (0=>1, 1=>0)

Example: Binary numbers 2’s: 1011 [11d]0100+1=0101 [5d = 16d –11d] 1’s: 1011 [11d]0100 [4d = 15d –11d]

Example: Decimal numbers: 10’s & 9’s 10’s: 123 877 [=1,000-123] = (9’s + 1) 9’s: 123 876 [= 999-123]; [1+8=9, 2+7=9, 3+6=9]

1’s in all

digits

Page 19: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

1’s Complement Numbers (base 2)

2n 23 22 21 20 r =2

0 1 1 1 1 = 2n-1

- n3 n2 n1 n0 = N

n3 n2 n1 n0 = N’

(2n-1)-N

All 1’sNo carry

Page 20: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

2’s Complement Numbers (base 2)

2n 23 22 21 20 r =2

1 0 0 0 0 = 2n

- n3 n2 n1 n0 = N

= N’’

2n -N

All 0’sWith carry

Page 21: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

1’s Complement Numbers

-3 100 011 -2 101 010 -1 110 001 -0 111 000

0 000 111-01 001 110-12 010 101-2

3 011 100-3Bitwise complement as the negative counterpart of a

positive number [NegNum=BitwiseComp(PosNum)]Two forms of ‘0’ [+0 & -0]

2n 22 21 20 r =2

0 1 1 1 2n -1

- n2 n1 n0 = N

= N’

Page 22: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

2’s Complement Numbers -4 100 011100

-3 101 010011-2 110 001010

-1 111 000001 0 000 111000 1 001 110111-1

2 010 101110-23 011 100101-3

NegativeNumber = 1’s(PositiveNumber)+1Unique representation for ‘0’MSB(NegNum)=‘1’ (sign bit)Rotational(+12):01…3-4-3…-10

Page 23: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

2’s Complement Numbers

Sign-extension: Using larger word size (e.g., 16-bit instead of 8-

bit) will extend sign bit to the left. -6 : 1’s(0000 0110) + 1 = 1111 1001 + 1

= 1111 1010

-6 : 1’s(0000 0000 0000 0110) + 1 = 1111 1111 1111 1001 + 1= 1111 1111 1111 1010

Page 24: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

FIGURE 2-2 Comparing (a) 8-bit and (b) 16-bit signed binary numbers. (From J. Uffenbeck, Digital Electronics: A Modern Approach, Prentice Hall, Englewood Cliffs, NJ, 1994.)

Page 25: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Subtraction withComplement Numbers

Subtraction with NOT&ADD instructions A-B => A + (2n –B) – 2n

= A + 2’s(B) – 2n = [- 2’s (A+2’s(B))]= A + 1’s(B) + 1 – 2n = [- 1’s(A+1’s(B))]= A + NOT(B) + 1 – 2n

SUB = ADD Complement, andhandling ‘1’ (if any) and (invisible) part (2^n)

Decimal example: 234 –123 = 234 + (1000–123) –1000 = 234 + (876

+ 1) -1000

Page 26: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Subtraction with1’s Complement Addition

X=M-N S=M+N’ (difference?? How to adjust??) N’=1s(N) i.e., S=M+(rn-1-N)= rn-1-(-X)=1s(-X)

carry end around: (result in 1’s complement form) M>N => X>0 => S = rn- 1 + X => C=1 and S + C (i.e., ‘1’) = rn + X (= “X with carry”)

• (S = pos + ‘111…1’)X Positive M+N’ results in C=1C=1 => add “1” (end-carry) to LSB of S, (& ignore rn) to get X

M<=N => X<=0 => S = rn- 1 - (-X) = rn- 1 - |X| => C=0• (S = ‘111…1’ - pos)

X Negative C=0C=0 => |X|= (S)’ = (M+N’)’ & X = -S’ = - (M+N’)’

Page 27: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Subtraction with 1’s complement

X=M-NX=M-N+

S=M+N’S=M+N’

MM NN

rn -1+X C

S=rS=rnn-1+X-1+XS=rS=rnn-1-|-X|-1-|-X|

1s(|X|)1s(|X|)

1 -1+X 1

rn -1-|X| C

0 |X|’ 0

|X||X|

+ + CC (=1) (=1)

XX

+add end carryadd end carrycomplementcomplement

(M>N)(M>N)(M<=N)(M<=N)

1s(N)=N’1s(N)=N’

Page 28: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Subtraction with2’s Complement Addition

X=M-NS=M+N’’ (difference?? How to adjust??) N’’=2s(N), i.e., S=M+(rn-N)= rn-(-X) = 2s(-X)

carry dropped: (result in 2’s complement form) M>=N => S= rn +X (= “X with carry”) => C=1

X Positive => S: C=1C=1 => discard carry, and ignore rn to get X

M<N => S= rn- (-X) = rn- |X| (= |X|’’) => C=0X Negative => S: C=0C=0 => |X| = (S)’’ = (M+N’’)’’ & X= -S’’ = -(M+N’’)’’

Page 29: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Subtraction with 2’s complement

X=M-NX=M-N+

S=M+N’’S=M+N’’

MM NN

2s(N)=N’’2s(N)=N’’

rn +X C

S = rS = rnn+X+XS = rS = rnn-|-X|-|-X|

2s(|X|)2s(|X|)

1 +X 1

rn -|X| C

0 |X|’’ 0

|X||X|

+ 0+ 0

XX

=ignore carryignore carrycomplementcomplement

(M>=N)(M>=N)(M<N)(M<N)

11

+

11

Page 30: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Overflow/Underflow in Addition of 2’s Complement Numbers

Range of n-bit 2’s number: - 2(n-1) ~ 0 ~ +2(n-1)-1

Addition: result is always correct when two operands have different

signs overflow when two positive operands are added and a

negative result is generated (falling in the negative range)64+96=160 = -96 in 8-bit 2’s

underflow when two negative operands are added and a positive result is generated

Page 31: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

1’s vs. 2’s Complement Numbers

1’s: Easy to implement Subtraction: May requires two arithmetic

additions (+1’s & +end-carry) Two forms of ‘0’ [+0 & -0]

2’s: Less easier to implement Subtraction: Requires only one addition Unique form of ‘0’

Page 32: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Figure 1–14  The unsigned and signed (2’s) bytes illustrating the weights of each binary-bit position.

Page 33: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Floating Point Representation

More than one way to represent a FP number: = 12345 * 10-2

= 1234.5 * 10-1

= 123.45= 12.345 * 101

= 1.2345 * 102 (* Some standards use this as the normalized form)

= 0.12345 * 103 — normalized form= 0.012345 * 104 = …

21.75 = 10101.11 = 0.1010111 * 25

= 1.010111 * 24 (IEEE floating standard)

Page 34: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Floating Point Representation

FP Number Representation: (within limited word size) Sign: plus/minus Magnitude: Mantissa x Base ** Exponent

Mantissa: (Coefficient)• Determine the precision (step size) between two consecutive

numbers

• Normalized s.t. floating point is located at the immediate left of the leftmost ‘1’ (or non-zero digit, if not base-2)

– Leftmost ‘1’ can be physically ignored (always =1), increasing one bit thus increasing precision

Exponent: (Power)• Determines the range of representation

Base: normally 2 (but, 10 and 16 are also used)

Page 35: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

PDP-11, VAX-11Floating Point Representation [32-bit]

precision 1/224 (~ 1/107)range 2127 (~ 1038)

0 10000101 01011100…….0

8 bits 23 bits

mantissasign exponent

Page 36: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

IEEE 32/64-bits Standards for Representing Scientific Notations

我們常用 32 bits(C稱為 float) 或 64 bits(C稱為 double)來存放浮點數 , 其標準由 IEEE制定。它表達的是這種形式的科學符號 :+0.01E10  表示 (1+1/4)*22。老師你寫錯了吧 , 應該是 (1/4)*22 吧 ? 這是因為二進位系統中只有 0 和 1兩個數字 , 因此正規化後的科學符號表示法的整數部份一定是 1, 換句話說任何數字都長這個樣子 , ±1.mmmmEee, 既然整數一定是 1, 那就不要浪費了 , 以便可以表達更大範圍的數字。上述例子的 ±稱為 (Sign bit),  ee部份稱為 Exponential(指數 ), mmmm稱為Mantissa(尾數 ), 以下的 SEM就是這幾個字的縮寫。  

指數部份 (Exp.) 小數部份 (Man.) 所代表的值

0 +-0 0

0 非 0 +-2-126*0.M

1 ~ 254 任何值 +-2E-127*1.M

255 +-0 +-infinite

255 非 0 特殊狀況 (NaN)

IEEE 64: SEM其中 S 占 1bit 為正負號, E 占 11bits 為指數部份, M 占 52bits 為尾數部份。 其中指數部份採用 excess-1023, 表示範圍超過 10 + -300 次方

IEEE 32: SEM其中 S 占 1bit 為正負號, E 占 8bits 為指數部份, M 占 23bits 為尾數部份。其中指數部份採用 excess-127 表示法,即範圍為 -127~128 ,但 0 和 255 有特殊意義,所以實際範圍為 -126 ~ +127.

Page 37: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits as Character Codes

ASCII - American Standard Code for Information Interchange - most widely used code, represents each character as a unique 8-bit code.

Page 38: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Code & Character Sets

ASCII ( American Standard Code for Information Interchange ) — 7 bit (0x00~0x7F)

0 1 2 3 4 5 6 7 8 9 A B C D E F 0 | 1 | 2 | ! “ # $ % & ` ( ) * + - , . / 3 | 0 1 2 3 4 5 6 7 8 9 4 | @A B C D E F G H I J K L M N O 5 | P Q R S T U V WX Y Z 6 | a b c d e f g h I j k l m n o 7 | p q r s t u v w x y z

Page 39: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

7-bit ASCII Codes0x00-0x1F,0x7F: control characters (non-printable)

0x20-0x7E: graphic characters (printable)

Page 40: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

0 1 2 3 4 5 6 7

0 NUL DLE SP 0 @ P ` p

1 SOH DC1 ! 1 A Q a q

2 STX DC2 “ 2 B R b r

3 ETX DC3 # 3 C S c s

4 EOT DC4 $ 4 D T d t

5 ENQ NAK % 5 E U e u

6 ACK SYN & 6 F V f v

7 BEL ETB ‘ 7 G W g w

8 BS CAN ( 8 H X h x

9 TAB EM ) 9 I Y i y

A LF SUB * : J Z j z

B VT ESC + ; K [ k {

C FF FS , < L \ l |

D CR GS - = M ] m }

E SO RS . > N ^ n ~

F SI US / ? O _ o DEL

7-bit A

SC

II Co

des

Control-A~Z =

SOH~SUB

Page 41: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

7-bit ASCII codes

Native code set for most computer systems and programming languages

128 codes (0x00~0x7F) Control codes:

2 columns + DEL Graphical characters:

SP + punctuations + [0-9A-Za-z]8-th bit (most significant bit): ‘0’

8-bit ASCII: ISO-8859-1 (including 80h~FFh)aka, Latin-1: extension for Latin/European characters

Page 42: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

7-bit ASCII Codes

0x00-0x1f: control characters 0x20-0x2f: space and symbols 0x20: SPACE 0x30-0x39: digits (0-9) 0x3a-0x40: symbols

0x20-0x40: symbols & digits 0x41-0x5a: upper case letters ([A-Z]) 0x5b-0x60: symbols 0x61-0x7a: lower case letters ([a-z]) 0x7b-0x7e: symbols 0x7f (127): DEL

Page 43: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

ExtendedASCII Codes for PC

0~127: ASCII (7-bit)

128~255: (ext) 80h~FFh

Page 44: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Many Windows-based applications use the Unicode system to store alphanumeric data. stores each character as 16-bit data Java programming language use 16-bit char data type: ‘\u4fe5'

Codes 0000H–00FFH (the first 256 codes) are the same as standard ASCII code (prefixed by the 00H byte).

Remaining codes, 0100H–FFFFH, store all special characters from many character sets. Allows software for Windows to be used in many countries

around the world. For complete information on Unicode, visit:

http://www.unicode.org

Unicode

Page 45: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Chinese Characters

Big-5 2 byte ( 16 bit ) 5401(frequent)+7652(rare)-2(duplicated) =13051 字

CNS11643 3 byte ( 24 bit ) 48222 字

Page 46: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Error Detection

ASCII – 8th-bit =0

- Use 8th bit for error detection

Parity:

- even, odd: number of 1’s is even/odd

- 1 (3,5,7,…)-bit error detection

- no error correction capability

- mark, space: always ‘1’/‘0’

Stop Bit

Start Bit D0 D1 D2 D3 D4 D5 D6 D7

Page 47: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

FIGURE 2-1 A parity generator circuit is used to add a parity bit to a 7-bit ASCII character before it is transmitted. The parity checker on the receiving end indicates an error if the received parity does not agree with the transmitted parity. (From J. Uffenbeck, Digital Electronics: A Modern Approach, Prentice Hall, Englewood Cliffs, NJ, 1994.)

Page 48: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits as Instructions

The computer stores programs as collections of bits.For instance, 01101010 might instruct the computer

to add two numbers. (OPeration code)

Other bits in instructions might include information on where to find numbers stored in memory or where to store them. (operands)

Page 49: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

FIGURE 2-4 80x86 assembly language program written to input two numbers, compute their sum, and output the result. Shown are the instruction object codes, instruction mnemonics (operation codes and operands), and program comments.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

AL := ioPort[27H]BL := ALAL := ioPort[27H]AL : = AL + BLioPort[30H] := ALHALT

Page 50: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits as Data: Word Sizes

Word Size: #bits of information processing units

Bit (b): smallest unit (one binary digit, 0/1)

Nibble: a group of 4 bits

Byte (B): a group of 8 bits (Octet)

Word (W): 16-bit

Double Word (DW): 32-bit

Quad-Word: 64-bit

Page 51: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits as Addresses: Memory Capacity

Memory Address (in binary or hex. form):- 0000 1111 0000 1111 = 0F0Fh

File size or memory capacity: Number of information processing units

K/KB: (kilobyte); about 1,000 bytes of information - technically 1024 (=2**10) bytes equals 1K of storage.

Page 52: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Bits as Addresses: Memory Capacity

KB: (kilobyte); about 1,000 bytes of information (=2**10)

MB: (megabyte); about 1 million bytes of information (=2**20)

GB: (gigabyte); about 1 billion bytes of information (=2**30)

TB: (tetrabyte); about 1 million megabytes of information (=2**40)

Page 53: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Prefixes for multiples of bits (b) or bytes (B)Decimal Binary

Value Metric Value JEDEC IEC

1000 ^ 1 k kilo 1024 ^ 1 K kilo Ki kibi

1000 ^ 2 M mega 1024 ^ 2 M mega Mi mebi

1000 ^ 3 G giga 1024 ^ 3 G giga Gi gibi

1000 ^ 4 T tera 1024 ^ 4     Ti tebi

1000 ^ 5 P peta 1024 ^ 5     Pi pebi

1000 ^ 6 E exa 1024 ^ 6     Ei exbi

1000 ^ 7 Z zetta 1024 ^ 7     Zi zebi

1000 ^ 8 Y yotta 1024 ^ 8     Yi yobi

* from: http://en.wikipedia.org/wiki/Binary_prefix

Basic Memory Specs: CapacityBasic Memory Specs: Capacity

Page 54: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Computer Programming

Machine LanguageAssembly LanguageEdit-Assembly-Test-DebugHigh-Level Languages:

Interpreters vs. Compilers

Page 55: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Programming

Machine language/object codes: [Fig 2.4] Binary sequences of bits, representing operations and

operands [E4 27 = IN AL, 27H] Instruction set (IS): list of operations supported by a

processor IS’s are machine/processor dependent

ISA: op-code + operand Op-code: actions [move, math, logical, jump/call] Operands: objects to act on Addressing modes: the ways to specify the physical

addresses of the operands

Page 56: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

KBD_PORT EQU 27H

PRT_PORT EQU 30H

IN AL, KBD_PORT

OUT PRT_PORT, AL

CALL PRINTF

FIGURE 2-4 80x86 assembly language program written to input two numbers, compute their sum, and output the result. Shown are the instruction object codes, instruction mnemonics (operation codes and operands), and program comments.

Symbolic names

(Symbols)Symbolic

names (Symbols)

Page 57: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Programming

Assembly: Command mnemonics: MOV AL, BL

High level languages [Fig. 2-5] Basic, FORTRAN, Pascal, C, C++, Java Machine independent

Interpreter vs. Compiler Compile HLL into assembly codes for execution Compiling at run time (interpreter) or pre-compilation (compiler)

Program Development Edit, assembly, test and debug DEBUG: an MSDOS utility

Page 58: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

FIGURE 2-5 C version of the input and add program.

Page 59: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

MicroprocessorMicroprocessorProgram DevelopmentProgram Development

EditorEditor AssemblerAssembler LinkerLinker

SymbolConverter

SymbolConverter ICEICE

TargetTarget

Program

.ASM .OBJ.HEX

.SYM

.SDT

(MASM,X8051)(Link)

(CVTSYM)

Debugger& Hardware Simulator

Page 60: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

MicroprocessorMicroprocessorProgram DevelopmentProgram Development

Editor: editing source program Assembler: convert source program into object codes Dis-assember: convert object codes back into assembly Linker: link (combine) object codes with library codes (object codes

modules written by other persons or from previous works) Loader: load linked objects into memory for execution

Many “linkers” also include the loader functions Symbol Table: a mapping table that store <name, value> pairs

Name: symbolic name for constant, address, port number Value: binary values for the corresponding name

Symbolic Debugger: Use dis-assembler to unassemble object codes into assembly codes Replace binary values into symbolic names by looking up the symbol table Easier to understand the semantics of the values from their symbolic names

Page 61: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

MicroprocessorMicroprocessorProgram DevelopmentProgram Development

ICE (In-Circuit Emulator) A hardware box capable of accepting executable object

codes (from a PC or development system) and emulating each instruction (by issuing appropriate CPU signals and emulating instruction actions)

With a cable+socket that can replace the real CPU socketSend the emulated signals to the target board

Capable of registering status & changes of the registers and memory contents

Easier to trace/debug program step-by-step (instead of full-speed run)

Page 62: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Computer Operating Systems

What is an OSMS-DOSDOS and BIOS Function Calls

Page 63: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

What is an Operating System

Turn-key systems Fixed functions with limited user intervention No complicated programs for handling user

requests A simple control program will be sufficient

Reside in memory all the time is OKJump into the “main” program at a fixed memory

location designated for the processor when power on E.g., simple telephone switching box, simple cell

phone, ATM, simple embedded systems

Page 64: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

What is an Operating System

OS: A control software that start and manage all services (applications) & a friendly program for mediating user and hardware Booting: loading required OS modules into memory Command interpretation (“shell”): accepting flexible

command combinations by users to accomplish complicated tasks

Job scheduling: putting multiple jobs to run simultaneously (actually, in time-sharing fashion)

Resource management: allocating I/O, memory, file system, processor time to each job

Page 65: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Known Operating Systems

NOS, VMS: for early main framesUnix (& Linux): for mini-computers & workstationsPC:

CP/M (“Control Program for Microcomputer”): by Gary Kildall, 1978, 1st C OS

8080/Z80-based, 64K memory, floppy disk drive Apple DOS: for APPLE–I, -II, -IIe Microsoft DOS: MS-Basic + 86-DOS (a CP/M workalike) PC-DOS: same as MS-DOS, for IBM PC Better GUI: OS/2, Mac, Windows 3.1, 95, 98, NT, 2K, XP,

Vista, Windows 7…

Page 66: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Types of Operating Systems

Types Single user single task Single user multi-task Multi-user multi-task

Page 67: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Running an Operating System

Booting: loading required OS modules into memory Complex OS (even the part to initialize a system) is too

large to be put into memory with other applications Only a few kernel functions need be resident in memory

all the timeSolution:

divide OS into modules & load required parts on demand (known as booting or

bootstrapping)A small program at the boot ROM, installed at the Reset or Start-

Up address (usually, address 0H), to start loading and then running other modules

Page 68: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Running an Operating System Dividing OS into modules & Running the pieces

BIOS ROM: bootstrapping firmware, init & load boot loader in MBR Boot device: Floppy, Hard-Disk, CD-ROM, (USB) Flash memory

Hard-Disk: (Physical)• Cylinders + Head + Sectors (C/H/S)• Sector = 512 Bytes (normally …)• Hard-Disk-like devices (CD-ROM, Flash) are logically divided similarly

Hard-Disk: (Logical) = MBR x 1 +Partition x N• MBR: partition table + boot loader + MBR signature (’0x55aa’)

– 512 = (4*16=64) + 440~446 bytes (DOS partition table/label) + 2– First sector in boot disk (not part of a partition, in front of 1st partition)

• Partitions: groups of physical (or logical) cylinders– Active partition: where volume boot record resides– Root partition: where main OS modules reside

Transfer of control: BIOS => MBR/boot loader (@boot disk) => volume boot record (@active

partition) (or private codes of boot managers) => OS (MS/DOS/Win/Linus) Kernel (@ root partition)

Page 69: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Running an Operating SystemOS Modules:

Each module can beAn executable file (.com/.exe)Normally, An archived executables (in compressed

format or not)• Kernel image (e.g., vmunix.gz)

Executable file = OBJ Codes + Runtime environment + User options (+Misc info.)Main (int argc, char *argv[], char **envp)

• { int a; a=a+1;} Main.exe

Page 70: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Running an Operating System

Bootstrapping procedure: load small modules to initialize the system and

build the basic capability to run larger complicated programs (including main OS modules)

then run (jump to) larger modules to complete system initialization (possibly repeated in several steps)

Then run “user commands” and “applications” in a “shell” environment

Page 71: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Booting Sequence [for MS-DOS]

Read MBR from C: for partition table, and “active partition” (MBR=partition table + boot loader codes)

Read DOS boot record for disk geometry parametersLoad and transfer control to bootstrap loaderBootstrap loader loads IO.SYS (extension to BIOS) IO.SYS start SYSINIT:

Initialization & load MS-DOS (including system services for application programs, like read/write devices/files)

Check CONFIG.SYS & load (device) drivers Load & Transfer control to COMMAND.COM (command

interpreter)Execute AUTOEXEC.BAT by COMMAND.COM

Page 72: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Memory Map (for MS-DOS)

Memory Map: the way memory is organized PC: 8088 based, 20-bit address (1M=640+384K) 640K User RAM

DOS, COMMAND.COM, Environment, User Memory 384K Special Memory (Buffer + ROM)

Video display buffer, HDC, BIOS

640 K Barrier Real Mode: maintain compatibility for this configuration,

even with advanced processors Broken now by more advanced OS

Page 73: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

FIGURE 2-8 Memory map for an 80x86 computer running MS-DOS. One MB of memory is required–640K of user memory and 384K of special memory.

John UffenbeckThe 80x86 Family: Design, Programming, and Interfacing, 3e

Copyright ©2002 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458

All rights reserved.

Page 74: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

BIOS services and DOS Functions

Using software interrupts to modularize & standardize common services (mostly I/O)

BIOS svc: basic I/O routines (mostly in ROM)INT interrupt_numberUnified calling interface:

AL = function parameters (or AH, AX, …) INT n

DOS function calls: higher-level services

Page 75: Chapter 2 Computer Codes, Programming, and Operating Systems zNumber Systems zCharacter Codes zMachine Codes zProgramming zOperating Systems.

Structure of MS-DOS OS

System Hardware

BIOS (ROM+IO.SYS)

MS-DOS Kernel (MSDOS.SYS)

Command Processor (COMMAND.COM

)