Download Programming Languages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CH/S5CS/Jan. 2002
Computer Operation
Central Processing Unit (CPU)

The CPU is a device to accept and execute sequentially the instructions stored in the computer’s main memory.

The CPU is composed of
( ALU ).

A CPU may be built on a single integrated circuit chip which is called a

Inside the CPU, there are special memory cells called
registers . They are used by the CPU for
temporarily storing data or instructions during the execution of an instruction.

The data
CPU.
bus,
control unit
address bus and
( CU ) and
control
arithmetic and logic unit
microprocessor
.
bus are the main communication links within the
Control Unit


Its functions are:
i.
to control the sequence in which the instructions are executed,
ii.
to control the flow of data between the ALU and the memory, and between the peripherals and the memory,
iii.
to interpret the instructions,
iv.
to regulate the timing of all operations done within the processor, and
v.
to send control signals to, and receive control signals from peripheral devices.
It uses the following registers:
i.
Instruction Register (IR). It holds the instruction fetched from main memory while the instruction is being
interpreted and executed.
ii.
Program Counter (PC). It holds the address of the next instruction in main memory to be executed.
Normally, the content of the PC increases by one after each instruction executed. (What will be the
exception?
Branching – if..then and case..of in Pascal
)
iii.
Memory Data Register (MDR). Data to be processed are read from main memory to MDR and Data to be
stored to main memory are read from CPU to MDR temporarily.
iv.
Memory Address Register (MAR). It holds the address of the data or instruction to be processed.
Arithmetic and Logic Unit (ALU)
i.
It carries out all arithmetic and logic operations.
ii.
Arithmetic operations include,
a. basic operation : +, -, *, /
b. comparison : >, <, =, >=, <=, <>
iii.
Logic operations include: NOT, AND, OR, NAND, NOR
iv.
It has the register accumulator (AC). It holds the output of the ALU.
COMPUTER OPERATION
page 1
CH/S5CS/Jan. 2002
address
0000
0001
0002
0003
0004
0005
0006
.
.
.
CPU
CU
PC
ALU
ACC
IR
MAR
PSR
MDR
Internal CPU bus
Address bus
Data bus
Control bus
Figure 1
Machine Code

Suitable instructions must be input and stored into the main memory before the actual processing to be executed.

The instructions are written in machine-readable (i.e. thus binary form) codes.

The instruction should be able to instruct the CPU
i.
on what operation should be performed, and
ii.
where the data is to be found.

Thus each instruction is divided into 2 parts, operation code (or function code, or opcode) and operand (or
operand address).

Operation code. This part specifies the function or operation to be performed (e.g. add, load, jump, ..., etc.).

Operand address. This part specifies the data (operand) or the locations of data to be processed.

Each computer CPU has its own machine-readable instruction set, which has a particular format. The length of
an instruction may also vary between different machines.
Sample instruction set (of an imaginary simple computer)
instructions (8 bits)
opcode (3 bits)
operand / operand
add. (5 bits)
000
r
001
r
010
r
011
r
100
r
101
r
110 *
0
111 *
0
COMPUTER OPERATION
function performed
add number in address r of main memory to number in accumulator
subtract number in address r from number in accumulator
multiply number in accumulator by number in address r
divide number in accumulator by number in address r
load the number from address r of main memory to accumulator
store the number in accumulator into address r of the main memory
input value from the input unit into accumulator
output the number from accumulator to output unit
page 2
CH/S5CS/Jan. 2002
*
Instructions 7 and 8 are so straightforward that neither data nor address is needed to be specified. Thus, the
value zero is placed in the operand part.
i.
The length of each instruction is 8 bits. The first 3 bits are opcodes and the rest are the addresses.
ii.
Each opcode represents one particular operation to be performed by the computer.
iii.
3 bits opcode  8 different opcodes (23 = 8)
Here are two sample machine code programs using the above instruction set:
program instruction
memory
location
opcode
operand
address
remark
00000 (0)
110
00000
00001 (1)
00010 (2)
101
110
01111
00000
00011 (3)
000
01111
00100 (4)
111
00000
input the first number (say “2”) into accumulator; the computer will
wait until data signals are received.
Store “2” from accumulator into location 15 in central memory.
Input the second number (say “5”) into accumulator; the computer will
wait until input data signals are received.
add “2” from location 15 in main memory to “5” in accumulator, the
result “7” is now in accumulator.
output the result “7” to the output device.
memory
location
machine code
content of
Accumulator
remark
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11000000
10101111
11000000
10101110
11000000
10101101
11000000
10101100
00001101
00001110
01101111
10101011
11100000
3
3
6
6
8
8
10
10
18
24
8
8
8
input ‘3’
store ‘3’ into location 15
input ‘6’
store ‘6’ into location 14
input ‘8’
store ‘8’ into location 13
input ‘10’
store ‘10’ into location 12
add ‘8’ to accumulator
add ‘6’ to accumulator
divide ‘24’ by ‘3’
store ‘8’ into location 11
output ‘8’ to output device
Program Execution

Each machine instruction is executed in 3 steps (cycles): fetch cycle, decode cycle and execution cycle. (FetchDecode-Fetch Cycle)

