Writing a Program in a Highlevel Language - PowerPoint PPT Presentation

About This Presentation
Title:

Writing a Program in a Highlevel Language

Description:

To build a computer program. Figure out what you want to do ... tell if cars are waiting. Turn Rules Into Statements. Decide whether to change state ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 58
Provided by: davidd5
Category:

less

Transcript and Presenter's Notes

Title: Writing a Program in a Highlevel Language


1
Writing a Program in a High-level Language
  • Figure out what you want to do
  • Understand the rules that guide the process you
    are automating
  • Make sure that your rules are complete
  • Translate the rules into the computer language
  • Build structures to hold your data
  • Build tools to manipulate the structures
  • Make sure that the program does what the rules say

2
Elements of Programming
  • We looked at machine language
  • Single instructions that tell the hardware what
    to do
  • Primitive
  • Arithmetic, simple branching, communication with
    memory
  • We built state machines
  • States using memory
  • Transitions modeling tasks
  • A hardwired program

3
Elements of Programming
  • Weve seen
  • Truth tables
  • Logic gates
  • States and transitions in a state machine
  • Machine language
  • Now, higher level programming language

4
To build a computer program
  • Figure out what you want to do
  • Understand the rules that guide the process you
    are automating
  • Make sure that your rules are complete
  • Translate the rules into the computer language
  • Build structures to hold your data
  • Build tools to manipulate the structures
  • Make sure that the program does what the rules say

5
Figuring out the rules
  • For traffic lights
  • We stored data that told us the current color of
    lights
  • We read input from sensors
  • We had rules that told us whether to change state
  • We had rules that told us how to change state

6
Traffic Light Behavior
Otherwise
IF A1 AND B0
Light A
Always
Always
IF A0 AND B1
Otherwise
Light B
7
Turn Memory, Inputs and Outputs Into Variables
  • Store data to tell current color of lights
  • Dim LightA, LightB as Integer
  • 0 for red, 1 for yellow, 2 for green
  • Read input from sensors
  • Dim SensorA, SensorB as Integer
  • tell if cars are waiting

8
Turn Rules Into Statements
  • Decide whether to change state
  • If LightA 0 And LightB 2 And SensorA 1 And
    SensorB 0 Then
  • here we want to specify that the colors change
  • If LightA 2 And LightB 0 And SensorA 0 And
    SensorB 1 Then
  • again, we want to specify that the colors change

9
Build shell of program
  • Dim LightA, LightB as Integer
  • Dim SensorA, SensorB as Integer
  • If LightA 2 And LightB 0 And SensorA 0 And
    SensorB 1 Then
  • ChangeGreenToYellow(LightA)
  • ChangeYellowToRed(LightA)
  • ChangeRedToGreen(LightB)
  • If LightA 0 and LightB 2 And SensorA 1 And
    SensorB 0 Then
  • ChangeGreenToYellow(LightB)
  • ChangeYellowToRed(LightB)
  • ChangeRedToGreen(LightA)

10
Some Rules
  • Statements have to be in blocks
  • How does the computer know that the instructions
    are
  • If LightA 2 And LightB 0 And SensorA 0 And
    SensorB 1 Then
  • ChangeGreenToYellow(LightA)
  • ChangeYellowToRed(LightA)
  • ChangeRedToGreen(LightB)
  • And not
  • If LightA 2 And LightB 0 And SensorA 0 And
    SensorB 1 Then
  • ChangeGreenToYellow(LightA)
  • ChangeYellowToRed(LightA)
  • ChangeRedToGreen(LightB)

11
Some Rules
  • Statements have to be in blocks
  • If LightA 2 And Light B 0 And SensorA 0 And
    SensorB 1 Then
  • ChangeGreenToYellow(LightA)
  • ChangeYellowToRed(LightA)
  • ChangeRedToGreen(LightB)
  • End If

12
More rules Loops
  • Dont want just a single state change
  • Program should run forever,until we want it to
    stop
  • Can do this with a while loop, which is
    governed by a termination/continuation condition

