You are on page 1of 22

Chapter 2

Computer Codes, Programming, and Operating Systems Outline


2.1 The Binary and Hexadecimal Number Systems 2.2 Computer Codes 2.3 Computer Programming 2.4 Computer Operating Systems

2-1

2.1 The Binary and Hexadecimal Number Systems Comparing Binary and Decimal Numbers

Converting from decimal to binary (a)Repeated SubtractionThe highest power of two is first subtracted from the number. The process is then repeated, subtracting the next highest power of two from the remainder.

2-2

(b)Repeated DivisionThe decimal number is repeatedly divided by two. The remainder, always a 1 or 0, becomes the binary result.

2-3

Hexadecimal Numbers (From Binary to Hexadecimal) The rule of foursHexadecimal is a popular number system used with microcomputers. Hex numbers are formed by grouping the binary digits four at a time, beginning on the right (the Rule of Fours). Ex: 11010101 =
D 516

From Hexadecimal to Binary Ex:


2016 = 00100000 2

2-4

From Hexadecimal to Decimal Ex. 2C 16 = 2x16+12x1= 44 10

From Decimal to Hexadecimal Ex. 39 10 = 39/16 = 2 remainder 2/16 = 0 39 10 = 27 16 Note that, for large decimal numbers, first converting the decimal number to hexadecimal, and then to binary, may be easier than converting the number directly to binary. remainder 7 2

Data Sizes:

byte (8 bits) word (2 bytes) doubleword or dword quad word

8-bit 16-bit 32-bit (80386, 80486) 64-bit (Pentium)

2-5

2.2 Computer Codes ASCII The Standard for Text ASCII (American Standard Code for Information Interchange) was first defined by the American National Standards Institute in 1968. In this code, each letter of the alphabet, punctuation mark, and decimal number is assigned a unique 7-bit code number. With 7 bits, 128 unique symbols can be coded. See Table 2.5.

2-6

Error Detection Codes ParityOne of the advantages of ASCII is that it allows text information to be transmitted between computers over long distances. This is done by converting the seven parallel ASCII data bits to bit-by-bit serial form. Using a device called a modem, the serial data are converted to audio tones, allowing computer-to-computer communication through the telephone network. Example Transmitter Data Receiver

1) High current or voltage switching in nearby equip. 2) Lightning 3) Signal distortion due to long cable lengths 4) Inductive coupling (cross-talk) between adjacent conductors To protect against these errors a) Parity a) Parity b) Checksum

2-7

Ex.1 Assume F is to be encoded in ASCII with even parity F has the ASCII code 46H, or 100 0110 The even parity-encoded F becomes 1100 0110 Ex.2 Assume A is to be encoded in ASCII with even parity A has the ASCII code 41H, or 100 0001 The even parity-encoded A becomes 0100 0001 Ex.3 How to do that the E1H, 20H and 72H are encoded using even parity? As you may have noted, the parity method of error detection can only be used to detect odd numbers of errors. Ex. 72H = 0111 0010 77H = 0111 0111
2-8

In many cases, the likelihood of even single bit errors is very small, and the addition of a single parity bit is very effective error detection method. Many microcomputer systems use this technique to protect data bytes fetched from memory by the processor.

b) ChecksumWhen data is to be transmitted over long distances via the telephone network, single parity bits are less effective. This is because the data is subject to burst noise sources that may persist for several milliseconds, changing the value of many data bits in the process. This is the case for a lightning strike.

For this type of noise, data is often protected by sending a checksum character following the transmission of a large block of characters. The checksum is computed by adding the numeric values of all bytes in the block. If the received checksum does not match the computed checksum byte, the receiver requests that the entire data block be
2-9

retransmitted.

Checksum can be divided into two types (1) -checksum (a) 8-bit -checksum Ex. Graseby 3400 Syringe Pump Command string structure (<) (command)(parameter)(>)(checksum)(CR) <STOP>(hex -checksum)(CR)(LF) The ASCII codes of the 6 characters <STOP> is 448 8-bit -checksum = (<STOP>) modulo 256 = (60+83+84+79+80+62)modulo 256 = (448) modulo 256 = 192 = C0h See Ref. 2, P. 217 Table 14.5

2-10

(b) 7-bit -checksum A 7-bit -checksum is also commonly used, and is the remainder after dividing by 128. ExThe ASCII codes sum is 130 7-bit -checksum = (130) modulo 128 =2 = 02h (2) Twos complement checksum (TC-checksum) TC-checksum = 128 (data) modulo 128 The 7-bit TC-checksum is used by a number of medical devices; two such devices being the Ohmeda 7800 ventilator and Ohmeda 9000 syringe pump.
2-11

Ex. Ohmeda 7800 ventilator Command string structure (:) (command)(parameter)(checksum)(CR) :VTYC(CR) The ASCII codes of the 4 characters :VTY is 317 7-bit TC-checksum = 128 - (:VTY) modulo 128 = 128 -(58+86+84+89) modulo 128 = 128 - (317) modulo 128 = 128 - 61 = 67 =C

