Docstoc

Program Instructions - the Machine Language of the Computer

Document Sample
Program Instructions - the Machine Language of the Computer Powered By Docstoc
					A2/414 Extension




                      AQA A2 Computing



                   Unit A2/414 Extension


             The Assembly Language Process




                                             1
A2/414 Extension

Program Instructions - the Machine Language of the Computer
A typical microprocessor chip has an instruction set of several hundred instructions. Each
one of these instructions is represented by a unique binary number (code). Instructions are
available to do simple mathematical operations such as add and subtract, to transfer data to
and from memory and to perform logical operations on data.

Each type of processor has its own set of binary instruction codes and so a program written in
the machine language of one processor will not run on another processor. For example an
Apple Macintosh computer using the Motorola 68000 series of processors while the PC uses
processors based on the Intel 8086. Thus the Apple Macintosh computer cannot run PC
programs and vice-versa.

A Simple Computer - the MCB-1
Let us assume that the processor of the MCB-1 uses 8-bit instructions. Each instruction is in
two parts. One part holds the instruction or operation code (op code) which decides what the
instruction actually does. The other part holds the memory address (if required) that will be
used by the instruction to store or retrieve data from main memory. Each memory cell is 8
bits (1 byte) so can hold one instruction.

Thus the 8 bit instruction is made up of a 4 bit op code followed by a 4 bit address. Thus the
processor can have a machine language of no more than 24 = 16 instructions (op codes) and 24
= 16 addresses (memory locations). This is a very lowly computer but sufficient to show the
basic idea.

The MCB-1 Instruction Set (Machine Language)
 Op Code        Decimal                                   Meaning
                  (for
             convenience)
  0000             00          Stop or Halt.
  0001             01          Load the accumulator with the data in a memory location
                               given in the address part.
  0010             02          Store the contents of the accumulator in the memory
                               location given in the address part.
  0011             03          Add the contents of the memory location given in the
                               address part to the contents of the accumulator. Store the
                               result in the accumulator.
  0100             04          Subtract the contents of the memory location given in the
                               address part from the contents of the accumulator. Store
                               the result in the accumulator.
  0101             05          Jump to the instruction stored at the address given in the
                               address part. (Unconditional jump)
  0110             06          Jump to the instruction stored at the address given in the
                               address part IF the contents of the accumulator are
                               NEGATIVE. (Conditional jump)
  0111             07          Jump to the instruction stored at the address given in the
                               address part IF the contents of the accumulator are ZERO.
                               (Conditional jump)
  1000             08          Input a number and store it in the memory location given
                               by the address part
  1001             09          Output a number from the memory cell given in the address
                               part.


2
A2/414 Extension

So a complete instruction might be: 00110101

This means: Add to the accumulator (0011) the contents of the memory cell at address 0101.

1) Decode the following MCB-1 instructions:
       (a) 01100111 (e) 11110101
       (b) 00000000 (f) 01001111
       (c) 01011101 (g) 10000000
       (d) 00111111 (h) 01111100

2) A computer processor uses 16 bit instructions where 6 bits are used for the op code and the
remaining 10 bits are used for addresses. How many different instructions can the instruction
set for this computer have? How many memory cells can the main memory have?

A simple program to add two numbers together and store the result in main memory would be
stored in memory thus:

 Memory            Instruction                             Meaning
 Address
0000           0001 1100
0001           0011 1101
0010           0010 1110
0011           0000 0000

1100           0000 0011         Data
1101           0000 0010         Data
1110           0000 0000         Data

3) Decode each instruction in the table above (Convert the addresses to decimal for
convenience).

How a Program is executed - The Fetch Execute Cycle
Basically a program is executed by bringing the instructions one by one from main memory
into the processor where they are decoded and executed. This is repeated for each instruction.
The process is called the ‘Fetch Execute Cycle’.

To start the execution the address of the first instruction is placed into the Program Counter
(PC). The Fetch Execute cycle can then start:

        (i)   Fetch - A copy of the instruction is fetched from the memory cell whose address
              is in the PC and place in the Current Instruction Register (CIR).
        (ii) Before execution the PC is incremented by 1 so that it now contains the address
              of the NEXT instruction that will be executed.
        (iii) Execute - The instruction just copied into the CIR is decoded and executed.
        (iv) The cycle starts again at (I).

