Numbersystem

Embed Size (px)

Citation preview

  • 8/13/2019 Numbersystem

    1/22

    2-1

    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

  • 8/13/2019 Numbersystem

    2/22

    2-2

    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.

  • 8/13/2019 Numbersystem

    3/22

    2-3

    (b)Repeated DivisionThe decimal number is repeatedly

    divided by two. The remainder, always a 1 or 0,

    becomes the binary result.

  • 8/13/2019 Numbersystem

    4/22

    2-4

    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 = 165D

    From Hexadecimal to Binary

    Ex: 1620 = 00100000 2

  • 8/13/2019 Numbersystem

    5/22

    2-5

    From Hexadecimal to Decimal

    Ex. 2C16= 2x16+12x1= 4410

    From Decimal to Hexadecimal

    Ex. 3910 = 39/16 = 2 remainder 7

    2/16 = 0 remainder 2

    3910 = 2716

    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.

    Data Sizes: byte (8 bits) 8-bit

    word (2 bytes) 16-bit

    doubleword or dword 32-bit (80386, 80486)

    quad word 64-bit (Pentium)

  • 8/13/2019 Numbersystem

    6/22

    2-6

    2.2 Computer Codes

    ASCIIThe Standard for TextASCII (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.

  • 8/13/2019 Numbersystem

    7/22

    2-7

    Error Detection CodesParityOne 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)Lightning3)Signal distortion due to long cable lengths4)Inductive coupling (cross-talk) between adjacent

    conductors

    To protect against these errors

    a)Parity b) Checksuma) Pari ty

  • 8/13/2019 Numbersystem

    8/22

  • 8/13/2019 Numbersystem

    9/22

    2-9

    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

  • 8/13/2019 Numbersystem

    10/22

    2-10

    retransmitted.

    Checksum can be divided into two types

    (1)-checksum(a) 8-bit -checksum

    Ex. Graseby 3400 Syringe Pump

    Command string structure

    ()(checksum)(CR)

    (hex -checksum)(CR)(LF)

    The ASCII codes of the 6 characters is 448

    8-bit -checksum = () modulo 256

    = (60+83+84+79+80+62)modulo 256

    = (448) modulo 256

    = 192

    = C0h

    See Ref. 2, P. 217 Table 14.5

  • 8/13/2019 Numbersystem

    11/22

    2-11

    (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.

  • 8/13/2019 Numbersystem

    12/22

    2-12

    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 ComplementThe twos complement code provides a way to

    represent positive and negative binary numbers signed

    binary numbers

    1.If the number is positive, make no changes2.If the number is negative, invert all bits and then add 1.

    Ex.6 00000110 +1 = 11111001 +1 = 11111010 = FAH

  • 8/13/2019 Numbersystem

    13/22

    2-13

    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 ArithmeticThe twos complement coding of negative numbers may

    at first seem complicated, but it is actually a very clever

    code.

    + 14 = 0000 1110

    + - 20 = 0001 0100 + 1 = 1110 1011 + 1 = + 1110 1100

    1111 1010

  • 8/13/2019 Numbersystem

    14/22

    2-14

    OverflowWhenever two signed numbers are added orsubtracted, 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. 36710 = 0011 0110 0111 BCD

    1001 0101 BCD = 9510

    The most common application of BCD is with

    seven-segment displays. (See Fig. 2.3)

  • 8/13/2019 Numbersystem

    15/22

    2-15

    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.

  • 8/13/2019 Numbersystem

    16/22

    2-16

    There are three different ways of writing these programs

    1.Machine language2.Assembly language3.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)

  • 8/13/2019 Numbersystem

    17/22

    2-17

    Machine language or object code (This is the only code acomputer can execute. However, it is nearly impossible

    for a human to work with).

    Object code is machine-dependent; that is, object codewritten for an 80x86 processor will only work with

    processors in the 80x86 family.

    Object code is usually written in hexadecimal form. Theprevious 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)

  • 8/13/2019 Numbersystem

    18/22

    2-18

    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 codeUsing an editor, the source codeof the program is created. For an assembly language

    program, this means selecting the appropriate instruction

    operation codes to accomplish the task.

    2.Compiling the programA compiler program, whichexamines 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.

  • 8/13/2019 Numbersystem

    19/22

    2-19

    3.LinkingAlthough the object code produced by thecompiler 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 correctlyon 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.

  • 8/13/2019 Numbersystem

    20/22

    2-20

    They are closer to English and save time for programming.

    The disadvantageof the Machine and assembly languages

    1. Programs are machine dependent and cannot be easilytransferred from one computer system to another

    2. Programming is very time consuming to learn. It requiresconsiderable experience to become proficient.

    3. Most of the program is occupied with internal detailswhich 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 advantageof high-level programming languages

    1.Learning to program in a high-level language is easysince the programs are written in a language that the

    programmer is accustomed.

    2.The programmer does not need to know about theorganization or the internal architecture of the computer.

    3.High-level programs require fewer statements. A single

  • 8/13/2019 Numbersystem

    21/22

    2-21

    source statement generates many machine statements.

    4.Theoretically, high-level programs are machineindependent, 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 (Beginners All-Purpose Symbolic Instruction

    Code)

    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

  • 8/13/2019 Numbersystem

    22/22

    2 22

    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)