13
More Rules
  • We have to tell the program to loop
  • Do While condition
  • action
  • Loop

14
More Rules
  • We have to tell the program to loop
  • Do While StillWantToControlTraffic
  • RunMyTrafficControlProgram
  • Loop

15
Procedures
  • We said ChangeGreenToYellow (LightA)
  • Could also say ChangeGreenToYellow(LightB)
  • Must write a procedure to change lights
  • Private Sub ChangeGreenToYellow(Light As Integer)
  • Light 1
  • End Sub
  • When called with an argument, such as LightA,
    substitutes it for Light parameter internally
  • So effect is to set LightA to 1

16
Procedures
  • Similarly
  • Private Sub ChangeYellowToRed(Light As Integer)
  • Light 2
  • End Sub
  • Private Sub ChangeRedToGreen(Light As Integer)
  • Light 0
  • End Sub
  • Procedure parameters and runtime arguments

17
Could build Procedure of Procedures
  • ChangeGreenToYellow(LightA)
  • ChangeYellowToRed(LightA)
  • ChangeRedToGreen(LightB)
  • Could become the command ChangeLights(LightA,Ligh
    tB)
  • Private Sub ChangeLights(Light1 As Integer,
    Light2 As Integer)
  • ChangeGreenToYellow(Light1)
  • ChangeYellowToRed(Light1)
  • ChangeRedToGreen(Light2)
  • End Sub

18
Using the procedure
  • ChangeLights(LightB,LightA) then does
  • ChangeGreenToYellow(LightB)
  • ChangeYellowToRed(LightB)
  • ChangeRedToGreen(LightA)
  • i.e. Light1 is LightB, and Light2 is LightA
  • Similarly, ChangeLights(LightA, LightC) does
  • ChangeGreenToYellow(LightA)
  • ChangeYellowToRed(LightA)
  • ChangeRedToGreen(LightC)

19
The Program Procedure Definitions
  • Private Sub ChangeGreenToYellow(Light As Integer)
  • Light 1
  • End Sub
  • Private Sub ChangeYellowToRed(Light As Integer)
  • Light 2
  • End Sub
  • Private Sub ChangeRedToGreen(Light As Integer)
  • Light 0
  • End Sub
  • Private Sub ChangeLights(Light1 As Integer,
    Light2 As Integer)
  • ChangeGreenToYellow(Light1)
  • ChangeYellowToRed(Light1)
  • ChangeRedToGreen(Light2)
  • End Sub

20
The Program (cont.)
  • Dim LightA, LightB as Integer
  • Dim SensorA, SensorB as Integer
  • If LightA 2 And LightB 0 And SensorA 0 And
    SensorB 1 Then
  • ChangeLights(LightA,LightB)
  • End If
  • If LightA 0 And LightB 2 And SensorA 1 And
    SensorB 0 Then
  • ChangeLights(LightB,LightA)

21
Make it happen forever
  • Dim LightA, LightB as Integer
  • Dim SensorA, SensorB as Integer
  • Dim StillWantToControlTraffic as Integer
  • StillWantToControlTraffic 1
  • Do While StillWantToControlTraffic
  • If LightA 2 And LightB 0 And SensorA 0
    And SensorB 1 Then
  • ChangeLights(LightA,LightB)
  • End If
  • If LightA 0 And LightB 2 And SensorA 1
    And SensorB 0 Then
  • ChangeLights(LightB,LightA)
  • End If
  • Loop

22
What could go wrong?
  • Program could get confused
  • Check for consistency
  • Replace
  • Private Sub ChangeGreenToYellow(Light As Integer)
  • Light 1
  • End Sub
  • With
  • Private Sub ChangeGreenToYellow(Light As Integer)
  • If (Light 0) Then
  • Light 1
  • Else
  • ReportInconsistency()
  • End If
  • End Sub

23
Building a bigger program
  • Could write this as a subroutine
  • Private sub ControlTrafficLight(light1,light2,sens
    or1,sensor2)
  • Could reuse the subroutine to do a whole string
    of lights.
  • But how would we keep track of hundreds of
    lights?