NB. 1. When a jump instruction is executed the address part is placed in the PC. In this
way jump instruction can be used to affect the order in which instructions are executed.
        2. This cycle has not included the involvement of the MAR and MDR which is given
in your text book.



                                                                                                 3
A2/414 Extension

Dry Running Programs
Dry running is a technique for testing a program before it is entered into the computer. The
program is executed on paper! A dry run shows the contents of the registers and memory
cells at the end of each instruction cycle.

To illustrate the technique the program above to add two numbers together is dry run below.
Firstly the program is reprinted here in decimal to make things a little simpler.

                   Memory        Address        Instruction      Instruction
                   Address      in decimal                       in decimal
                    0000            00           0001 1100          01 10
                    0001            01           0011 1101          03 11
                    0010            02           0010 1110          02 12
                    0011            03           0000 0000          00 00

                    1100            10           0000 0011           Data
                    1101            11           0000 0010           Data
                    1110            12           0000 0000           Data

For the dry run make a table containing columns for the registers and the memory cells used
by the programs data:

     PC             CIR        Accumulator      Location 10     Location 11      Location 12
     01             01 10          03               03              02               00
     02             03 11          05               03              02               00
     03             02 12          05               03              02               05
     04             00 00          05               03              02               05

Things to do
1) Dry run the following program using input data 2 then 3:
    Memory          Instruction                             Meaning
    Address
       00              0813
       01              0814
       02              0115
       03              0313
       04              0215
       05              0114
       06              0412
       07              0214
       08              0710
       09              0502
       10              0915
       11              0000
       12              0001      Data
       13              0000      Data
       14              0000      Data
       15              0000      Data




4
A2/414 Extension

What is the output produced and what is the program doing?




2) Explain the meaning of the bye 01000001:
(a) if it is an MCB-1 machine code instruction
(b) if it is a number stored in 2’s complement.

3) A computer uses 8 bit instructions where 3 bits are used as the op code and 5 bits for the
memory address.
(a) How many instructions are possible for this computer?
(b) How many memory cells can the main memory have?




                                                                                                5
A2/414 Extension

MCB-1 Functional Units
                         Control Unit             Clock

                                Program Counter

                           Current Instruction Register   External
    External
                                                          Data and
    Data and              Op Code       Address           Address
    Address
                                                          Bus
    Bus
                           Op             Address
                           Decoder        Decoder


                           To control     To
                           switches       address
                                          switches


                         Arithmetic & Logic Unit
                                 Accumulator

                                Logic circuits


                                Result Register


                         Memory Unit (IAS)
                           Memory Address Register
                             Memory Data Register

                                        IAS



                                                             Input
                                                              Port
                         Input/Output Unit



                                                            Output
                                                             Port




6
A2/414 Extension

Assembly Language
Because writing and correcting (debugging) programs written in machine code is difficult and
time consuming the binary codes are replaced with short abbreviated words or mnemonics.
The mnemonic for an instruction is chosen to represent the meaning of the instruction and so
make it easier to remember and use.

The table below gives the machine language instruction set of the MCB-1 computer along
with its assembly language mnemonics.

The MCB-1 Instruction Set (Machine Language)
Op Code Mnemonic                                       Meaning
  0000       HLT         Stop or Halt.
  0001       LDA         Load the accumulator with the data in a memory location given in
                         the address part.
  0010        STA        Store the contents of the accumulator in the memory location
                         given in the address part.
  0011       ADD         Add the contents of the memory location given in the address part
                         to the contents of the accumulator. Store the result in the
                         accumulator.
  0100        SUB        Subtract the contents of the memory location given in the address
                         part from the contents of the accumulator. Store the result in the
                         accumulator.
  0101        JPU        Jump to the instruction stored at the address given in the address
                         part. (Unconditional jump)
  0110        JPN        Jump to the instruction stored at the address given in the address
                         part IF the contents of the accumulator are NEGATIVE.
                         (Conditional jump)
  0111        JPZ        Jump to the instruction stored at the address given in the address
                         part IF the contents of the accumulator are ZERO. (Conditional
                         jump)
  1000        INP        Input a number and store it in the memory location given by the
                         address part
  1001       OUP         Output a number from the memory cell given in the address part.

The program given earlier to add two numbers together might be written in assembly
language as follows:

        .BEGIN 0
        NUM1:4
        NUM2:3
        ANS:

        LDA NUM1
        ADD NUM2
        STA ANS
        HLT
        .END

