25
13-1 Programming…

13-1 Programming…. 13-2 General Purpose The computer is a “general purpose” tool –General purpose means it can do more than one thing –How do we make

Embed Size (px)

Citation preview

13-1

Programming…

13-2

General Purpose

• The computer is a “general purpose” tool– General purpose means it can do more than one thing

– How do we make it do more than one thing?

• make it perform a handful of small tasks that could be combined to perform more complicated tasks

• allow it to follow instructions to perform the above tasks

– It is programmable (i.e., we can program it)

• A computer program: a list of instructions for the computer

– What sort of instructions does the computer understand?

13-3

Tell me what to do. . .

• Make me a peanut butter and jelly sandwich:

– What does this mean to you?

13-4

Really tell me what to do. . .1. Go to the refrigerator2. Open fridge3. If ((no peanut butter) or (no jelly))

1. Go to store and buy ingredients

4. Remove peanut butter and jelly5. Get bread6. Open bread7. Do twice:

1. Take slice from bag

8. Place slices side by side9. Get knife10. Open peanut butter jar11. Insert knife into jar12. Apply peanut butter to knife13. While surface of bread is not covered with peanut butter

1. Apply peanut butter to bread from knife

14. Close peanut butter jar15. Open jelly jar16. Insert knife into jar17. Apply jelly to knife18. While surface of bread is not covered with jelly

1. Apply jelly to bead with knife

19. Close jelly jar20. Place both slices of bread together such that the side with peanut butter is touching the side with jelly

and the uncovered sides are facing out.And we’re probably missing steps!

“simple” tasks

repetition

logic

13-5

Language of Instructions

• What do we need to know about the target of our instructions before we can give them?

• How do we ask them to do things?

• What do they know how to do?

• What language do they speak?

13-6

Syntax and Grammar• Syntax – the form of what you say

– The grammatical structure (form) of a language– Grammar: defines the words you are allowed to use and specifies the order

in which you can place them• a grammar defines a syntax

noun := sandwichconnector := andadjective := peanut butter | jelly | adjective connector adjectiveverb := makesimpleRequest := verb noun | verb adjective noun

– a simpleRequest can be » make peanut butter and jelly sandwich» make sandwich» make jelly sandwich» make jelly and jelly sandwich» make jelly and jelly and jelly sandwich

– A grammar tells you how to parse a message into simpler parts

» (…or how to compose a message from parts)

the sandwich grammar

13-7

Language of instructions

• Semantics – the meaning of what is said– Semantics explain how you interpret the results of parsing

• make is a verb– A verb tells me what action to take

• peanut butter and jelly is an adjective– An adjective tells me about a noun

• sandwich is a noun– The noun is the target of my action

– The semantics provide the meaning for each of these things:• Ex: “when you encounter a make adjective sandwich you will need to get

two pieces of bread, and put the adjective between the pieces of bread.”

– How does this relate to computers?• Beyond the obvious fact that in the future there will be robots and they

will make us sandwiches

13-8

Applied to Computers. . .

• What language does a computer “speak” ?– Computers “think” in binary.

– Internally, they only understand 1’s and 0’s

– We’ve learned that computers can do fancy things with 1’s and 0’s

• Boolean logic

• Mathematics

• Store and retrieve 1’s and 0’s

• We need the computer to know that a particular sequence of 1’s and 0’s uniquely identifies some task to do (semantics)

• We need to give the computer a series of 1’s and 0’s

– Ideally: we (humans) would like a mapping between English (or something closer to English) and something that makes sense to the computer (binary)

13-9

In the beginning

• Early computers were primarily special purpose mathematic computers.

– Computers were designed to perform a particular arithmetic task or set of mathematic tasks.

• Computers that were unable to change their tasks are “hardwired”

– Hardwiring: Using solder to create circuit boards with connections needed to perform a specific task (in a larger sense).

• Computers that were able to have their tasks changed required direct “hardware modification” by skilled engineers

– Change the flow of electricity to run through different logic paths– Fat-fingering: Engineer needed to position electrical relay switches

manually.

13-10

The Programming Language Continuum

• ENIAC– Used programs to complete a

number of different mathematical tasks.

• Programs were entered by plugging connector cables directly into sockets on a plug-in board.

– Set-up could take hours.

– A program would generally be used for weeks at a time.

13-11

The Programming Language Continuum

• In the beginning… To use a computer, you needed to know how to program it.

• Today… People no longer need to know how to program in order to use the computer.– In fact, programming has become easier as well

– programming languages “evolve” from low-level to high-level.

– Fifth Generation - Natural Languages

– Fourth Generation - Non-Procedural Languages

– Third Generation - People-Oriented Programming Languages

– Second Generation - Assembly Language

– First Generation - Machine Language (code)

High

level

Low

level

13-12

Representing (Instructions) Programs

• Arithmetic Instructions– Addition, subtraction, multiplication, division, and other numeric operations

• Data Movement Instructions– Move numbers from place to place in the computer

• Logical or Comparison Instructions– Decision making instructions e.g. x < 10

• Control Instructions– Controls the sequences in which instructions are executed e.g. for x = 1 to 10

• Input/Output Instructions– Allow the program to communicate with something outside the program

(user, monitor, network, other programs)

