3.5 Programming paradigms

Slides:



Advertisements
Similar presentations
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Advertisements

Instruction Set Architecture Classification According to the type of internal storage in a processor the basic types are Stack Accumulator General Purpose.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
1BA3 G Lacey Lecture 51 Evaluating mathematical expressions  How do computers evaluate x + y or any mathematical expression ?  Answer : “Reverse Polish.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Instruction Set Architecture & Design
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Introduction to a Programming Environment
Implementation of a Stored Program Computer
Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Evolution of Programming Languages Generations of PLs.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Computer Architecture and the Fetch-Execute Cycle
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
Objective At the conclusion of this chapter you will be able to:
Programming Paradigms Backus Naur Form and Syntax Diagrams.
Chapter 5 A Closer Look at Instruction Set Architectures.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
Represents different voltage levels High: 5 Volts Low: 0 Volts At this raw level a digital computer is instructed to carry out instructions.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.
CHP-3 STACKS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
Chapter 10 Programming Fundamentals with JavaScript
Displacement (Indexed) Stack
GCSE COMPUTER SCIENCE Computers 1.5 Assembly Language.
Chapter 6 JavaScript: Introduction to Scripting
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
F453 Computing Questions and Answers
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Revision Lecture
Data Structures Interview / VIVA Questions and Answers
A Closer Look at Instruction Set Architectures
William Stallings Computer Organization and Architecture 8th Edition
Stacks Chapter 4.
Backus Naur form.
Stack Data Structure, Reverse Polish Notation, Homework 7
About Instructions Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter PHY 201 (Blum)
BIC 10503: COMPUTER ARCHITECTURE
Chapter 10 Programming Fundamentals with JavaScript
PROGRAMMING PARADIGMS
CSCE Fall 2013 Prof. Jennifer L. Welch.
Chapter 8 Central Processing Unit
Programming paradigms
ECEG-3202 Computer Architecture and Organization
R.Rajkumar Asst.Professor CSE
Computer Architecture and the Fetch-Execute Cycle
A Closer Look at Instruction Set Architectures Chapter 5
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
CSCE Fall 2012 Prof. Jennifer L. Welch.
UNIT V Run Time Environments.
Mastering Memory Modes
ECE 352 Digital System Fundamentals
CSC 143 Java Applications of Trees.
COMPUTER ORGANIZATION AND ARCHITECTURE
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

3.5 Programming paradigms Part 3

Declarative Languages In declarative languages, the programmer can simply state what is wanted, having declared a set of facts and rules. In the following example coded in prolog, the query male (x) searches and returns all those that are male. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Example: We have the following facts. female(jane). female(anne). female(sandip). male(charnjit). male(jaz). male(tom). parent(jane,mary). parent(jane, rajinder). parent(charnjit, mary). parent(charnjit, rajinder). parent(sandip, atif). parent(jaz, atif). The output of male(x) will be X = charnjit X = jaz X = tom No “No” means that there are no more results. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Goals Looking in the database for male(X) is known as looking for a GOAL A goal is a statement that we are trying to prove whether or not it is True or False. If we add the rule father(X, Y) :- parent(X, Y), male(X) This rule states that X is father of Y if (the :- symbol) X is a parent of Y AND (the comma) X is male. This means that father is a predicate, and X & Y are arguments VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Instantiation Instantiation means assigning a value to a variable. Example: The query male(X) instantiates X to charnjit. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Backtracking Backtracking is the going back to a previously found successful match in order to continue a search. If the goal (search) fails on the last part of a rule the language will backtrack to where the failed variable was first found and it will move the variable onto the next record. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Example: Consider female(jane). female(anne). female(sandip). male(charnjit). male(jaz). male(tom). parent(jane,mary). parent(jane, rajinder). parent(charnjit, mary). parent(charnjit, rajinder). parent(sandip, atif). parent(jaz, atif). father(X, Y) :- parent(X, Y), male(X). The last rule states that X is father of Y if (the :- symbol) X is a parent of Y AND (the comma) X is male. To find the father of rajinder (goal) we use the rule father(X, rajinder) X is instantiated to jane, and the rule male(jane) is applied, which gives a False. Prolog backtracks to find a match for the rule male(x) until it becomes True at Charnijt. Now prolog applies again the whole rule which returns a TRUE so it outputs Charnjit. Then the program continies looking for any other matches, and finds none, so it returns No VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Low level languages Low level languages represent binary with mnemonics, such as… ADD = 001 (a command) NUM1 = 00101 (an address) An Assembler translates the mnemonics (using a table) to produce machine code VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Memory addressing techniques An instruction in memory consists of an opcode and an operand. The operand is the data on which the operation should be applied. The method used to locate said data is known as memory addressing. There are 5 major techniques of memory addressing Immediate addressing Direct addressing Indirect addressing Indexed addressing Relative addressing