24
Arrays
  • Build arrays
  • LightNS1, LightNS2, LightNS3,
  • LightEW1. LightEW2. LightEW3,
  • SensorNS1, SensorNS2, SensorNS3,
  • SensorEW1, SensorEW2, SensorEW3,

25
Arrays (cont).
  • Access particular lights/sensors in array
  • ControlTrafficLight(lightNSi,lightEWi,sensorNS
    i,sensorEWi)
  • Control 100 traffic lights
  • For i 1 To 100
  • ControlTrafficLight(lightNSi,lightEWi,sensorNS
    i,sensorEWi) Next i
  • But
  • Lights may want to communicate
  • Lights affected by neighboring lights, not just
    sensors

26
Object oriented programming
  • Figure out characteristics of your data
  • Objects
  • Figure out operations youll want to perform
  • Methods
  • A modern programming style

27
Objects
  • Traffic light at intersection involves
  • Lights in each direction
  • Call them red, yellow and green and not 0,1,2
  • Sensors in each direction
  • Neighboring Lights
  • May affect change as much as sensors

28
Methods
  • Method of querying color of light
  • Method of changing color of light
  • Method of scheduling a color change later

29
What happens to the program?
  • It is either compiled or interpreted
  • Eventually it gets translated into machine
    language
  • If compiled
  • Can store executable and run again
  • If interpreted
  • Interpret from original language each time it is
    executed

30
What does the compiler do?
  • Identifies variables
  • These need space in RAM
  • Uses stores and loads to get values to registers
  • Parses commands
  • Turns each program statement into a string of
    machine language commands
  • Sets things up for execution

31
Steps in compilation
  • Lexical analysis
  • Identify all keywords
  • Identify all operators
  • Identify all variables
  • Make everything into tokens
  • Parsing
  • Turn the tokens into operations
  • Build a computation tree
  • Code generation
  • Generate machine code

32
Lexical analysis
  • Keywords
  • If Then .. End If
  • If .. Then .. Else End If
  • Do While Loop
  • Private sub .
  • End sub
  • Dim as Integer
  • Operators
  • (in 2 contexts)
  • If (Light 0)
  • Light 1
  • - /
  • - also in 2 contexts (unary or binary)

33
Simplified code fragment
  • Dim low As Integer, high As Integer
  • Dim fahrenheit As Double, celsius As Double
  • For fahrenheit low to high
  • celsius 5 / 9 (fahrenheit - 32)
  • print fahrenheit, celsius
  • Next fahrenheit
  • End

34
Code fragment (lexed)
  • Dim low As Integer, high As Integer
  • Dim fahrenheit As Double, celsius As Double
  • For fahrenheit low to high
  • celsius 5 / 9 (fahrenheit - 32)
  • print fahrenheit , celsius
  • Next fahrenheit
  • End
  • Keywords, variables, constants, operators,
    functions, separators

35
Code fragment (cont.)
  • Dim lttag1gt As Integer, lttag2gt As Integer
  • Dim lttag3gt As Double, lttag4gt As Double
  • For lttag3gt lttag1gt to lttag2gt
  • lttag4gt 5 / 9 (lttag3gt - 32)
  • print lttag3gt , lttag4gt
  • Next lttag3gt
  • End
  • Replace variables by tags
  • these are really locations in RAM
  • How things are defined (i.e. the types of the
    variables) determines
  • how much RAM they need
  • how operations on them work

36
Code fragment (cont.)
  • For lttag3gt lttag1gt to lttag2gt
  • lttag4gt 5 / 9 (lttag3gt - 32)
  • print lttag3gt , lttag4gt
  • Next lttag3gt
  • The instructions in the loop must be unwound
  • lttag3gt lttag1gt
  • lttag4gt 5 / 9 (lttag3gt - 32)
  • print lttag3gt , lttag4gt
  • lttag3gt lttag3gt 1
  • If lttag3gt lt lttag2gt go back