For the fetch cycle, (a register enclosed by a square blanket, say [reg.], means content of the register)
F1 :
MAR  [PC]
Copy the address of the instruction from PC to MAR.
F2 :
MDR  [[MAR]]
Fetch the instruction from the location specified by the address in MAR and store it in MDR.
COMPUTER OPERATION
page 3
CH/S5CS/Jan. 2002
F3 :
IR  [MDR]
Load the instruction from MDR to IR.
F4 :
PC  [PC] + 1
Increase the value in PC by 1

For the decode cycle, the
instruction in IR.

For the execute cycle, the control unit sends control signals to the appropriate devices to carry out the
instruction.
in control unit decodes the opcode from the
** Sample instructions
i.
Load the content in address 1234 into AC:
MAR  1234
MDR  [[MAR]]
c. AC  [MDR]
a.
b.
ii.
Store the content in AC into memory location 1234:
MDR  [AC]
MAR  1234
c. [MAR]  [MDR]
a.
b.
iii.
Add the content in memory location 1234 to the value in AC, store the result into AC:
MAR  1234
MDR  [[MAR]]
c. AC  [AC] + [MDR]
a.
b.
iv.
Branch to instruction at location 1111:
a.
PC  1111
Programming Languages
Machine Languages

It is a language which is the only language that a computer CPU can read, i.e. the machine instruction set.

All other programming language programs must be translated into machine language before they can be
executed.
Assembly Language

It is tedious for a programmer to write a machine code program because the programmer is required to memorise
the opcode for each instruction and the actual address location for each variable addresses.

More meaningful letter symbols or semi-word  mnemonics  is used for the binary machine opcodes and
variable addresses.

The language written in these mnemonics is called the
.
Usually, each
mnemonic in the assembly language has one to one corresponding in the machine language instruction set, i.e. if
you find one assembly language instruction, it will match with one and only one machine language instruction,
and vice versa.

Assembly language is machine-oriented, and thus machine dependent.
COMPUTER OPERATION
page 4
CH/S5CS/Jan. 2002

An
codes.
is a program that translates an assembly language program into the equivalent machine
High Level Languages

Assembly languages and machine languages both relate closely to the architecture of the processor and hence are
machine dependent. We call such languages as low level languages.

There are problems in using low level languages:



i.
Details knowledge about the registers and architecture of a particular machine is necessary. (More effort
required)
ii.
More attention should be drawn from the operational aspects of the CPU.
iii.
They are machine dependent.
Advantages of high level language.
i.
They are easier to learn and faster to write.
ii.
It is easier to add documentation into the program so as to make debugging and testing easier.
iii.
The throughput of programmers can be increased.
iv.
Each language has its own rules and syntax to fulfil different needs.
v.
They don
vi.
Programs written in high level languages for one computer can be run on another. That is, these programs
are
.
vii.
Because of the
of programs, a program library can be set up. Standard programs
performing specific jobs are available.
go into the details of the structure of computers.
Disadvantages of high level languages
i.
There is a set of very complicated rules and syntax which must be learnt fairly well before one can write an
error free high level language program.
ii.
There is usually a special purpose in the design of any high level language. Choosing the wrong languages
for the purpose will make programming much more difficult than it should be.
iii.
Because of the standard subroutines taken from the library during compilation, complied object programs are
usually larger and run slower than machine language programs.
iv.
In order for the programs to be run on other computers, special features of particular computer have to be
ignored in writing portable high level language programs.
5 common types of high level languages
i.
Commercial languages, e.g. COBOL, RPG
ii.
Scientific languages, e.g. ALGOL, FORTRAN, BASIC
iii.
Special purpose languages, e.g. ADA, CSL
iv.
Command languages for operation system, e.g. JCL
v.
Multipurpose languages, e.g. PL/1, LOGO, C, Pascal
COMPUTER OPERATION
page 5
CH/S5CS/Jan. 2002

