MODULE 1 : WHY PROGRAMMING LANGIUAGES - Overview Programming fundamentals is about knowing how to give instructions in a computer language to the computer system to solve problem, the problem domain. This implies having the basic knowledge of the architecture of the computer machine and its influence on the problem solving process – the instructions to be given. The problem domain equally dictates the solving process and the computer tools, techniques and methods that could be used. Mr. Joseph Ecklu (DCS) Slide 1
Session Objectives At the end of the session, the student will – To enable the user to enlist the help of a computer system in solving problems in the domain of interest – To enable the user to bridge the gap between problem and machine domain Mr. Joseph Ecklu (DCS) Slide 2
Reading List Programming Language Essentials by Bal H.E. and Grune D. (Chapter 1) C.S. French “Computer Science” Fifth Edition (Chapters 13,27, 28) Foundations of Computer Science by Forouzan B. and Mosharraf F (Chapter 9) Mr. Joseph Ecklu (DCS) Slide 3
WHY PROGRAMMING LANGUAGES ? To understand better some of the features found in programming languages AND some of the issues in programming language design It is useful to summarise some well known aspects of the computer machine Mr. Joseph Ecklu (DCS) Slide 4
WHY PROGRAMMING LANGUAGES ? Very broadly a computer machine consists of Common Elements/components -a memory -Central Processing Unit ( CPU ) -Peripheral equipment (Input/Output) Mr. Joseph Ecklu (DCS) Slide 5
WHY PROGRAMMING LANGUAGES ? The Memory contains machine instructions and data -both represented as sequences of bits -grouped into bytes of 8-bits each on most machines - each byte occupies a location in memory and - each location has an address Mr. Joseph Ecklu (DCS) Slide 6
WHY PROGRAMMING LANGUAGES ? The Memory /Program The machine instructions in memory constitute the program At any point in time the machine is in a certain state, determined by the contents of its memory and the state of its peripherals Mr. Joseph Ecklu (DCS) Slide 7
WHY PROGRAMMING LANGUAGES ? The Memory / Program Instruction Pointer ( IP ) very central to the machine’s state it points to the next machine instruction in memory to be executed Flow of control refers to the series of successive values of the instruction pointer manipulating the flow of control is a main issue for many programming languages Mr. Joseph Ecklu (DCS) Slide 8
WHY PROGRAMMING LANGUAGES ? The Memory / Program a John von Neumann Machine a type of machine whose memory can contain both - instructions and - data - and that manipulates the flow control Other machine types exist e.g. data flow machine but the von Neumann machine type is still predominant Mr. Joseph Ecklu (DCS) Slide 9
WHY PROGRAMMING LANGUAGES ? The Memory / Program Program as a sequence of bits A program running on a computer was, and is, a sequence of machine instructions, and thus a sequence of bits Programming the computer by writing this sequence of bits by hand was necessary in the very early days – a very tedious and error prone effort – but was abandoned Mr. Joseph Ecklu (DCS) Slide 10
WHY PROGRAMMING LANGUAGES ? The Memory / Program Program as a sequence of bits Programming using notation was invented to replace programming using bits Assembly language Names were given to - machine instructions - data and - positions in the program Mr. Joseph Ecklu (DCS) Slide 11
WHY PROGRAMMING LANGUAGES ? Figure 1. Shows a short program fragment which counts from 1 to 10 using machine level and low-level notations Machine Code( hex)Assembly language 23fc movl#Ox 1, n compare: 0cb a cmpl#0xa, n 6e0c bgtend…of…loop 06b addl#0x1, n 60e8 bracompare end…of…loop; Mr. Joseph Ecklu (DCS) Slide 12 Fig. 1
WHY PROGRAMMING LANGUAGES ? assembly machine code memory code (hex) location address movl #Ox 1, n 23fc compare 0002 cmpl #0xa, n 0cb a bgt end…of…loop 6e0c0004 addl #0x1, n06b bra compare 60e80006 end..of…loop n Mr. Joseph Ecklu (DCS) Slide 13 Fig. 2
WHY PROGRAMMING LANGUAGES ? The program addresses a memory location called n, Initialises its value to 1 using the assembly instruction ( movl #0x1, n ) ‘move long number 1 to location n’ Mr. Joseph Ecklu (DCS) Slide 14
WHY PROGRAMMING LANGUAGES ? The program then enters a program loop It repeatedly increases the value in n by 1 Until it becomes greater than 10 The loop consists of four instructions Starts by comparing n to the number 10 ( cmpl #0xa, n ) Note : 0xa is hexadecimal notation for decimal 10 Mr. Joseph Ecklu (DCS) Slide 15
WHY PROGRAMMING LANGUAGES ? If the result of this comparison is ‘greater’ the program branches to the location named ( end…of…loop ) which means that the instruction pointer ( IP ) is set to the address of that location – see fig. 2 ( IP = 0007 ) Otherwise the IP is not affected and now points to ( addl #0x1,n ) (IP = 0005 ) which adds 1 to the contents of location n Mr. Joseph Ecklu (DCS) Slide 16
WHY PROGRAMMING LANGUAGES ? A branch instruction ( bra compare ) sets the IP to the address of the location labelled compare ( IP = 0002 ) causing the loop body to be repeated Mr. Joseph Ecklu (DCS) Slide 17
WHY PROGRAMMING LANGUAGES ? Refer to Fig 2. The left column shows the corresponding machine code in hexadecimal notation. The hexadecimal notation is a higher abstraction of the real binary notation of 0s and 1s or bits Mr. Joseph Ecklu (DCS) Slide 18
WHY PROGRAMMING LANGUAGES ? Advantages of Notation Language e.g. Assembly -A big step forward i.e. instead of using bits of 0s and 1s mnemonics such as mov, add, are used - Less cumbersome compared to programming using bits Mr. Joseph Ecklu (DCS) Slide 19
WHY PROGRAMMING LANGUAGES ? Disadvantage of Notation language -Language is machine-specific or machine-dependent Mr. Joseph Ecklu (DCS) Slide 20
WHY PROGRAMMING LANGUAGES ? Machine-independent language the need to have machine-independent programming language led to the development of High-level languages with more problem-oriented notation Mr. Joseph Ecklu (DCS) Slide 21
WHY PROGRAMMING LANGUAGES ? High-level notation Figure 2. Shows a short program fragment which counts from 1 to 10 using high-level notation FORTRAN CodeCOBOL Code DO 2 N = 1, 10 MOVE 1 TO N 2 CONTINUEAGAIN. IF N IS EQUAL TO 10 GOTO END-DEMO ADD 1 TO N GOTO AGAIN END-DEMO Mr. Joseph Ecklu (DCS) Slide 22
WHY PROGRAMMING LANGUAGES ? The Central Processing Unit ( CPU ) it can inspect and modify the memory Mr. Joseph Ecklu (DCS) Slide 23
WHY PROGRAMMING LANGUAGES ? PERIPHERAL EQUIPMENT for human interaction which may include keyboard mouse display unit printer … Mr. Joseph Ecklu (DCS) Slide 24
Session Summary This session has covered the – Importance of computer system in solving problems in variety of areas and domain of interest – Understanding the features found in programming languages programming Mr. Joseph Ecklu (DCS) Slide 25
BASIC COMPUTER ARCHITECUTRE Control Unit C Hi 55 * * * * Bill Auxiliary Storage Input Device c. A.L.U d.. Program Counter b CPU a. address control Trial Questions Mr. Joseph Ecklu (DCS) Slide 26
1. Complete the diagram by joining any missing arrow. 2. Try and identify the parts labeled a, b, c and d. 3. You have just written a program which is currently situated in auxiliary storage. Should you decide to run the program briefly describe how the program will be moved and processed using the diagram above. 4. Address, control and a. are collectively known as Give 2 examples of the part labeled c. Trial Questions Mr. Joseph Ecklu (DCS) Slide 27
Machine Language Assembly Language High Level Language Speedfastest ConvenienceHighest Portability Difficulty Level medium Trial Questions Mr. Joseph Ecklu (DCS) Slide 28
Consider the following codes. Specify which one is machine, assembly or high level a. movl #0x1, n b. 23fc c. if (a > b ) { a = b ; } Trial Questions Mr. Joseph Ecklu (DCS) Slide 29