37
The unwound loop can be translated into machine
language
Store 32 in R3 Store 5/9 in R4 Store 1 in R5 Load
lttag1gt into R1 L1 Store R1 into lttag3gt Load
lttag3gt into R2 Subtract R3 from R2 and store in
R2 Multiply R4 by R2 and store in R2 Store R2 in
lttag4gt Print R1,R2 Add R5 to R1 and store in
R1 Store R5 in lttag3gt Load lttag2gt into
R6 Subtract R6 from R5 and store in R5 Go back to
L1 if R6 gt 0
  • lttag3gt lttag1gt
  • lttag4gt 5 / 9 (lttag3gt - 32)
  • print lttag3gt , lttag4gt
  • lttag3gt lttag3gt 1
  • If lttag3gt lt lttag2gt go back

38
How is the unwinding etc done Parsing
  • Language is defined by a grammar
  • Grammar is defined by production rules
  • Parsing is done by unwinding
  • Grammars and rules for parsing languages are
    complex, so lets look at something simpler A
    grammar for generating numbers (instead of
    computer programs)

39
How do we specify a grammar?
  • 2 aspects to a language
  • Symbols
  • Rewriting rules (also called productions)
  • Simple language for generating numbers
  • Symbols
  • Non-terminals
  • ltnumbergt, ltdigitsgt, ltsigngt, ltdigitgt
  • Terminals
  • - . 1 2 3 4 5 6 7 8 9

40
Simple rewriting rules
  • ltnumbergt ? ltsigngt ltdigitgtltdigitsgt . ltdigitsgt
  • ltsigngt ? -
  • ltdigitsgt ? ltdigitgtltdigitsgt ltdigitgt e
  • ltdigitgt ? 0 1 2 3 4 5 6 7 8 9

41
An example
  • ltnumbergt ? ltsigngt ltdigitgtltdigitsgt . ltdigitsgt
  • ltsigngt ? -
  • ltdigitsgt ? ltdigitgtltdigitsgt ltdigitgt e
  • ltdigitgt ? 0 1 2 3 4 5 6 7 8 9
  • ltnumbergt ?
  • ltsigngtltdigitgtltdigitsgt.ltdigitsgt ?
  • ltsigngtltdigitgtltdigitgtltdigitsgt.ltdigitsgt ?
  • ltsigngtltdigitgtltdigitgt.ltdigitsgt ?
  • ltsigngtltdigitgtltdigitgt.ltdigitgtltdigitsgt ?
  • ltsigngtltdigitgtltdigitgt.ltdigitgtltdigitgt ?
  • 98.65

42
Alternative rules
  • ltnumbergt ? ltsigngt ltdigitgtltdigitsgt . ltdigitsgt
  • ltsigngt ? -
  • ltdigitsgt ? ltdigitgtltdigitsgt ltdigitgt e
  • ltdigitgt ? 0 1 2 3 4 5 6 7 8 9
  • ltnumbergt ? ltsigngtltdigitsgt.ltdigitsgt
    ltsigngtltdigitsgt
  • ltsigngt ? -
  • ltdigitsgt ? ltdigitgtltdigitsgt ltdigitgt
  • ltdigitgt ? 0123456789

43
Parsing
  • ltnumbergt ? ltsigngtltdigitsgt.ltdigitsgt
    ltsigngtltdigitsgt
  • ltsigngt ? -
  • ltdigitsgt ? ltdigitgtltdigitsgt ltdigitgt
  • ltdigitgt ? 0123456789
  • What rules were applied to get 123.45?

44
What about real languages?
  • The complete grammar for C
  • about 400 lines long
  • 58 tokens (based on keywords)
  • 65 basic productions (each with many options)
  • Only a few complex situations

45
Some programming language issues
  • Type declarations
  • If you Dim something as an integer and then try
    to make it hold a double, what should happen?
  • Verification
  • How do you tell if your specification is right?
  • How do you tell if your program meets your
    specification?

