Binary System and Information Representation in Computers z
Binary System and Information Representation in Computers z. Number Systems z. Character Codes z. Machine Codes z. Programming z. Operating Systems
Bits: Binary Digits A bit (binary digit) y is the smallest unit of information y can have two values - 1 and 0. y Can be stably implemented with many physical devices Binary digits, or bits, can represent data (numbers, character codes, …) or instructions (machine codes). On Off
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 ~ 2 N-1
Numbers (in fixed number of bits) z. Unsigned vs. Signed z. Integer vs. Floating Point numbers
Number Systems: Unsigned Integer z. Decimal: Base 10 y 0, 1, 2, 3, …, 9, 10, 11, … y. Use 10 digits (0~9) to represent numbers y. Positional Notation: prepend more digits to left positions if greater than or equal to 10 (逢十進一) x. Each position take a different weights (units, tens, hundreds, …): 123 = 1* 100 + 2 * 10 + 3 x. Radix 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. 01 B
Number Systems: Unsigned Integer z Binary: Base 2 y Use 2 digits, 0 ~ 1, to represent all numbers y 0, 1, 10, 11, 100, 101, 110, 111, … y Positional Notation: prepend more digits to left positions x. Each position take a different weights (units, tens, hundreds, …) x 110 = 1 * 2^2 + 1 * 2^1 + 0 * 2^0 = 1 * 4 + 1 * 2 + 0 * 1 y 00, 012, 10 B, 11 B z Octal: Base 8 y 0, 1, 2, …, 7, 108, 11 O, …, 27 z Hexadecimal: Base 16 n-digit base-r number: N = i=0, n-1 di x ri y 0, 1, 2, …, 9, a 16, b 16, …, f 16, 10 H, 11 H, …, 0 FFH, … z BCD: Binary Coded Decimal (4 -bit for one decimal number) y 10010111 = 9710 (decimal) – packed format (two numbers in one byte) y 97: 00001001 00000111 – unpacked format (one number in one byte)
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 John Uffenbeck The 80 x 86 Family: Design, Programming, and Interfacing, 3 e Copyright © 2002 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
Number Systems: Unsigned Integer z Decimal Octal Hex. Binary BCD 0 0 0 0000 1 1 1 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 1001 10 12 a 1010 0001 0000 11 13 b 1011 0001 12 14 c 1100 0001 0010 13 15 d 1101 0001 0011 14 16 e 1110 0001 0100 15 17 f 1111 0001 0101 binary-to-octal: rule of threes
Base Conversion: Bin/2 Oct/8 Hex/16 z Binary to Octal (bi oj) y. The rule of threes: grouping binary digits three at a time x Every octal number can be represented by 3 bits (binary digits) z Binary to Hexadecimal (bi hj) y. The rule of fours: grouping binary digits four at a time x Every hexadecimal number can be represented by 4 bits x. BCH: Binary Coded Hexadecimal Number • 1101 0111 = D 7 H (Hexadecimal) z Octal to Hexadecimal y. Oct Bin Hex
Base Conversion: 2/8/16 10 z. Binary/Oct/Hex to Decimal (bi D) y. D = i=0, n-1 bi x 2 i y. D = i=0, n-1 oi x 8 i y. D = i=0, n-1 hi x 16 i xbi oi or hi D, for large numbers
Base Conversion: 10 2/16 z. Decimal to Binary –the integer part y. D = bmx 2 m + bm-1 x 2 m-1 + … + b 0 bi = 0? 1? y. Repeated subtraction: subtract to see if 2 m exists x. D’ = i=0, m-1 bi x 2 i = D - 2 m • bm=1, if positive or zero; bm=0, if not x. D D’ & m m’ (m’: max exp. s. t. bm’=1) y. Long division (repeated division) xi=0, D’ = D mod 2 … bi & D D’, i++ z. Alternative method: y. Dec Hex (shorter) Bin (longer)
Base Conversion: 10 2/16 z Decimal to Binary – the fractional part y. Repeated (Long) multiplication x. Repeatedly multiply the fractional part by 2 and take the whole part as the next bit y. Example: x 0. 25 = A x 2 -1 + B x 2 -2 + C x 2 -3 + … x. A=? ? , B=? ? , C=? ? y. Solution: x 0. 25 x 2 = 0. 5 = A + (B x 2 -1 + C x 2 -2 + … ) => A=0 x 0. 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
Representations of Negative Numbers z. Biased (shifted, offset, Excess-N/XS-N) z. Sign magnitude notation z 1’s complement z 2’s complement
Biased Numbers z decimal Binary/XS-4 biased 0 1 2 3 4 5 6 7 as unsigned integers 000 001 010 011 100 101 110 111 -4 -3 -2 -1 0 1 2 3 as signed integers
Sign Magnitude Notation z sign magnitude -3 1 11 -2 1 10 -1 1 01 -0 1 00 0 00 1 0 01 2 0 10 3 0 11
Complement for Base-R Numbers z For representing negative numbers y. Use complement of N to represent -N z For simplifying subtraction operations z Complement of complement restores to original y. Infer magnitude of a negative number carry 9’s in all n-digit 9’s complement carry for base-r number n-digit base-r number (0~rn-1) 10’s complement
Complement for Base-R Numbers z Base-r complements: two systems per base r yr’s complement & (r-1)’s complement for ‘N’ xr’s: rn – N (if N not zero), or 0 (if N=zero; truncated to n-bit) x(r-1)’s: = (rn – 1) – N (for integer, i. e. , no fractional part, m=0) x = rn – r -m – N (n: #integer digits, m: #fractional digits) y. Binary (base r=2): 2’s & 1’s y. Decimal (base r=10): 10’s & 9’s z Complement of complement restores to original y. N => N’ = rn – r-m –N => (N’)’ = rn–r-m – (rn–r-m – N) = N y. Infer magnitude of a negative number
Complement for Base-R Numbers z Binary (r=2): 2’s & 1’s y 2’s: 2 n – N (= 1’s + 1) [2 n =‘ 1 00… 0’ (carry + n-zeros)] y 1’s: 2 n – 1 – N (for integer numbers) [2 n – 1=‘ 11… 1’] x= Bitwise NOT/Complement over all digits (0=>1, 1=>0) z Example: Binary numbers y 2’s: 1011 [11 d] 0100+1=0101 [5 d = 16 d – 11 d] y 1’s: 1011 [11 d] 0100 [4 d = 15 d – 11 d] 1’s in all digits z Example: Decimal numbers: 10’s & 9’s y 10’s: 123 877 [=1, 000 -123] = (9’s + 1) y 9’s: 123 876 [= 999 -123]; [1+8=9, 2+7=9, 3+6=9]
1’s Complement Numbers (base 2) z All 1’s No carry 2 n 23 22 21 20 r =2 0 1 1 = 2 n-1 - n 3 n 2 n 1 n 0 = N’ (2 n-1)-N
2’s Complement Numbers (base 2) z All 0’s With carry 2 n 23 22 21 20 r =2 1 0 0 = 2 n - n 3 n 2 n 1 n 0 = N’’ 2 n -N
1’s Complement Numbers z -3 100 011 -2 101 010 2 n 22 21 20 r =2 -1 110 001 0 1 1 1 2 n -1 -0 111 000 0 000 111 -0 - n 2 n 1 n 0 = N 1 001 110 -1 = N’ 2 010 101 -2 3 011 100 -3 z Bitwise complement as the negative counterpart of a positive number [Neg. Num=Bitwise. Comp(Pos. Num)] z Two forms of ‘ 0’ [+0 & -0]
2’s Complement Numbers z -4 100 011 100 -3 101 010 011 -2 110 001 010 -1 111 000 001 0 000 111 000 1 001 110 111 -1 2 010 101 110 -2 3 011 100 101 -3 z Negative. Number = 1’s(Positive. Number)+1 z Unique representation for ‘ 0’ z MSB(Neg. Num)=‘ 1’ (sign bit) z Rotational(+12): 0 1 … 3 -4 -3 … -1 0
2’s Complement Numbers z. Sign-extension: y. Using larger word size (e. g. , 16 -bit instead of 8 bit) will extend sign bit to the left. y-6 : 1’s(0000 0110) + 1 = 1111 1001 + 1 x= 1111 1010 y-6 : 1’s(0000 0110) + 1 = 1111 1001 + 1 x= 1111 1010
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. ) John Uffenbeck The 80 x 86 Family: Design, Programming, and Interfacing, 3 e Copyright © 2002 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
1’s vs. 2’s Complement Numbers z 1’s: y. Easy to implement y. Subtraction: May requires two arithmetic additions (+1’s & +end-carry) y. Two forms of ‘ 0’ [+0 & -0] z 2’s: y. Less easier to implement y. Subtraction: Requires only one addition y. Unique form of ‘ 0’
Figure 1– 14 The unsigned and signed (2’s) bytes illustrating the weights of each binary-bit position.
Floating Point Representation z 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 = … z 21. 75 = 10101. 11 = 0. 1010111 * 25 = 1. 010111 * 24 (IEEE floating standard)
Floating Point Representation z FP Number Representation: (within limited word size) y. Sign: plus/minus y. Magnitude: Mantissa x Base ** Exponent x. 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 x. Exponent: (Power) • Determines the range of representation x. Base: normally 2 (but, 10 and 16 are also used)
PDP-11, VAX-11 Floating Point Representation [32 -bit] sign exponent mantissa 0 100001011100……. 0 8 bits 23 bits zprecision 1/224 (~ 1/107) zrange 2127 (~ 1038)
Bits as Character Codes ASCII - American Standard Code for Information Interchange - most widely used code, represents each character as a unique 8 -bit code.
Code & Character Sets z. ASCII ( American Standard Code for Information Interchange ) — 7 bit (0 x 00~0 x 7 F) 0 1 2 3 4 5 6 7 8 9 AB CDE F 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ! 0 1 @A P Q a p q “ 2 B R b r # 3 C S c s $ %&` ( ) 4 5 6 7 8 9 DE F GHI T U V WX Y d e f g h I t u v wx y * + - , . / J K L MN O Z j k l mn o z
7 -bit ASCII Codes 0 x 00 -0 x 1 F, 0 x 7 F: control characters (non-printable) 0 x 20 -0 x 7 E: graphic characters (printable)
7 -bit ASCII Codes Control. A~Z = SOH~SUB 0 1 2 3 4 5 6 7 0 NUL DLE SP 0 @ P ` p 1 SOH DC 1 ! 1 A Q a q 2 STX DC 2 “ 2 B R b r 3 ETX DC 3 # 3 C S c s 4 EOT DC 4 $ 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 ASCII codes z Native code set for most computer systems and programming languages z 128 codes (0 x 00~0 x 7 F) y. Control codes: x 2 columns + DEL y. Graphical characters: x. SP + punctuations + [0 -9 A-Za-z] x 8 -th bit (most significant bit): ‘ 0’ y 8 -bit ASCII: ISO-8859 -1 (including 80 h~FFh) xaka, Latin-1: extension for Latin/European characters
7 -bit ASCII Codes z 0 x 00 -0 x 1 f: control characters z 0 x 20 -0 x 2 f: space and symbols z 0 x 20: SPACE z 0 x 30 -0 x 39: digits (0 -9) z 0 x 3 a-0 x 40: symbols y 0 x 20 -0 x 40: symbols & digits z 0 x 41 -0 x 5 a: upper case letters ([A-Z]) z 0 x 5 b-0 x 60: symbols z 0 x 61 -0 x 7 a: lower case letters ([a-z]) z 0 x 7 b-0 x 7 e: symbols z 0 x 7 f (127): DEL
Extended ASCII Codes for PC z 0~127: y. ASCII (7 -bit) z 128~255: (ext) y 80 h~FFh
Unicode z Many Windows-based applications use the Unicode system to store alphanumeric data. ystores each character as 16 -bit data y. Java programming language use 16 -bit char data type: ‘u 4 fe 5' z Codes 0000 H– 00 FFH (the first 256 codes) are the same as standard ASCII code (prefixed by the 00 H byte). z Remaining codes, 0100 H–FFFFH, store all special characters from many character sets. y. Allows software for Windows to be used in many countries around the world. y. For complete information on Unicode, visit: http: //www. unicode. org
Chinese Characters z. Big-5 2 byte ( 16 bit ) y 5401(frequent)+7652(rare)-2(duplicated) y=13051字 z. CNS 11643 3 byte ( 24 bit ) 48222字
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)
FIGURE 2 -4 80 x 86 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 Uffenbeck The 80 x 86 Family: Design, Programming, and Interfacing, 3 e AL : = io. Port[27 H] BL : = AL AL : = io. Port[27 H] AL : = AL + BL io. Port[30 H] : = AL HALT Copyright © 2002 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
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
Bits as Addresses: Memory Capacity Memory Address (in binary or hex. form): - 0000 1111 = 0 F 0 Fh 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 1 K of storage.
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)
Basic Memory Specs: Capacity Prefixes for multiples of bits (b) or bytes (B) Decimal Value Binary 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
Computer Programming Machine Language Assembly Language Edit-Assembly-Test-Debug High-Level Languages: Interpreters vs. Compilers
Programming z Machine language/object codes: [Fig 2. 4] y. Binary sequences of bits, representing operations and operands [E 4 27 = IN AL, 27 H] y. Instruction set (IS): list of operations supported by a processor y. IS’s are machine/processor dependent z ISA: op-code + operand y. Op-code: actions [move, math, logical, jump/call] y. Operands: objects to act on y. Addressing modes: the ways to specify the physical addresses of the operands
FIGURE 2 -4 80 x 86 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) John Uffenbeck The 80 x 86 Family: Design, Programming, and Interfacing, 3 e KBD_PORT EQU 27 H PRT_PORT EQU 30 H … IN AL, KBD_PORT OUT PRT_PORT, AL … CALL PRINTF Symbolic names (Symbols) Copyright © 2002 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
Programming z Assembly: y Command mnemonics: MOV AL, BL z High level languages [Fig. 2 -5] y Basic, FORTRAN, Pascal, C, C++, Java y Machine independent z Interpreter vs. Compiler y Compile HLL into assembly codes for execution y Compiling at run time (interpreter) or pre-compilation (compiler) z Program Development y Edit, assembly, test and debug y DEBUG: an MSDOS utility
FIGURE 2 -5 C version of the input and add program. John Uffenbeck The 80 x 86 Family: Design, Programming, and Interfacing, 3 e Copyright © 2002 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
Microprocessor Program Development. SDT (CVTSYM) program Symbol Converter Debugger & Hardware Simulator ICE . SYM Editor . ASM Assembler . OBJ (MASM, X 8051) Linker . HEX (Link) cpu Target
Microprocessor Program Development z z 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) z Loader: load linked objects into memory for execution y Many “linkers” also include the loader functions z Symbol Table: a mapping table that store <name, value> pairs y Name: symbolic name for constant, address, port number y Value: binary values for the corresponding name z Symbolic Debugger: y Use dis-assembler to unassemble object codes into assembly codes y Replace binary values into symbolic names by looking up the symbol table y Easier to understand the semantics of the values from their symbolic names
Microprocessor Program Development z ICE (In-Circuit Emulator) y. 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) y. With a cable+socket that can replace the real CPU socket x. Send the emulated signals to the target board y. Capable of registering status & changes of the registers and memory contents y. Easier to trace/debug program step-by-step (instead of fullspeed run)
Computer Operating Systems What is an OS
What is an Operating System z OS: A control software that start and manage all services (applications) & a friendly program for mediating user and hardware y. Booting: loading required OS modules into memory y. Command interpretation (“shell”): accepting flexible command combinations by users to accomplish complicated tasks y. Job scheduling: putting multiple jobs to run simultaneously (actually, in time-sharing fashion) y. Resource management: allocating I/O, memory, file system, processor time to each job
Known Operating Systems z NOS, VMS: for early main frames z Unix (& Linux): for mini-computers & workstations z PC: y. CP/M (“Control Program for Microcomputer”): by Gary Kildall, 1978, 1 st m. C OS x 8080/Z 80 -based, 64 K memory, floppy disk drive y. Apple DOS: for APPLE–I, -IIe y. Microsoft DOS: MS-Basic + 86 -DOS (a CP/M workalike) y. PC-DOS: same as MS-DOS, for IBM PC y. Better GUI: OS/2, Mac, Windows 3. 1, 95, 98, NT, 2 K, XP, Vista, Windows 7, 8, 10…
Structure of MS-DOS OS Command Processor (COMMAND. COM ) MS-DOS Kernel (MSDOS. SYS) BIOS (ROM+IO. SYS) System Hardware
- Slides: 57