Comparison between the low and high level languages
low-level language
high-level language
1. Machine dependence
Yes
Not usually
2. Detailed knowledge of machine in use required
Yes
Not usually
3. Access to basic machine features
Yes
Not usually
4. Program preparation costs (coding, debugging and
documentation
Higher
Lower
5. Translator
Assembler
Compiler or interpreter
6. Object program
Shorter and faster
Longer and slower
7. Equivalent machine code instruction
One-to-one
One-to-many
Assembly Language Examples
Sample 1:
Opcode
Mnemonics
Meaning
Address
(dec. bin.)
Mnemonic
Machine
code (bin.)
Meaning
000
BRK
Stop
12 01100
LDA 18
001 10010
Load contents of cell 18 to Ac
001
LDA
Load Accumulator
13 01101
CMP 19
101 10011
Compare Ac with contents of
cell 19
011
STA
Store Accumulator
14 01110
BPL 16
110 10000
If larger go to instruction 16
101
CMP
Compare Accumulator
15 01111
LDA 19
001 10011
Otherwise load contents of 19
to Ac
110
BPL
Branch if Positive
16 10000
STA 20
011 10100
Store contents of Ac at cell 20
17 10001
BRK
000 00000
Execution stops
Sample 2:
opcode
mnemonic
function performed (operand r)
001
LDA M
LoaD Accumulator with memory
010
STA M
Store Accumulator in memory
011
ADD M
ADD memory to accumulator
100
SUB M
SUBtract memory from accumulator
101
DEC M
DECrement memory by 1
110
JMP X
unconditional JuMP
111
BNE X
Branch if NEgative
000
STP
SToP
COMPUTER OPERATION
page 6
CH/S5CS/Jan. 2002
Rewrite the following machine program in the ASSEMBLY language defined above:
Address
machine code
10000
00111111
10001
10011110
10010
11110101
10011
00111111
10100
11010110
10101
00111110
10110
01011101
10111
00000000
assembly instructions

Suppose the values in memory location 11110 and 11111 are 4 and 3 respectively, find the final value in
memory location 11101.

What is the main function of the program?
Translators

A translator is a special system program which translates the program written by human into machine language.

The program to be translated is often called
program .

Three types of translators
i.
ii.
iii.

program while the resulting program is called
Assembler
a.
Translate assembly language to machine codes.
b.
It must finish translating the whole program before the program can be executed.
Compiler
a.
Translate high level language to machine codes
b.
It also must finish translating the whole program before the program can be executed.
Interpreter
a.
Translate high level language to machine codes.
b.
It usually translates one high level instruction and then executes it immediately, and so on. The
interpreting process follows the logical sequence of the source program (e.g. BASIC interpreter).
Comparison between the translators
i.
The works of compilers or interpreters are more complicated than those of assemblers.
ii.
Once the object program is produced by the compiler and assembler, the execution of the program is very
fast as compared with executing one using an interpreter.
COMPUTER OPERATION
page 7
CH/S5CS/Jan. 2002
iii.
The interpreter handles one source statement at a time  useful in debugging.
iv.
Interpreter is useful in simulating one computer to another (i.e. it makes one computer work like another).
v.
If the source program has to use again and again, using an assembler or a compiler to produce an object
program make more sense.
vi.
Sometimes, a compiler only translates the source program into a low level language program (e.g. assembly
languages).
Source programs
Translators
High level
language programs
Assembly programs
Object programs
Compiler
Interpreter
Machine codes
Assembler
Fig. 2 Three types of translators
EXECUTION
Trends of Program Language

Computer scientists found that programs should be written in a structured, modular way which can be
understood and debugged easily.

Constructs like IF...THEN...ELSE, FOR...NEXT, WHILE...WEND, DO...LOOP, SELECT...CASE, etc. are
considered basic to a structured language.

Structured programming led to 2 trends:
i.
Object-oriented. Program have fully debugged should be reusable. Emphasis is on regarding procedures
and data structure as a whole (object). The approach led to object-oriented programming (OOP).
ii.
Event-oriented. Another trend is event-oriented, especially in the Windows environment where a single,
double or triple to click initiates a procedure.
Operating System
System Programs and Application Programs


Application Program
i.
is also called user program.
ii.
perform different tasks and solve user’s problems.
iii.
e.g. Word, Excel, FoxPro.
System Program
i.
is supplied by computer manufacturer and software suppliers.
ii.
raise efficiency and process capability.
iii.
e.g. All compilers, interpreters, assemblers and operating systems.
iv.
make computer more useful and easy-to-use.
COMPUTER OPERATION
page 8
CH/S5CS/Jan. 2002
The Need of Operating System

An operating system (O.S.) is a collection of programs for controlling the resources of a computer system and
provides an interface between the computer and the users or the application programs.

The advantages of using an O.S.
i.
Computer can be operated more effectively and reliably.
ii.
Users need not know the complex operating procedures of the system hardware. Instead, they have to know
only simple commands recognised by the O.S. e.g. DIR/W.
Functions of an O.S.
 Scheduling of Jobs. Jobs are selected for processing according to their priorities.

Control of input/output. The O.S. allows the users to share the resources, input / output devices by
controlling over the selection and operating input / output devices for each user and arrange the users in queue.

Primary Storage Management. The O.S. optimises the primary storage usage, especially in case of
multiprogramming  several programs are begin active executed in the same time in a computer system.

Handling errors. When errors occur, the O.S. makes system interrupts, processes corrective routines where
possible and gives error messages.

Communication with the user. There is a group of system programs which can be used through system
commands, e.g. COPY, DIR.

Keeping a log of all programs. The O.S. for minicomputer and mainframes usually keep a complete
record of what has happened during processing.
Components of an O. S.



Control Programs. They are responsible for
i.
Initialisation of the system. It includes booting system, initialisation of primary storage and check out
communication links.
ii.
Control of the program processing. It is handled by job control programs to perform the function of job
scheduling, I/O devices control, error handling and program log.
System service program
i.
are usually found in minicomputers and mainframes.
ii.
include linkage and library programs.
Processing programs. including language translators and utility programs. (The utility programs perform
various useful routine services.)
Bootstrapping (booting) means:i.
transferring the system loader program from ROM into primary storage; and
ii.
loading the master control program (O.S.) into the primary storage and starting overall control of the
system.
COMPUTER OPERATION
page 9