Download presentation
Presentation is loading. Please wait.
1
T. Jumana Abu Shmais – AOU - Riyadh
Agenda Quick Recap of sections 2 and 3 Unit 6: Section 4: Running a program Section 5: Types of programming languages Section 6: The structure of computer programs Unit 7: Part of Section 2: Getting Started What’s Next? T. Jumana Abu Shmais – AOU - Riyadh
2
Quick Recap of sections 2 and 3
Hardware (physical – tangible)/Software (instructions – intangible). The computer is a machine that accepts input, processes that input, and produces output. Central Processing Unit (CPU): Arithmetic Logic Unit (ALU) Control Unit (CU) ALU and CU registers RAM/ROM The Operating System: Manages resources, memory, peripherals, access to CPU, provides interface…etc. Bootstrapping
3
Agenda Quick Recap of sections 2 and 3
Unit 6: Section 4: Running a program Section 5: Types of programming languages Section 6: The structure of computer programs Unit 7: Part of Section 2: Getting Started What’s Next?
4
The Fetch/Execute Cycle
The steps carried out by the CPU in the execution of a computer program: Get (fetch) an instruction from the program. Find and transfer any data necessary to perform instruction. Carry out the instruction (which may involve saving the result for later use). The process of locating, transferring and carrying out a single instruction during execution is called fetch/execute cycle.
5
The Fetch/Execute Cycle
During each cycle the CPU must: Locate the next instruction (or the first, if execution has just started) in the program, which is stored in main memory. Transfer the instruction into the processor by putting it into an appropriate register. Decode the instruction (to see what it is to do). Locate (in main memory) any data the instruction refers to and fetch it into the processor by placing it into a register. Do the processing on the data that the instruction requires. Place the result in an appropriate register. If necessary, move the result back into main memory so that it can be used later in the processing. Update the program counter to hold the memory address of the next instruction.
6
Buses and Processor Clock
Bus: an electronic channel through which data and instructions travel between different components of the computer. Generally at least three categories of bus are needed to ensure that the fetch/execute cycle operates effectively: The address bus: is used to carry information about the addresses in main memory to be accessed. The data bus: The external data bus transmits data between the registers and main memory, The internal data bus transmits data between the different registers within the processor. The control bus: carries control signals sent by the control unit, as well as signals that report the status of various devices. Bus technology is also important for transferring data between the CPU and the input and output devices.
7
Buses and Processor Clock
During the processing of instructions, the order in which events happen is clearly critical. To enable events to be synchronized by the control unit, all computers have a processor clock, which sends out pulses at regular intervals. The length of time between pulses is called the clock cycle time. Historically each instruction took at least one clock cycle to execute but advanced processors can now execute more than one instruction in a single cycle. The number of pulses per second is called the clock frequency or the clock speed, and is measured in megahertz (MHz). So a processor with a 900MHz clock sends 900,000,000 pulses per second. All other things being equal, a computer with a high clock frequency can execute more instructions per second than one with a lower clock frequency.
8
A Look at a Simple Computer Program
The following is a simple program for adding two numbers written in a programming language called assembly language. LDA 501 ADD 502 STO 503 HLT 000 In this simple example, data being processed at any single time is held in a single register called the accumulator.
9
A Look at a Simple Computer Program
operator operand The operator acts upon the operand. LDA 501 address of a memory location LoaD Accumulator part of the computer's memory The Accumulator Before LDA executes ??? 3 ? 2 502 500 503 501 after LDA executes 2
10
A Look at a Simple Computer Program
operator operand ADD 502 address of a memory location ADD to Accumulator The Accumulator part of the computer's memory before ADD executes 2 3 ? 2 502 500 503 501 after ADD executes 5
11
A Look at a Simple Computer Program
operator operand STO 503 address of a memory location STOre Accumulator in memory computer's memory before STO 3 ? 2 502 500 503 501 The Accumulator computer's memory after STO before STO executes 5 3 ? 5 2 502 500 503 501 after STO executes 5
12
A Look at a Simple Computer Program
operator operand HLT 000 dummy value - not used HaLT = End program No effect on accumulator or memory When HLT is executed, the control unit sends a signal informing the CPU that there are no more instructions to be executed in this program.
13
More on Registers Types of registers:
Accumulator: Historically the accumulator was the register used to store any intermediate values during a calculation. Program Counter (CP) or Next Instruction Pointer (NIP): located in the control unit and holds the memory address of the next instruction to be executed. If the program is sequential, this register is incremented by 1 each time an instruction is carried out. Instruction register (IR): located in the control unit, holds the instruction that is currently being executed. It is responsible for decoding the instruction, i.e. separating the instruction into its operator and operand, and interpreting the meaning of the operator.
14
More on Registers Memory address register (MAR): holds the address of a main memory location that needs to be accessed. Memory data register (MDR): is a temporary holding area for either data or instructions that are being transferred between main memory and the other registers. Status registers: hold information about the current status of an instruction. One use of a status register is to flag up or signal a problem. For example, the status register could contain a special code to indicate that the programmer has inadvertently tried to do something impossible, such as divide by zero.
15
Virtual Memory Main memory can be supplemented by a technique whereby an area of the hard disk can be used to simulate additional capacity during processing. This technique is known as virtual memory. So, a relatively small working set of data and instructions are held in main memory at any one time, with the rest being kept in a special area on the hard disk. Data and instructions are swapped in and out of main memory as required, creating an illusion of far more main memory than actually exists. The transfer of blocks of data (called pages) between the main memory and the hard disk is managed by the operating system. Although the use of virtual memory enables a computer to run bigger programs, the speed of execution is reduced due to the increased time needed to access the hard disk.
16
Cache Memory and the Hierarchy of Speed and Cost
Cache memory is faster than main memory, but slower than the registers. Cache memory is ‘fed’ from main memory before the data is finally moved to the registers. When an instruction calls for data, the processor first looks in the cache (cache hits and misses).
17
Program-Controlled Input and Output
Historically the lower speed of input and output relative to data flow within the processor has slowed down the speed at which a computer can process data. To solve this problem, a local Input/Output bus (I/O bus) which is a high-speed bus that connects performance-critical devices, such as video cards, modems and network interface cards, to the main memory and the processor. The most common local I/O bus is the Peripheral Component Interconnect bus (PCI). PCI technology allows the processor to be bypassed altogether when transferring data to and from main memory, resulting in very fast transfer speeds.
18
Agenda Quick Recap of sections 2 and 3
Unit 6: Section 4: Running a program Section 5: Types of programming languages Section 6: The structure of computer programs Unit 7: Part of Section 2: Getting Started What’s Next?
19
Types of Computer Language
Low-level Languages High-level Languages
20
Low-level Languages Machine Language:
A computer can only handle bits, so each instruction in machine language is a long stream of 1s and 0s. Example: Disadvantages of machine language: Slow and difficult for humans to read and write. Programmers have to deal directly with memory management. Machine code programs are difficult to understand and modify. Each family of processor understands only its own machine language. Programs are rarely written in machine language these days. However, since machine language is the language of the processor, all programs, regardless of what language they are originally written in, must be translated into machine language before they can be executed.
21
Low-level Languages Assembly Language:
As with machine language, an assembly language is a language in which each instruction exactly corresponds to one hardware operation of the processor. Any computer programming language with this one-to-one correspondence (including the machine language itself) is referred to as a low-level language. Programming languages where one instruction may correspond to many machine language instructions are referred to as high-level languages. Whenever a program is written in a language other than machine language, the instructions in the original program are called the source code. When the source language is an assembly language the program that translates the code to machine language is called an assembler.
22
Low-level Languages Assembly language programs have a number of advantages over machine language: Operators are given mnemonic names (e.g. ADD). Allow the use of symbolic names for memory locations. Allows the programmer to provide data in forms other than binary and let the assembler worry about translating them. Most assemblers include a facility to print the source program and its machine language equivalent. Assemblers usually notify the programmer of errors. However, assembly language retains some of the disadvantages of An assembly language program can only run on one family of processors. Still difficult to understand by anyone other than the programmer, and so are difficult to modify or update. Programs written in assembly language tend to be very long.
23
High-level Languages High-level languages are easier for humans to read, write, understand and modify, because they are close to English and natural languages. This increases programmers’ productivity and program’s reliability. There are two different mechanisms by which a program written in a high-level language may be translated into machine language: Compilation: a translator program called a compiler translates the source into machine language. The machine language version is then saved and executed every time the program is run. (C, C++ and Java) Interpretation: a translator program called an interpreter translates each instruction in the source code one-by-one into machine language as the program is running, and then immediately executes it. No machine language program is generated or saved. Interpretation results in a slower execution process. (JavaScript, Perl and Basic).
24
Agenda Quick Recap of sections 2 and 3
Unit 6: Section 4: Running a program Section 5: Types of programming languages Section 6: The structure of computer programs Unit 7: Part of Section 2: Getting Started What’s Next?
25
Sequence, Selection and Repetition
For most programming languages the default execution order is sequential – instructions are executed in the order in which they appear in the program. All programming languages have mechanisms for changing the order in which their instructions are carried out. An instruction that changes the default sequential order is referred to as a control instruction. Control instructions are used to support selection and repetition. Selection refers to the process of deciding between two or more courses of action. In repetition the decision determines whether or not one or more instructions are repeated.
26
Selection Selection (sometimes called branching) occurs in a computer program when the instruction or instructions to be executed depend on a condition. Conditions are statements that can be either true or false. In mathematics and computer programming, true and false are sometimes referred to as Boolean values, and a condition that can be evaluated true or false is referred to as a Boolean expression.
27
Selection The if branch (or clause) The else branch (or clause)
28
Single and more than one possibility:
if (condition) Do something Nesting: if (light is red) Stop else if (light is yellow) Get ready Go Red Light Stop Yellow Get ready Green Go
29
Negative Conditions NOT (something that evaluates to true) will give false NOT (something that evaluates to false) will give true In each case the Boolean expression shown in parentheses (round brackets) is evaluated first, to either true or false, and the NOT then has the effect of reversing it to the other Boolean value. NOT is called a Boolean operator because it operates on a Boolean expression. It is also an example of a unary operator, which means that it operates on a single value. Binary operators operate on two Boolean values.
30
Truth Tables The effects of applying Boolean operators to different Boolean values are often shown in tables, usually known as truth tables. AND and OR are binary Boolean operators. A Not A true false A B A AND B true false A B A OR B true false
31
Repetition Repetition, or looping, occurs when we have an instruction (or a sequence of instructions) that we might want to execute more than once. For example, an algorithm for reading a book might contain the following instructions: while (there are pages left to read in the book) read the current page turn the current page over select a new book This is called the while loop. Boolean condition loop body
32
Repetition if care is not taken to ensure that the condition eventually becomes false, the loop body will repeat forever. This is called an infinite loop.
33
Trace Tables A trace table is a way of working out what a program does by tracing execution step by step. (1) set number to the value 2 while (NOT (number is 6)) write out the value of number in the output window move to the next line in the output window add 2 to number write ‘all finished’
34
Compound Conditions A condition that is made up of two or more conditions is referred to as a compound condition. Consider the condition: The shape is neither square nor filled in We can translate this as meaning that the shape is not square and it is not filled in and write it in Boolean notation as: NOT (The shape is square) AND NOT (The shape is filled in) NOT (The shape is square) AND NOT (The shape is filled in) NOT (false) AND NOT (true) true AND false false
35
De Morgan’s Laws De Morgan’s Laws: NOT A AND NOT B = NOT (A OR B)
NOT A OR NOT B = NOT (A AND B) Precedence rules (useful in evaluating compound Boolean expressions): parentheses first then unary operators then binary operators
36
Agenda Quick Recap of sections 2 and 3
Unit 6: Section 4: Running a program Section 5: Types of programming languages Section 6: The structure of computer programs Unit 7: Part of Section 2: Getting Started What’s Next?
37
Introduction The rules of a programming language are called its syntax. JavaScript, as its name suggests, is a scripting language. This means that programs written in JavaScript cannot be run on their own, but have to be run within some application, such as a web browser. JavaScript is an interpreted language. Browsers have embedded JavaScript interpreters. JavaScript was designed to add interactivity to HTML pages. JavaScript is usually embedded directly into HTML pages.
38
A Simple Program <HTML> <HEAD>
<TITLE> Activity_7.2.2 First Program </TITLE> <SCRIPT LANGUAGE = "JavaScript"> document.write('Welcome to programming!') </SCRIPT> </HEAD> <BODY> </BODY> </HTML> Everything between <script> and </script> is written in a scripting language. The LANGUAGE attribute defines the scripting language. JavaScript is the default language, so that if no specific language is specified, HTML will assume that the code is JavaScript.
39
A Simple Program document.write(‘Welcome to programming!’) :
document.write() is a standard JavaScript command for writing output to a page. document is an object consisting of the contents of the current page that will be displayed in a window opened by the browser. write() is a method associated with the document object, which enables JavaScript to add text to the page. The ‘dot notation’ (in document.write()) tells the document to execute the code of its write() method. The parenthesis () enclose the argument of the method. It provides information that the method needs to do its job, which, in this case, is the text to be displayed. The text in the program statement must be enclosed in quotation marks (either double or single quotes).
40
JavaScript Variables A variable is a named location in the computer’s memory where a value is stored. The name of a variable is called its identifier. The value of a variable can change as the program executes. Declaring variables: var myVar; The keyword var is used to declare a variable. myVar doesn’t yet have a value. A group of variables can be declared in one statement var var1, var2, var3;
41
JavaScript Variables Assigning values to variables: myVar = 5;
The assignment operator = is used to assign the value on its right (5) to the variable on its left (myVar): 5=myVar is invalid. You can assign values to the variables when you declare them: var carName=‘Volvo’; If you assign values to variables that have not yet been declared, the variables will automatically be declared. E.g. carName="Volvo"; has the same effect as: var carName=“Volvo”; Assignment examples: myVar = 6; yourVar = myVar + 3; myVar = yourVar;
42
Naming Variables Because JavaScript is case-sensitive, variable names are case-sensitive. Variable names must begin with a letter, the underscore _ character or the $ character. Subsequent characters may include digits, Don’t use a reserved word which is already a part of JavaScript’s vocabulary (such as var, if, for, while…etc.) while naming variables. Avoid very short identifiers such as a, b, x, ch.., because they are not very informative. Choose meaningful names that give some indication of the role played by the variable. Start your identifiers with lower-case letters. When an identifier is composed of more than one word, use single upper-case letter to mark the start of each word. E.g. myFamilyName.
43
Agenda Quick Recap of sections 2 and 3
Unit 6: Section 4: Running a program Section 5: Types of programming languages Section 6: The structure of computer programs Unit 7: Part of Section 2: Getting Started What’s Next?
44
What’s Next? Start working on TMA02, if you haven’t yet.
Read the rest of Unit 7: An Introduction to Programming Using JavaScript
45
Any Questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.