The words BEGIN and END are instructions on how to assemble (translate) the program.
They are examples of assembler directives and, as such, are not part of the assembly
language. In the MCB-1 assembly language all directives begin with the full stop (period).


                                                                                             7
A2/414 Extension

BEGIN 0 means that the first instruction will be placed in memory location zero. This
denotes the start address of the program. END denotes the end of the program - the point at
which translation finishes.

At the start of the program are listed all the memory locations that are going to be used to
hold data in the program. These are given symbolic names (called symbols) such as NUM1,
NUM2 and ANS in the example above. Symbols are followed by a colon and a value if
required. In the example the memory location called NUM1 will be given the initial value of
4. Data locations are usually found at the end of the program code in memory.

The Assembly Process
The assembly of the assembly language program into machine code can be done by hand but
for long programs this is a slow process. Because assembly language programs follow a strict
set of rules (syntax) in the way they are set out it is a relatively simple task to design a special
computer program which will enable the computer to do the translation for us. Such a
translating program is called an Assembler.

The assembly language program is called the source code and is created in an editor program.
The source code is the input for the assembler which translates into the machine code which
is also known as the object code.

The steps in the assembly process are:
       1. Create the assembly language program (source code) in a text editor and save it.
       2. Load the assembler and input the source code.
       3. Save the object code on disk.
       4. Load and execute the machine code object program.



                                          Input source code



                                                                                Correct
                                                                                errors
             Source
                                                 Editor
             code
                                                                               Error list




                                              Assembler
                                                                               Object
                                                                               code


                                          Load & run object
                                                code


                           System flow diagram for the assembly process

8
A2/414 Extension

In most assemblers the source code is scanned twice. This is called a two-pass assembler.

Pass 1 - Memory addresses are allocated to all symbolic addresses and jump labels. The
results are held in a symbol table.

Pass 2 - The assembler contains a table of all the assembly language mnemonics and their
corresponding binary machine codes. In the second pass the mnemonics and symbols are
replaced with the corresponding binary representations from the two tables to produce the
final object code.

        .BEGIN 0
        NUM1:4
        NUM2:3
        ANS:

0       LDA NUM1
1       ADD NUM2
2       STA ANS
3       HLT
        .END

Assembly of the example program produces the following symbol table after the first pass:
                  Symbol/Label     Actual address         Address
                                                          (Binary)
                 NUM1                     4                 0100
                 NUM2                     5                 0101
                 ANS                      6                 0110

The mnemonic table in the assembler is:
                              Op Code          Mnemonic
                                0000             HLT
                                0001             LDA
                                0010             STA
                                0011            ADD
                                0100             SUB
                                0101             JPU
                                0110             JPN
                                0111             JPZ
                                1000             INP
                                1001             OUP

The second pass produces the following object code:
                 Address          Address        Object Code
                                 (Binary)
                     0             0000           0001 0100        01 04
                     1             0001           0011 0101        03 05
                     2             0010           0010 0110        02 06
                     3             0011           0000 0000        00 00
                     4             0100           0000 0100        00 04
                     5             0101           0000 0011        00 03
                     6             0110           0000 0000        00 00

                                                                                            9
A2/414 Extension


The object code can be loaded into the memory addresses given and then executed.

Things to do
1) The following program inputs two numbers and find their difference. Assemble the
program by produce a symbol table and the object code.

        .BEGIN 0
              NUM1:
              NUM2:
              ANS:

                   INP   NUM1
                   INP   NUM2
                   LDA   NUM1
                   SUB   NUM2
                   STA   ANS
                   HLT
        .END

2) Assemble the following program by producing a symbol table and the final object code.

        .BEGIN 0
              NUM1:0
              NUM2:0

                   INP   NUM1
                   INP   NUM2
                   LDA   NUM1
                   SUB   NUM2
                   JPN   A
                   OUP   NUM1
                   OUP   NUM2
                   JPU   B
        A:         OUP   NUM2
                   OUP   NUM1
        B:         HLT
        .END

        What does this program actually do?
        Dry run the program where the first number input is 2 and the second number is 3.


3) What are the advantages of assembly languages over machine languages?




10

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:558
posted:2/24/2010
language:English
pages:10
Description: Program Instructions - the Machine Language of the Computer