High-Level Programming Languages Nell Dale & John Lewis (adaptation by Michael Goldwasser)
Low-Level Languages What are disadvantages of low-level languages? (e.g., machine code or assembly code) Programming in a low-level language is tedious! (have we made that clear yet?) Programs are only applicable to a particular CPU. Only primitive data types are directly supported. Languages offer minimal expressiveness for describing complex behaviors 8-2
High-Level Languages Higher-level languages provide a richer set of instructions and support, making the programmer s life even easier. Yet before a high-level program can be executed on a given CPU, it must be translated back to machine code. 8-3
Advantages Advantages of high-level languages include: - better portability (program runs on many CPUs) - richer data types and memory management - natural structures for expressing flow of control - much better support for software maintenance - much better support for software reuse 8-4
Translation Process A single high-level program can be translated to various CPU machine codes, but only if a translator exists for each such machine. Compiler: translates an entire high-level language program into machine code before it is executed. Interpreter: simulates a high-level language program, translating commands to machine code along the way, only as needed. 8-5
Compilers Figure 8.1 Compilation process 8-6
A Compiled Language Figure 8.2 8-7
An Interpreted Language Figure 8.2 8-8
High-Level Languages In designing high-level languages, goal is: maximize expressiveness to aid programmers yet ensure an automated translation process Many, many different high-level languages have been developed, e.g.: Fortran(1954), Lisp(1958), COBOL(1959), Simula(1964), Basic(1964), Smalltalk(1969), Prolog(1970), Pascal(1970), C(1971), ML(1973), Scheme(1975), Ada(1979), C++(1983), Perl(1987), Python(1991), Java(1995), C#(2000), VB.NET(2001) 8-9
Language Examples Our text draws upon four high-level languages for the purpose of demonstration: Ada, VB.NET, C++, Java We will introduce and experiment with one additional language (not discussed in text): Python 8-10
Variables Variables associate an identifying name with a piece of data stored in memory. Python SSCPU age = 25 LDI 25 STO age age DAT 8-11
Data Types Some primitive data types may be handled directly by the CPU (e.g., numbers, boolean values) High-level Languages support additional data types, such as character strings, files, pictures, or other data types designed for particular applications. 8-12
Data Types Many languages require an explicit declaration of the data type associated with a variable name. For example int age; string greeting; In Python, the data type is determined dynamically. age = 25 # stored as integer greeting = "Hello" # stored in ASCII 8-13
Assignment statement Python SSCPU age = 1 + age LDI 1 ADD age STO age the value of the right-hand expression is associated with the left-hand variable 8-14
Arrays and Lists Measurements Might refer to a specific entry, for example as: Measurements[6] 8-15
Output Commands Can evaluate and print any expression. Python SSCPU print age LOD OUT age print "Hello", guest (no equivalency in SSCPU) 8-16
Input Commands In Python, a response can be read from the user using the raw_input( ) command. guest = raw_input("who are you?") The variable is assigned to the string of characters entered by the user. If those characters are intended to represent a number, they must be explicitly converted. age = int(raw_input("how old are you?")) 8-17
Boolean Expressions Boolean expression: a sequence of identifiers, separated by compatible operators, that evaluates to True or False Boolean expression can be A Boolean variable An arithmetic expression followed by a relational operator followed by an arithmetic expression A Boolean expression followed by a Boolean operator followed by a Boolean expression 8-18
Relational, Logical Operators Relationship equal to not equal to less than less than or equal to greater than greater than or equal to Symbol ==!= < <= > >= Logical Operator and or not sample Boolean expressions: xvalue < yvalue name == "Michael" (temp >= 75.0) and (day == "Tues" or month!= "Oct") 8-19
Control Structures Control structure: an instruction that determines the order in which other instructions in a program are executed. These are implemented using the various "jump" commands in SSCPU. We will look at several common control structures: selection statements, looping statements, and subprogram statements 8-20
Selection Statements The if statement allows the program to test the state of affairs using a Boolean expression Python SSCPU if x < 0: x = -x LOD x JNG work JMP rest work LDI 0 SUB x STO x rest 8-21
If/Else Statements Figure 8.3 Flow of control of if statement 8-22
Selection Statements Python if temperature > 70: rating = 5 else: rating = 2 SSCPU LDI 70 SUB temperature JNG warm LDI 2 JMP save warm LDI 5 save STO rating 8-23
Looping Statements The while statement is used to repeat a course of action while boolean expr : loop body rest of program 8-24
While Loop High-Level Construct Low-Level Equivalency while boolean expr : loop body rest of program top [evaluate expression with result in Acc] JZR exit [body] JMP top exit [rest of program] 8-25
While loop example Python total = 0 val = int(raw_input( )) while val!= 0: total = total + val val = int(raw_input()) print total SSCPU LDI 0 STO total top INP JZR exit ADD total STO total JMP top exit LOD total OUT 8-26
While loop example Python count = 1 while count <= 5: print count count = count + 1 SSCPU LDI 1 STO count top LDI 5 SUB count JNG exit LOD count OUT ADD one STO count JMP top exit 8-27
Infinite loops, oops! What happens if the loop condition never becomes false? 8-28
Infinite loops, oops! count = 1 while count > 0: print count count = count + 1 8-29
Function Example def greeting(): print "Bonjour" greeting() 8-30
Subprogram/Functions If the language does not already include a particular behavior, we can define one! We give a section of code a name and use that name as a statement in another part of the program When the name is encountered, the processing in the other part of the program halts while the named code is executed 8-31
Subprogram Example Picture yourself in a boat And she s gone Chorus() Follow her down to a bridge and you re gone def Chorus(): Lucy in the sky with diamonds Lucy in the sky with diamonds Ahhh. Chorus() Picture yourself on a train 8-32
Parameters There are times when the calling unit needs to give information to the subprogram to use in its processing A parameter list is a list of the identifiers with which the subprogram is to work. (usually placed in parentheses, often with data type designations) 8-33
Parameter Example My Birthday Celebration Get Cake Place Candles in Cake Light Candles Sing( Michael ) Blow out Candles. Sing(name) Happy Birthday to you. Happy Birthday to you. Happy Birthday dear name Happy Birthday to you. 8-34
Function Example def greeting(person): print "Bonjour", person greeting("pierre") 8-35
Parameter Passing Value parameter: a parameter that expects a copy of its argument to be passed by the calling unit Reference parameter: a parameter that expects the address of its argument to be passed by the calling unit Analog to Machine Language: LDI vs LOD immediate operands vs. direct addressing 8-36
Machine Code Implementation The body of a function is code that can be stored somewhere in memory. A function call is enacted through the use of a JMP command. When the function completes, it must jump back to the context at which it was called. Technically, this requires a form of "jump" with an indirect operand (not supported by SSCPU) 8-37
Nested Subprograms Subprogram A( ) Call B( ) Subprogram B( ) Call C( ) Subprogram C( ) 8-38
Recursion Recursion: the ability of a subprogram to call itself Each recursive solution has at least two cases base case: the one to which we have an answer general case: expresses the solution in terms of a call to itself with a smaller version of the problem For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0: N! = N * (N 1)! 8-39
Recursion def factorial(n): if n <= 0: return 1 else: return n * factorial(n-1) 8-40