CMPE003
Personal Computer Concepts: Software and Hardware
Spring 1996
Lecture 12
Chapter 9 (Computing Unbound)
Class Notes - Lecture : Draft 1
Chapter 9 Transition to Pascal
Basic Perspective:
Code: instructions written in an actual programming language, like Pascal
Pseudo-code: informal algorithmic notation in English and code
*Note: Remember that an Algorithm is a set of instructions in sequence
that solve a problem. The machine "thinks" sequentially and linearly.
The advantage of Pascal is that pseudo-code is similar to the actual
coding syntax of the language.
Also read carefully the definitions in the book.
Pascal Structure:
There are four elements in each program and they must be in the correct
order.
1. program title
2. variable declarations
3. procedure declarations
4. main program code
For example;
program myfirstone; {this is 1 above}
var {2 above, note the variables}
x : real; {are declared with thier }
y : real; {datatypes also listed }
area: real;
counter : integer;
name : char;
procedure Find_Area(a:real,b:real); {the procedure would have }
{code to find the area and }
begin {it would accept 2 variables}
... {that would fill the parameters}
... {x and y that are local within}
end. {the procedure Find_Area }
{the begin and end correspond}
{to the main program where the}
{procedure can be called }
The program name myfirstone could also be written my_first_one for better
clarity, but no spaces are allowed. The variable declarations section
tells the program that is translating the code (the compiler)
what variables it has and what types they are. The datatype , real, is a
floating point number and corresponds to a number with a decimal point.
An integer is a number without a decimal. The parameters in the procedure
declarations are only known within the procedure. Also note the period at
the last reserved word (end.) don't forget it!
Program Content:
syntax: rules of construction; grammer
statement: an individual instruction always followed by a semicolon
Syntax is very formal, ordered, and precise. Code must follow the correct
syntax perfectly, otherwise the translator won't be able to translate the
program.
compound statement:
begin
instruction - 1
....
....
instruction - n
end;
Note this end has a SEMICOLON not a PERIOD. This end refers to the end of
the compound statement not the end of the program.
Datastructures:
decisions(conditional tests)
repetition(loops)
etc.
The "if" statement: if then else
where if,then, else are reserved words. For example the statement:
if testscore = 100
then grade := 'A';
this would test if testscore was equal to 100 and if it is the grade
would be assiged A.
The repeat loop is a repetitive loop. The basic structure is the following:
repeat
until
The body of the loop is executed until the condition is false.
EXAMPLE PROGRAM:
Program Divisors;
uses crt; {crt contains the graphic code and other programs}
var
number,divisor : integer;
begin
Clrscr;
repeat
read(number);
if number > 0 then
begin
writeln('the divisors of' ,number, 'are:');
for divisor := 2 to (number -1 ) do
if number mod divisor = 0
then writeln(divisor)
end
until number <=0
end. {Divisors program}
The looping datastructure is the repeat loop. Mod is a function that only
looks at a remainder; therefore, 4 mod 2 = 0. The spacing is done for the
programmers clarity, the translator ignores all the spaces.The if
statement is executed if the condition (number > 0) is true, then the
compound statement (begin...end) is executed.
So if the user put inputted the numbe 15, then the output would be the
following:
15
the divisor's of 15 are:
3
5
Lecture 13
Chapter 15 (Computing Unbound)
PROJECTS:
By next thursday(5-23) alternative project proposals are due. By next
tuesday(5-21) project ideas to be considered will be offered. If you or your
group have an alternative project idea, send mail to Steve Petersen.(His
e-mail address is petersen@cse.ucsc.edu)
For project ideas, try to harmonize with your field of study. Think
creatively about how to coordinate your interests with a project. Pascal
projects are encouraged, but spreadsheets, databases, or a combination of
are permitted. Groups of two are highly encouraged, but up to four would
be considered;however, be prepared for each person in the group to defend
the project proposal independently. The group projects will be held to
different standards then an individual. Work among the group must be
equitably distributed. Take the projects seriously, students in the past
have gained a lot from these projects.
REVIEW:
According to the syllabus, class is slightly ahead of schedule so lets
review the programming introduced up to this point.
Pseudocode: An informal algorithmic notation with a mixture of english
and code.
Code: The implementation of an algorithm in a particular language.
Four Components of Algorithm Structure
1. Title
2. Variable declarations
the compiler (also called the translator) needs to know about the
formal declaration of the variables that the program will use
3. Procedure declarations
the compiler needs to know which procedures exist
for example: in lab the programs have the line uses crt, this is a
built in procedure made available to the program from a library of
functions. It is also a formal declaration
4. Main program
implementation of the algorithm is contained within the main program
Some useful definitions: (the definitions in the book are very good
reference and you will need to know them)
Syntax: the rules of order and constuction
Semantics: the implementation of the algorithm...what the programmer
intends the program to do
A program may have no syntax errors, thus it will compile; however,
it can have semantical errors. Errors of syntax are usually easy to find,
but semantical errors are difficult to find because the problem is in the
algorithmic thinking. The compiler can only find syntax errors not
semantical errors.
For example, if a period is left off a line then a syntax error will
occur. In lab if the programmer calls the sky function after the cloud
function the clouds get covered with sky and are no longer visible. This
is a semantical error, the functions still work, but the algorithmic
problem is not being solved correctly.
Reserved words: Words the program uses; therefore, the program "reserves"
them for it's use only. If the program trys to use a
reserved word a syntax error will occur.
examples:
var
title
begin
end
Datatypes: Each variable in a program is assigned a datatype in the
formal declarations.
examples:
char: a single character that has an ASCII representation
integer: a non-decimal number including 0, + or - 1,2,3...
real: a number with a decimal point including 0
boolean: contains two values, either true or false
Often ASCII is called text as well. The original ASCII is 7-bit which
corresponds to 128 values (2^7=128). The extended ASCII is 8-bit which
corresponds to 256 combinations. Page 525 lists the ASCII code.
Quick question? How much space is required for each boolean variable?
(think about and the answer will be posted in the next lecture notes!)
CONDITIONALS:
if then
statement
next_statement
if the condition is true the statement will be executed followed by the
next_statement. If the condition is false only the next_statement is
executed.
if then
statementX
else
statementY
next_statement
If the condition is true then statement X is executed followed by the
next_statement. If the statement is false then statement Y is executed
followed by the next_statement.
The case statement, for example:
case grade of {grade is a variable declared in the program}
'A' : writeln('excellent');
'B' : writeln('very good');
'C' : writeln('good');
end
This is the same as assigning the variable grade (which is declared as a
char) to either A, B, or C. Then if grade equals A then printout
excellent. These if statements would be much longer then the case
statement to do the same function.
Note: Appendix 2 page 526 gives a good review of pascal syntax
Class Notes - Lecture : Draft 1
In the last lecture notes a question was proposed: How much space is
required for each boolean variable? the answer is 1 bit.
Ch 15 Hardware Architecture
Hardware architecture refers to how the logic is implemented in circuits.
The ENIAC computer was made of vacuum tubes, required 40,000 watts, and
ran eleven minutes before a vacuum tube failed and had to be repaired.
The ENIAC could not store programs, but in 1946 John von Neumann wrote a
paper that inspired the first stored program design. The von Neumann
Architecture has formed the basis of all hardware since 1946. Only within
the past decade have deviations from the von Neumann architecture
appeared, which include RISC or reduced instructions set computer and
CISC or complex instruction set computer.
In the von Neumann architecture there are standard components, including
input/output units, memory, the arithmetic logic unit, and a control
unit which make up the CPU. The input and output units interface with the
outside devices. The internal memory is fast, local memory. It is also
called the 'scratch pad" or simply local memory. Basicly enough memory
needs to be allocated to execute one machine instruction. (Remember main
memory is different, main memory is slow and is for data storage, and is
not within the CPU). The ALU or arithmetic logic unit implements arithmetic
operations and basic logic. The ALU can accept input from a stored
program and give output to a program. (See fig 15.1) The control unit or
control logic controls the conditional sequence of operations. It
controls the operation of input and output, memory, the ALU, and timing.
Examples of von Neumann based machines include: 80286, 386, 486, and the
586 or pentium. Basicly all follow the stored program concept.
The von Neumann units are connected by buses. A bus is a common electronic
data pathway. The buses are made of wires that are carrying digital bits. Any
bit pattern is an "object".
Hardware names dealing with Bus Design
1. ISA or industry standard architecture
It was the original IBM PC XT, ran at 8 MHz, which means that 8
million things can be occurring per second
2. EISA enhance industry standard architecture
32 MHz , PC AT standard
3. PCI pentium 100 MHz
How Logic Works:
How digital bits are manipulated defines how logic works. The rules of
logic are the study of combinational logic or "combinatorial" logic.
Three basic elements deal with the rules of logic, these include AND,
NOT, OR. Recall that a bit has two states:
on / off
high / low
1 / 0
T / F
On, high, 1, and true are equivalent and off, low, 0, and false are
equivalent terms. Let X be a bit that holds the result and A,B also bits.
The OR primitive states that X is true (1) when either or both A or B are
true. A truth table expresses the logic relation between outputs and
inputs, it also maps all possible inputs to an output. The OR gate can
take multiple inputs, for example if there are three inputs there will be
eight possible states (if there are 2 bits then 2^3 = 8). The NOT gate is
a logical negation or inversion. It simply takes the input value and
inverts it, so if 1 was the input the output would be 0. The AND gate
states that if A and B are true then the result X will be true. n page 334
fig 15.6 and 15.7 , the truth tables for these gates are listed. With a
combination of these gates you can theoretically construct any digital
computer. With two bits, 0 and 1, and the NOT, OR, AND gates binary
addition can be accomplished and memory. The text gives a full
explanation of the one bit adder and the flip/flop which allows for
memory. It is very important to look at the symbols used in chapter 15 to
represent the gates and circuit diagrams developed from them.