305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University A COMPUTER is an electronic device that can: – Receive information – Perform processes – Produce output – Store info for future use. 2 Personal computers (PC) 3 Mobile devices 4 Game consoles 5 Servers 6 Mainframe computers IBM zEnterprise 7 Super computers Fujitsu’s K - Computer 8 Embedded systems 9 Robots 10 PC Mainframe Mobile Etc. Hardware Programmer System Analyst User Administrator Etc. Software OS Applications Firmware Content Etc. Peopleware 11 Feedback Input (Input Devices) Process (CPU) Output (Output Devices) Storage (Memory) 12 13 14 15 Arithmetic and logic unit (ALU) – performs arithmetic and logical operations Control unit – controls the flow of data through the processor, and coordinates the activities of the other units within it Registers – small amount of storage available on the CPU whose contents can be accessed more quickly than storage available elsewhere 16 Main (primary) memory – Stores programs and data while computer is running. Main memory is fast and limited in capacity. The CPU can only directly access information in main memory. – Random Access Memory (RAM) – Read-Only Memory (ROM) 17 Memory mapping Address Values 1 0 1 1 1 0 0 1 0000 0 0 0 0 1 1 1 1 0001 0002 .. .. .. .. FFFF 1 1 1 0 0 0 1 1 • Size : • • • • • • • • Kilobyte Megabyte Gigabyte Terabyte Petabyte Exabyte Zettabyte Yottabyte KB MB GB TB PB EB ZB YB 210 ~103 220 ~106 230 ~109 240 ~1012 250 ~1015 260 ~1018 270 ~1021 280 ~1024 0 0 1 0 0 1 0 0 18 External (secondary) memory – Holds information too large for storage in main memory. – Information on external memory can only be accessed by the CPU if it is first transferred to main memory. – It retains information when the computer is switched off. 19 CPU ALU Main Memory CU VDO controller Registers BUS Clock Disk controller I/O ports 20 A clock signal is a signal generated by quartz crystal circuit that oscillates between a high and a low state and is utilized to coordinate actions of circuits. Clock rate is measured in cycles/sec (Hertz ). 21 User Level 5 Problem oriented language level Translation (compiler) Level 4 Assembly Language Translation (assembler) Level 3 Operating System Partial interpretation (operating system) Level 2 Instruction Set Architecture Interpretation (microprogram) or direct execution Level 1 Micro-architecture level Hardware Level 0 Digital Logic Level Circuits 22 The interesting objects at this level are gates; Each gate has one or more digital inputs (signals representing a 0 or 1) and computes as output some simple function of these inputs, such as AND or OR; A small number of gates can be combined to form a 1-bit memory, which can store a 0 or 1; The 1-bit memories can be combined in groups of, for example, 16, 32 or 64 to form registers. 23 A collection of 8-32 registers that form a local memory and ALU The registers are connected to the ALU to form a data path over which the data flow; The operation of the data path may be controlled by a microprogram, directly by hardware, or even by software. 24 The ISA level is defined by the machine’s instruction set This is a set of instructions carried out interpretively by the microprogram or hardware execution sets; Machine languages – Strings of numbers giving machine specific instructions 25 The Operating System Machine (OSM) level is a complete set of instructions available to application programmers. This includes all ISA level instructions and a new set of instructions that the operating system adds called system calls. The OSM level is always interpreted. For instance, if reading data from file, the operating system carries it out step by step. 26 This level is really a symbolic form. This level provides a method for people to write programs for levels 1, 2 and 3 in a form that is more readable. Programs in assembly language are first translated to level 1, 2 or 3 language and then interpreted by the appropriate virtual or actual machine. The program that performs the translation is called an assembler. Example: LOAD ADD MOV R1, #FFFF R2,R1 R3,R2 27 This level usually consists of languages designed to be used by applications programmers. These languages are generally called higher level languages, for examples: C, C++, Java, BASIC, LISP, Prolog, Pascal, FORTRAN, COBOL, etc. Programs written in these languages are generally translated to Level 3 or 4 by translators known as compilers, although occasionally they are interpreted instead. 28 Computers use binary numbers internally because storage devices like memory and disk are made to store 0s and 1s. A number or a text inside a computer is stored as a sequence of 0s and 1s. Each 0 and 1 is called a bit, short for binary digit. The binary number system has two digits, 0 and 1. Binary numbers are not intuitive. When you write a number like 20 in a program, it is assumed to be a decimal number. Internally, computer software is used to convert decimal numbers into binary numbers, and vice versa. 29 The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The value that each digit in the sequence represents depends on its position. A position in a sequence has a value that is an integral power of 10. 5892 = 5 x 103 + 8 x 102 + 9 x 101 + 2 x 100 We say that 10 is the base or radix of the decimal number system. Similarly, the base of the binary number system is 2 since the binary number system has two digits and the base of the hex number system is 16 since the hex number system has sixteen digits. 30 A positional number system Has only 2 symbols (0 and 1), so that base = 2 The maximum value of a single digit is 1 (base – 1) The maximum value of N digits is 2N-1 Used mostly in computer system 31 5 digits binary can store number up to 25-1 = 32-1 = 31 Example 101012 = = = (1x24) + (0x23) + (1x22) + (0x21) + (1x20) 16 + 0 + 4 + 0 + 1 2110 32 A positional number system Has only 8 symbols (0, 1, 2, 3, 4, 5, 6, 7), so that base = 8 The maximum value of a single digit is 7 (base – 1) The maximum value of N digits is 8N-1 Used mostly in computer system 33 Example 20578 = = = (2x83) + (0x82) + (5x81) + (7x80) 1024 + 0 + 40 + 7 107110 34 A positional number system Has only 16 symbols (0-9, A, B, C, D, E, F), so that its base = 16 The maximum value of a single digit is 15 (F) (base – 1) The maximum value of N digits is 16N-1 Used mostly in computer system 35 Example 1AF16 = = = (1x162) + (10x161) + (15x160) 256 + 160 + 15 43110 36 Method 1. Determine the column (positional) value of each digit. 2. Multiply the obtained column values by the digits in the corresponding columns. 3. Calculate the sum of these products. 37 Division-Remainder Method 1. Divide the decimal number by the value of the new base. 2. Record the remainder from step 1 as the right most digit of the new base number 3. Divide the quotient of the previous divide by the value of the new base. 4. Record the remainder from step 3 as the next digit (to the left) of the new base number. 5. Repeat step 3 and 4 until the quotient become 0, the last remainder will be the most significant digit of the new base number. 38 Example 95210 = ?8 Solution 8 )952 )119 )14 )1 0 Hence, 95210 = 016708 Remainders 0 7 6 1 39 Example 95210 = ?16 Solution 16)952 )59 )3 0 Hence, 95210 = 3B816 Remainders 8 11 3 40 Method 1. Convert the original number to a decimal number 2. Convert the decimal number that obtained on in step 1 to the new base number 41 Method 1. Divide the digits into groups of three starting from the right. 2. Convert each group of three binary digits to one octal digit using the method of binary to decimal conversion. 42 Example 11010102 = ?8 Solution 1. Divide the binary into groups of three digits 001 101 010 2. Convert each group into one octal digit 0012 = 0x22 + 0x21 + 1x20 = 1 1012 = 1x22 + 0x21 + 1x20 = 5 0102 = 0x22 + 1x21 + 0x20 = 2 Hence, 11010102 = 1528 43 Method 1. Convert each octal digit to 3 binary digits. 2. Combine all the resulting binary groups into a single binary number. 44 Example 5628 = ?2 Solution 1. Convert each octal digit to 3 binary digits. 58 = 1012 68 = 1102 28 = 0102 2. Combine all the resulting binary groups into a single binary number. 5628 = 101 110 010 2 Hence, 5628 = 1011100102 45 Method 1. Divide the digits into groups of four starting from the right. 2. Convert each group of four binary digits to one hexadecimal digit using the method of binary to decimal conversion. 46 Example 1111012 = ?16 Solution 1. Divide the binary into groups of four digits 0011 1101 2. Convert each group into one hexadecimal digit 00112 = 0x23 + 0x22 + 1x21 + 1x20 = 3 11012 = 1x23 + 1x22 + 0x21 + 1x20 = 13 = D Hence, 1111012 = 3D16 47 Method 1. Convert each hexadecimal digit to 4 binary digits. 2. Combine all the resulting binary groups into a single binary number. 48 Example 2AB16 = ?2 Solution 1. Convert each hex digit to 4 binary digits. 216 = 00102 A16 = 10102 B16 = 10112 2. Combine all the resulting binary groups into a single binary number. 2AB16 = 0010 1010 1011 2 Hence, 2AB16 = 0010101010112 49 All information that is processed by computers is converted in one way or another into a sequence of numbers. This includes – numeric information – textual information and – Pictures, sound, video, etc. Therefore, if we can derive a way to store and retrieve numbers electronically this method can be used by computers to store and retrieve any type of information. 50 Computers Store ALL information using binary numbers Computers use binary numbers in different ways to store different types of information. Common types of information that are stored by computers are : – Whole numbers (i.e. Integers). Examples: 8 97 -732 0 -5 etc. – Numbers with decimal points. Examples: 3.5 -1.234 0.765 999.001 etc. – Textual information (including letters, symbols and digits) 51 Unsigned integers – Given n bits, it is possible to represent the range of values from 0 to 2n - 1 – For example an 8-bit representation would allow representations that range 0 to 255 52 Signed Magnitude – An extra bit in the most significant position is designated as the sign bit which is to the left of an unsigned integer. The unsigned integer is the magnitude. S MMMMMMMMMMMMMMM – A 0 in the sign bit means positive, and a 1 means negative 53 Signed Magnitude (cont.) – Given an n bit signed magnitude number the range of values that it can represent is -(2n-1-1) to +(2n-1-1) for example: 8 bits -127 to 127 16 bits -32767 to 32767 – Signed magnitude representation associates a sign bit with a magnitude that represents zero, thus it has two distinct representation of zero: 00000000 and 10000000 54 1’s Complement representation – For positive numbers, the representation is the same as for unsigned integers where the most significant bit is always zero – The additive inverse of a 1’s complement representation is found by inverting each bit. – Inverting each bit is also called taking the 1’s complement 55 1’s Complement representation Examples 0000 0011 (3) 1111 1100 (-3) 0001 0111 (23) 1110 1000 (-23) 0000 0000 (0) 1111 1111 (0) 56 2’s Complement representation – The additive inverse of a 2’s complement integer can be obtained by adding 1 to its 1’s complement – The 2’s complement representation for a negative number is the additive inverse of its positive representation – An advantage of 2’s complement is that there is only one representation for zero – Given an n bit 2’s complement number the range of values that it can represent is -(2n-1) to +(2n-1-1) 57 2’s Complement representation Examples 010001 (17) take 000000 (0) the 1’s complement 101110 + 1 101111 (-17) 111111 + 1 1000000 (0) 58 mantissa exponent 6.0210 x 1023 decimal point radix (base) Normalized form: no leadings 0s (exactly one digit to left of decimal point) Alternatives to representing 1/1,000,000,000 –Normalized: 1.0 x 10-9 –Not normalized: 0.1 x 10-8,10.0 x 10-10 59 mantissa exponent 1.02 x 2-1 “binary point” radix (base) Computer arithmetic that supports it called floating point, because it represents numbers where the binary point is not fixed, as it is for integers 60 Normalized format: +1.xxxxxxxxxx2 x 2yyyy2 Multiple of Word Size (32 bits) 31 30 23 22 S Exponent 1 bit Mantissa 8 bits 0 23 bits S represents Sign Exponent represents y’s Mantissa represents x’s 61 To store letters and symbols, the computer assigns every character a numerical value. Computers remember letters and other symbols by storing the binary number for the symbol. For this system to work a standard numbering system needs to be defined and consistently used for all symbols that the computer needs to process. 62 ASCII (American Standard Code for Information Interchange) is the standard numbering given to all characters. “ASCII values” range in number from 1 to 128 (8 bits). 63 Unicode is a computing industry standard for the consistent encoding, representation and handling of text expressed in most of the world's writing systems. Unicode can be implemented by different character encodings. The most commonly used encodings are UTF-8 (which uses one byte for any ASCII characters, which have the same code values in both UTF-8 and ASCII encoding, and up to four bytes for other characters. 64 A raster image, or bitmap, consists of a rectangular array of pixels. Each pixel has a corresponding red, green, and blue value that combine to determine the color displayed by that pixel. 65 Audio analog to digital converters work by repeatedly measuring the amplitude of an incoming electrical pressure sound wave, and outputting these measurements as a long list of binary bytes. 66