MODULE 1 WHY PROGRAMMING LANGIUAGES Overview Programming fundamentals





























- Slides: 29

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 instruction pointer manipulating the flow of control is a main for many programming languages Mr. Joseph Ecklu (DCS) Slide 8 the issue

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 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 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 23 fc 0000 0001 0000 0040 movl #Ox 1, n compare: cmpl #0 xa, n bgt end…of…loop addl #0 x 1, n bra compare end…of…loop; 0 cb 9 0000 000 a 0000 0040 6 e 0 c 06 b 9 0000 0001 0000 0040 60 e 8 Fig. 1 Mr. Joseph Ecklu (DCS) Slide 12

WHY PROGRAMMING LANGUAGES ? assembly code movl machine code memory (hex) #Ox 1, n location address 23 fc 0000 0001 0000 0040 0001 0 cb 9 0000 000 a 0000 0040 bgt end…of…loop 6 e 0 c addl #0 x 1, n 06 b 9 0000 0001 0000 0040 bra compare 60 e 8 end. . of…loop 1 0000 0001 0002 0003 0004 0005 0006 0007 n compare cmpl #0 xa, n Fig. 2 Mr. Joseph Ecklu (DCS) Slide 13

WHY PROGRAMMING LANGUAGES ? • The program addresses a memory location called n, Initialises its value to 1 using the assembly instruction ( movl #0 x 1, 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 #0 xa, n ) Note : 0 xa 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 #0 x 1, 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 0 s and 1 s 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 0 s and 1 s 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 Code DO 2 N = 1, 10 2 CONTINUE COBOL Code MOVE 1 TO N AGAIN. 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

Trial Questions BASIC COMPUTER ARCHITECUTRE b C Hi 55 * * Bill Mr. Joseph Ecklu (DCS) CPU address Control Unit a. A. L. U control Input Device c. d. . Program Counter Auxiliary Storage Slide 26

Trial Questions 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. . 5. Give 2 examples of the part labeled c. Mr. Joseph Ecklu (DCS) Slide 27

Trial Questions Machine Language Speed Assembly Language fastest Convenience Highest Portability Difficulty Level Mr. Joseph Ecklu (DCS) High Level Language medium Slide 28

Trial Questions Consider the following codes. Specify which one is machine , assembly or high level a. movl #0 x 1, n b. 23 fc 0000 0001 0000 0040 c. if (a > b ) { a = b ; Mr. Joseph Ecklu (DCS) } Slide 29