46
History of Programming Languages
  • Fortran (1954) for scientific
  • Cobol (1959) for business
  • Algol (1958) more universal Fortran
  • Lisp (1958) string/concept oriented
  • APL (1960) formula oriented

47
History of Programming Languages
  • PL/1 (1964) from Algol Fortran
  • Basic (1964) for everyone to use
  • Simula (1967) combines with Algol to yield
    Smalltalk (1969) object oriented
  • BCPL ? B ? C (1971)
  • Algol ? Pascal (1971) ? Modula 1,2,3,

48
History of Programming Languages
  • C (1983) C with object oriented features
  • Often C is still used
  • Awk (1978) ? Perl (1987) report generators
  • Web programming language
  • Java (1991) object oriented and portable
  • Web applets, devices
  • Visual Basic(1991) macros and programs
  • Core of Microsoft systems

49
What makes a good language
  • Does the task you want
  • Keeps you from making mistakes
  • Supports debugging when you need it
  • Has a strong tool kit

50
Big number bug
On June 4, 1996 an unmanned Ariane 5 rocket
launched by the European Space Agency exploded
just forty seconds after its lift-off from
Kourou, French Guiana. The rocket was on its
first voyage, after a decade of development
costing 7 billion. The destroyed rocket and its
cargo were valued at 500 million. A board of
inquiry investigated the causes of the explosion
and in two weeks issued a report. It turned out
that the cause of the failure was a software
error in the inertial reference system.
Specifically, a 64 bit floating point number
relating to the horizontal velocity of the rocket
with respect to the platform was converted to a
16 bit signed integer. The number was larger than
32,768, the largest integer that could be stored
in a 16 bit signed integer, and so the conversion
failed.
51
Pentium II bug
  • Software bug encoded in hardware
  • Division algorithm uses a lookup table of 1066
    entries
  • Only 1061 of the entries are downloaded to the
    PLA (programmed logic array from which the data
    are used)
  • Intel had to recall all versions of the chip

52
Syntax typo bugs
  • NASA Mariner 1 , Venus probe (1992)
  • Intended to be the first US spacecraft to visit
    another planet, it was destroyed by a range
    officer on 22 July 1962 when it behaved
    erratically four minutes after launch.
  • Essentially a period instead of a comma in a
    FORTRAN DO-Loop

53
Control flow bug
  • ATT long distance service fails for nine
    hours(Wrong BREAK statement in C code)
  • January 15, 1990
  • 70 million of 138 million long distance customers
    in the US lost long distance service.
  • Cost to ATT was between 75 Million and 100
    Million (plus the loss of good will).

54
Data structure management bug
  • E-mail buffer overflow (1998)
  • Several E-mail systems suffer from a "buffer
    overflow error", when extremely long e-mail
    addresses are received.  The internal buffers
    receiving the addresses do not check for length
    and allow their buffers to overflow causing the
    applications to crash.  Hostile hackers use this
    fault to trick the computer into running a
    malicious program in its place.

55
Summary
  • Programming is difficult
  • Have to thoroughly understand the task
  • Have to anticipate all possibilities
  • Code is written at a fairly primitive level
  • Impossible to anticipate what users might do
  • Programming languages allow the user to use tools
    to build code
  • But everything still has bugs
  • The cost of a bug can be very large
  • There is no Moores Law for software.

56
Where are we
  • Weve built a computer
  • Weve built programs
  • And looked under the hood

57
Whats next
  • Algorithms
  • Networking The Internet, Email, the Web
    Operating Systems (Mon, Apr 7)
  • Sound and Graphics (Wed, Apr 9 and Mon, Apr 14,
    resp.)
  • Distributed Systems (Wed, Apr 16)
  • Sharing files, sharing cycles, distributed
    computing
  • Complexity theory
  • Undecidable problems, unsolvable (in practice)
    problems
  • Applications of hard problems
  • Social impacts
  • Digital rights management
  • Access to information (Digital Divide)
  • Artificial intelligence
Write a Comment
User Comments (0)
About PowerShow.com