Immediate addressing Immediate addressing is so-named because the value to be stored in memory immediately follows the operation code in memory. For example, if you wanted to add 3 to the contents of the accumulator, this would simply be: ADD 0011 The opcode is ADD and the actual data is 0011 (binary 3) Advantage It is very fast since the value to be loaded is included in the instruction. Disadvantage The data cannot be used by other instructions In our example, the value 3 would have to be repeated in every instruction that needs it It is difficult to change the value.

Direct Addressing Direct addressing points to a location in memory that holds the data. E.g. ADD 0101 means that “go and find whatever is in memory address 5 and add it to the accumulator.” Advantage: It is simple to use because you don’t need to know the data in the memory location Disadvantage The number of memory addresses that can be accessed is limited by the number of bits available for the operand. If the operand was a 16 bit number, 216 memory locations can be addressed giving us 64KB of memory which isn’t a lot.

Indirect addressing Indirect addressing points to a memory location that holds another memory location that holds the data to be used. E.g, ADD 1011 means “go to memory location 11 where you’ll find another memory location; go to this other address and use the value found there” Advantage It allows access to larger memory addresses as the full size of register is used for an address.

Indexed Addressing Indexed Addressing uses a special register called the index register The value of the index register is incremented each time it is referred to The memory address given in the instruction is then adjusted according to the value of the register. Advantage The same instruction can be repeated but applied to a different memory location each time. This is useful for processing an array, which would be stored in a contiguous block of memory.

Indexed addressing: example suppose we wanted to add up every number in an array. Normally, we would execute: ADD0001 ADD0010 ADD0011 etc. However, the operator is the same (“ADD”), only the operand changes. This means we could repeat the same instruction and use the index register to increment the memory address.

Relative addressing When we write a program, we don’t know where in memory it will be held during execution, but we do know that it will go into memory as a single block. In relative addressing, all memory addresses are relative to the first one. Example: **** If the first instruction in a program is ADD 100, all subsequent addresses will be relative to 0100. This means that if the next instruction is ADD 010, the memory address that is accessed is 100+010=110.

Methods of defining syntax Each language uses a different set of rules to define its syntax. E.g. a Visual Basic compiler would not understand C++ syntax and vice versa. The RULES of a language are specified using Backus Naur Form (BNF) or syntax diagrams.