Twos Complement The twos complement code provides a way to represent positive and negative binary numbers signed binary numbers 1. If the number is positive, make no changes 2. If the number is negative, invert all bits and then add 1. Ex. 6 00000110 +1 = 11111001 +1 = 11111010 = FAH
2-12

Ex. Convert the following signed binary numbers to their decimal equivalent (a) 11010001 (b) 01010000 (c) 1000 1111 0101 1101

S (a) 11010001 + 1 = 00101110 + 1 = 00101111 = 2FH-47

Twos Complement Arithmetic The twos complement coding of negative numbers may at first seem complicated, but it is actually a very clever code. + 14 = + - 20 = 0000 1110 0001 0100 + 1 = 1110 1011 + 1 = + 1110 1100 1111 1010

2-13

OverflowWhenever two signed numbers are added or subtracted, the possibility exists that the result may be too large for the number of bits allocated. Ex. +64 = 0100 0000 +96 = 0110 0000 +160 = 1010 0000 +64 = 0000 0000 0100 0000 +96 = 0000 0000 0110 0000 +160 = 0000 0000 1010 0000 Y2K Problem !!! (http:/y2kbmd.mc.ntu.edu.tw/)

Binary Codes Decimal (BCD) The BCD code provides a way for decimal numbers to be encoded in a binary form that is easily converted back to decimal. Ex. 367 10 = 0011 0110 0111 BCD 1001 0101 BCD = 95 10 The most common application of BCD is with seven-segment displays. (See Fig. 2.3)
2-14

2.3 Computer Programming (See Textbook p. 54) Introduction A computer is usually described by its hardware the number of bits used by its data and address buses, the amount of RAM and ROM memory, the type of processor chip, etc. But to do useful work, it is the software that is important; that is, the programs that can be run on that computer.

2-15

There are three different ways of writing these programs 1. Machine language 2. Assembly language 3. High level language

Machine language: Object codeThe instruction set of a computer is a list of the operations that can be performed by the processor. Each of these instructions has its own unique binary code. Consider the following 80x86 microprocessor program that adds two numbers input from a keyboard. (see p. 54)

2-16

Machine language or object code (This is the only code a computer can execute. However, it is nearly impossible for a human to work with). Object code is machine-dependent; that is, object code written for an 80x86 processor will only work with processors in the 80x86 family. Object code is usually written in hexadecimal form. The previous program written in hex becomes: E4 27 88 C3 E4 27 00 D8 E6 30 F4 Certainly this is more readable than the binary version, but the function of the program is still hidden.

Assembly language: When programming a microprocessor, programmers often use assembly language. This involves using 3-5 letter abbreviations for the instruction operation codes rather than the binary or hex object codes. (See Fig. 2.4)

2-17

Source CodeThe assembly language program in Fig. 2.4 is said to be in source-code form. The development of a modern computer program actually requires four separate steps 1. Creating the source code Using an editor, the source code of the program is created. For an assembly language

program, this means selecting the appropriate instruction operation codes to accomplish the task. 2. Compiling the program A compiler program, which examines the source-code file created by the editor and determines the object codes for each instruction in the program, is then run. When compiling assembly language programs, the compiler is often referred to as an assembler.
2-18

3. Linking Although the object code produced by the compiler is in machine language, it is not in a form that can be directly executed by the computer. The last step is to link the object code using a program called the linker. The linker takes the object file produced by the compile and combines this with other object files and library functions to produce an executable program. The linker produces an executable program. You can now run the program by simply typing in the name of the program. 4. DebuggingUsually the program does not work correctly on the first try, and changes must be made. This involves reinvoking the editor to modify the source-code file, recompiling, relinking, and retesting. Locating anf fixing the source of errors is called debugging.

High level language: High-level languages are geared more toward the people writing the programs rather than the computer.
2-19

They are closer to English and save time for programming.

The disadvantage of the Machine and assembly languages 1. Programs are machine dependent and cannot be easily transferred from one computer system to another 2. Programming is very time consuming to learn. It requires considerable experience to become proficient. 3. Most of the program is occupied with internal details which have very little to do with the actual task to be accomplished. 4. Programs are difficult to alter due to their complexity. 5. It is very easy to introduce errors and crash the computer. The advantage of high-level programming languages 1. Learning to program in a high-level language is easy since the programs are written in a language that the programmer is accustomed. 2. The programmer does not need to know about the organization or the internal architecture of the computer. 3. High-level programs require fewer statements. A single
2-20

source statement generates many machine statements. 4. Theoretically, high-level programs are machine

independent, and programs can be run on a great many different computers. 5. High-level languages have extensive error diagnostics. These help the programmer in locating and correcting errors in the programs and generally result in a more friendly environment.

BASIC Code)

(Beginners

All-Purpose

Symbolic

Instruction

Many programmers opt to work in a safer environment. They choose a high level language that offers an easier to comprehend syntax together with a built-in safety net that will prevent their programs from crashing. BASIC Program 10 INPUT N1, N2 20 PRINT SUN=; N1+N2 30 END
2-21

C Programming In recent years, C has become the high level language of choice for most program developers. Also, C is a structured high level language. (See p. 58 Fig. 2.5)

2-22