13-13

Representation of Programs

• Program: A collection of instructions for the computer.

– Instructions are broken down into an opcode and an operand

• Opcode – what to do

• Operand – additional info needed to do the opcode

• Instruction – opcode + operand

– For some specific given CPU, imagine there are only eight valid opcodes (shown below)

READ PRINT

ADD SUB

STORE LOAD

PJUMP STOP

13-14

Representation of Programs

– Although we could simply use the ASCII codes and store each command as a collection of characters ...

• at some point the CPU will need to be able to “make decisions” based on opcodes – i.e., these opcodes will be binary input to the Instruction Decoding Unit

•To have the CPU directly understand ASCII would require many more wires/bits input into the CPU

• Map each opcode into a binary numberREAD = 000

LOAD = 001

ADD = 010

etc..

• And each operand will also be in binary such that the entire instruction is binary

–Ex: 001 11001–What does the operand 11001 mean?

–It could be a number, a symbol, a sound, a pixel of an image...

–It’s the context (program/instruction) that gives meaning

13-15

Generation I: Machine Language

• Machine language programs made up of instructions written in binary code.

• Computers operate in their “native” language• Humans translate their instructions into binary

• Each instruction has two parts: Operation code, Operand– Operation code (Opcode): The command part of a

computer instruction.– Operand: The address of a specific location in the

computer’s memory.

– You write programs in the “lowest-level” language directly for the CPU.

• Opcodes do very simple things (the computer is “dumb”) and you have it perform more complicated tasks by combining these instructions.

13-16

Generation I: Machine Language

• example . . .

Store the accumulator value into memory location “15”

011111013

Add the contents of memory slot “13” into the accumulator

011010112

Load contents of memory slot “14” into accumulator

011101001

MeaningOperandOpcodeInstruction # (slot #)

13-17

Generation I: Machine Language

• Machine language – Hardware dependent: Could be performed by only one type of

computer with a particular CPU.

– That is, each computer (although all speak binary) might have different arrangements of 1s and 0s for different instructions.

» “Different words could have different meanings”

» Maybe some computers can’t do the same things

– 111 might be add to me, but 010 might be add to you..

13-18

Generation II:Assembly

• Second Generation - Assembly Language– Assembly language programs are made up of instructions written in

mnemonics.

– Mnemonics: Uses convenient alphabetic abbreviations to represent operation codes, and abstract symbols to represent operands.

– Each instruction (still) had two parts: Operation code, Operand

» One-to-one mapping from written instruction to machine instruction

– Still a “low-level” language

– “higher level languages”

» Easier on the programmer

READ num1READ num2LOAD num1ADD num2STORE sumPRINT sumSTOP

13-19

The Programming Language Continuum

• Second Generation - Assembly Language– Assembly language programs are made up of instructions written in

mnemonics. » Because programs are not written in 1s and 0s, the

computer must first translate the program before it can be executed.

» The translation is a direct translation

» We translate into machine codeREAD == OPCODE 001num1 == Memory 0000 1110…

READ num1READ num2LOAD num1ADD num2STORE sumPRINT sumSTOP

13-20

Assembled, Compiled, or Interpreted Languages

• Assembled languages: – Assembler: a program used to translate Assembly language

programs.

– Produces one line of binary code per original program statement.

• The entire program is assembled before the program is sent to the computer for execution.

13-21

Generation: III

• Third Generation – High-level languages– Instructions in these languages are called statements.

• High-level languages: Use statements that resemble English phrases combined with mathematic and logical terms needed to express the problem or task being programmed.

• As before, programs are not written in 1s and 0s, the computer must first translate the program before it can be executed.

– Instructions are more complicated – one may actually represent several simple opcodes

– NOT-Hardware dependent. “portable”– So, as long as you can translate the statements from this

language to another machine code, you can run it on that platform. (more on this later)

13-22

High Level Language Example

• Pascal Example: Read in two numbers, add them, and print them out.

Program sum2(input,output);var num1,num2,sum : integer;

begin writeln(”please enter num1”); read(num1); writeln(”please enter num12”); read(num2); sum:=num1+num2; writeln(sum);end.

13-23

Generation: IV

• Fourth Generation - Non-Procedural Languages– Object-Oriented Languages: A language that expresses a computer

problem as a series of objects a system contains, the behaviors of those objects, and how the objects interact with each other.

• Object: Any entity contained within a system.– Examples:

» A window on your screen.» A list of names you wish to organize.» An entity that is made up of individual parts.

– Objects pass messages, expose interfaces, can be inherited from other objects. . .

• Some popular examples: C++, Java, Smalltalk, Eiffel.

13-24

O-O Language Example

– Object-Oriented Languages: example (Simplified Java?):

Object Person{String name;String gender;Integer age;String socialSecurityNumber;

}

Object BUStudent extends Person{// contains all properties of a personString BUID;Float GPA;// etc. . .

}

13-25

Generation: V

• Fifth Generation - Natural Languages– Natural-Language: Languages that use ordinary conversation in

one’s own language.

• Research and experimentation toward this goal is being done.

– Intelligent compilers are now being developed to translate natural language (spoken) programs into structured machine-coded instructions that can be executed by computers.

– Effortless, error-free natural language programs are still some distance into the future.