Backus – Naur Form (BNF) It is an internationally accepted notation language to define the syntax of a system or programming language. Note that It is necessary to unambiguously define the syntax of a computer language. Suppose we wanted to define a digit using BNF. we say <digit> ::= 0|1|2|3|4|5|6|7|8|9 Brackets < > mean ‘can be defined further’ ::= means ‘is defined to be’ | means ‘or’ We could then define an integer as follows. <integer> ::= <digit> | <digit><integer> This allows an integer to be a single digit or a digit followed by an integer. This part of the definition is recursive. It allows any number of digits. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi BNF Example. Defining of a staff code being entered into a database. <DIGIT> ::= 0|1|2|3|4|5|6|7|8|9 <LETTER> ::= A|B|C|D|E <STAFF_CODE> ::= <LETTER><DIGIT> | <STAFF_CODE><DIGIT> This means “a staff code can be a letter and then any number of digits.” SO D4, E644678, A564 would all be valid staff codes. The following examples would be invalid staff codes: Hf – because H is not defined to be a letter d4 – because a lowercase ‘d’ is not defined to be a letter A56E – because you can only have a letter at the beginning of a staff code AD574 – because the definition only allows a single letter at the beginning of a staff code. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi BNF Example 2 Defining a variable (which is which is a sequence of one or more characters starting with a letter) <variable> ::= <letter>|<variable><character> <character> ::= <letter>|<digit>|<under-score> <letter> ::= <uppercase>|<lowercase> <uppercase> ::= A|B|C|D|E|F|G|H|I|J|K|ZL|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z <lowercase> ::= a|b|c|d|e|f|g|h|i|j|k|zl|m|n|o|p|q|r|s|t|u|v|w|x|y|z <digit> ::= 0|1|2|3|4|5|6|7|8|9 <under-score> ::= _ VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Syntax diagrams Syntax diagrams are diagrammatic way of representing a BNF definition. The staff code example from slide no. 18 would be represented as follows LETTER DIGIT STAFF_CODE VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Example 3: Signed integer BNF for Integer (signed or unsigned) <integer> ::= <unsigned integer>|<signed integer> <signed integer> ::= + <unsigned integer>| - <unsigned integer> <unsigned integer> ::= <digit>|<digit><unsigned integer> <digit> ::= 0|1|2|3|4|5|6|7|8|9 To add a sign to the integer you just add signs ! Syntax diagram for signed integer VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Digit Digit + -

Reverse Polish notation Reverse Polish Notation is an unambiguous method of writing mathematical expressions without brackets. Adding two numbers is written as A + B This is called infix notation as the operator is in between the operands To multiply the result by two, we’d write 2 * (A + B) We need brackets to make sure it’s done in the right order. Polish notation avoids brackets by putting each operator before its operands. A + B would become +AB and 2 * (A + B) becomes *2+AB Reverse polish notation is simply polish notation written the other way around. So those examples would be AB+ and AB+2*. This can be read as ‘take the numbers A and B and add them together, then take the result and the number 2 and multiply them together.’ Reverse polish notation puts each operator after its operands. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Converting between Reverse polish and Infix notations Binary tree diagrams can be used to convert between normal infix notation and reverse polish notation. A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi

Traversing a binary tree Traversing a tree: IN-ORDER (infix) Using this method, we must visit the tree in this order: Visit the left sub-tree. Visit the root node. Visit the right sub-tree. So we would have DBEAC Traversing a tree: POST-ORDER Using this method, need to: Using our binary tree, the order that we would be: D E B C A

Converting… Consider the: 3 * (6 + 2) - 4 / (3 + 7) If we wanted to get the infix notation from a binary tree, we would follow this algorithm, which is known as 'in-order': 1) Traverse the left sub-tree 2) Visit the root 3) Traverse the right sub-tree This would give us: 3 * (6 + 2) - 4 / (3 + 7)

Converting If we wanted to get the reverse polish notation, we would follow this algorithm, which is known as ‘post-order': 1) Traverse the left sub-tree 2) Traverse the right sub-tree 3) Visit the root This would give us: 3 6 2 + * 4 - 3 7 + /  

Using stacks Stacks are also associated with reverse polish notation. They can be used to evaluate expressions using the following algorithm Read expression from left to right If a number is encountered, PUSH it onto stack If an operator is encountered, POP two number from stack and carry out operation PUSH result onto stack End if last item in expression has been dealt with.

Example: using stacks 1 2 We have the expression 6 4 5 + * 25 2 3 + / - The numbers 6, 4 and 5 are encountered so they are PUSHED to the stack one by one. Then an operator is encountered: + The top two numbers are POPPED (4 and 5) and the operation carried out on them, the result is PUSHED to the stack (9) Another operator is encountered: * This is applied to the two items POPPED from the stack (9 and 6). The result is PUSHED onto the stack The numbers 25, 2 and 3 are PUSHED onto the stack as they are encountered The ‘+’ is encountered so the 3 and 2 are POPPED, added together and the result is PUSHED (5) The 5 and 25 are POPPED and divided (notice the order of the operands) and the result, 5, is PUSHED. Finally, the 5 and 54 and POPPED and subtracted and the result is PUSHED back onto the stack. This is our final answer. Again, notice that the first operand to be POPPED is placed to the right of the operator i.e. 54-5. 4 3 6 5 7