Low and High Level Languages

The evolution of computer languages has seen a nearly constant progress away from so-called 'low-level' languages (those most similar to the binary code) toward more abstract and conceptual ways of organizing computational tasks. This has largely been motivated by the need to alleviate the programmer of the more tedious parts of controlling a complex computer system.

In the early days of computing, computers and computer time was far more expensive than programmer time; therefore, programmers would actually create programs of sequences of binary code by hand. Since binary sequences are difficult to work with, they developed a shorthand called 'assembly' language where each valid binary sequence was given a shorthand notation. A program called an 'assembler' would translate such code into machine-readable binary. A simple example of assembly language for the Motorola 68000 processor is as follows.

This assembly language program does the following:

• moves the number stored at memory location indicated in register A2 to data register D0;

• adds this number to the number stored at memory location indicated in register A4; and

• moves the result to memory location indicated in register A1.

Each line of this language is a one-to-one translation of the binary codes used to enact these actions in the processor.

In the early days of computing, this translation was done manually by programmers. A story which has passed into legend in computer science tells of an enterprising young programmer, who suggested that a computer program could do this tedious translation much more efficiently than human programmers. He was rebuked by his supervisor: ''How dare you suggest that we use this expensive machine for mere clerical work.'' As programmer time became more expensive than computer time, assemblers and other automated forms of computer language translation became commonplace. Further details are provided in the section entitled 'compiled and interpreted languages'.

Since assembly is merely shorthand for the underlying binary code, it shares the same structure. That is, the computer begins executing commands starting from the first instruction and continues downwards, one instruction at a time. In order to control the flow of the computation, a special 'jump' instruction exists (often denoted by 'JMP' in assembly). This is a conditional branching command, which instructs the computer to continue execution at some arbitrary point in the program if a specific condition holds. This simple instruction allows for all of the complexity of modern software. It can be used to specify that certain sequences of code be executed only in certain cases (an 'if—then-else' structure). By having the execution return to a previous section of code, it can allow certain sequences of code to be executed many times (a 'loop' structure).

FORTRAN was the first language that was readily adopted for scientific work. Although it is rarely taught any more as an introduction to programming, many existing simulation models contain a large amount of legacy code written in FORTRAN. Fortunately, FORTRAN is relatively easy to learn for anyone with experience with another computer language. BASIC is another relatively easy to learn low-level language, and is often recommended for novice programmers or scientists who do not program on a continuous basis, as a first language to use when developing a simulation model. Because of its relative simplicity, it allows the modeler to focus more on the model and less on programming problems. The following example program in FORTRAN 77 shows how some of these control structures can be achieved using such conditional branching.

1 this program computes a standard population growth model: 1 P(t) = P0*eArt

1 Declare variables & parameters INTEGER T, N

DOUBLE PRECISION E, R, I 1 Initialize our constant parameters

PARAMETER (N = 20, E = 2.718281828, R = 0.5, P0 = 2.0 ) 1 Array for the population values DOUBLE PRECISION P(N)

WRITE(*, * ) 'population growth steps starting from a starting '

1 set loop counter T = 1

1 loop through and compute population 1 without using any control statements except for 1 if comparisons and goto statements 10 IF ( T .GT. N ) GO TO 20 P(T) = P0 *E* * (R* (T-l) ) T = T + 1 GO TO 10

1 End of the loop

1 loop through and print each one out 1 using a do-loop control structure 20 DO 3 0 T = 1, N

3 0 CONTINUE

1 End of the loop

1 End of the program

4 0 END

The FORTRAN program contains two different loops. The first, at marker 10, uses a conditional branch to escape from the loop (by jumping to marker 20) when the variable T becomes greater than N, and a GOTO to jump back to marker 10 at the end of the loop. The second loop, at marker 20, also escapes when T becomes greater than N; however, it uses the DO-LOOP form to do this. Although both forms accomplish the same end, the DO-LOOP syntax relieves the programmer from the mundane bookkeeping tasks of making sure that the structure of the loop is sound. It would be very easy, for example, to forget to increment the variable T on each pass through the loop, which would result in a loop which would never terminate!

As a group, high-level languages are not characterized by any particular features. Rather they each contain some form of abstract or conceptual structures that are not directly reflected in the underlying binary code. The languages dealt with in all subsequent sections are generally considered higher-level languages.

Procedural Languages

For the applications that are most likely to be built in ecology, the categorization between procedural and object-oriented languages may be the most relevant in deciding on the approach to programming, whether it is for simulation modeling, graphical displays, or other analyses. As can be seen in the FORTRAN example above, a large number of specific instructions are required to have the program execution jump to a particular block of code, execute it, and then return. All of this is automated in a procedural language by providing a clear framework within the language for defining such blocks of code. Rather than executing a program line-by-line from the first instruction to the last, all code is organized into blocks, which can be either subroutines or control structures. The program typically starts execution with a special 'main' subroutine. Code within a block is executed line-by-line, but it may also jump into and back from other blocks.

The following example C program produces the same population growth model sequence as the FORTRAN program example.

#include <stdio.h> #include <stdlib.h> #include <math.h>

// function to compute populations double* popn( int n, double pO, double r ) {

// create & allocate the array double* p = (double*) malloc( n * sizeof(double) )

// loop through the array int t;

for( t = 0; t < n; t++ ){ p[t] = pO * exp( r*t );

return p;

// prints out the contents of the popn array void print_popn( int n, double* popn ) {

int n = 20; double rl = 0.5; double il = 2.0; double r2 = 0.02 5; double ±2 = 42.0;

// main function

// code execution starts here int main(void) {

// calculate for first set of parameters printf("computing %d steps of pop. growth ", n );

printf("from initial pop. of %f with growth rate of %f\n", il, rl ) double* pn = popn( n, il, rl ); print_popn( n, pn );

// calculate for 2nd set of parameters printf("computing %d steps of pop. growth ", n );

printf("from initial pop. of %f with growth rate of %f\n", ±2, r2 ) pn = popn( n, i2, r2 ) ; print_popn( n, pn );

There are some very noticeable differences between this code and the FORTRAN code, particularly how the code is organized into blocks. The blocks in C are enclosed by curly braces ( { and } ). The code is mostly grouped into three main blocks, which are the three functions, or subroutines, of the program, namely popn, print_popn, and main. There are additionally two FOR loops (which are equivalent to the FORTRAN DO loops), which are also blocks, and also use curly braces.

One of the other language features of procedural languages is the 'scope' of a variable. There are many times when certain values must be computed within a block, which are of no use or interest in other blocks. In fact, allowing all such variables to be accessible from all parts of the program would be wasteful both of the computer's resources, and of the space of available variable names. The scope of a variable defines a certain range in which that variable is accessible. Some variables can be defined as globally accessible; others are only locally accessible to a particular block, and possibly its sub-blocks. In the above C program, for example, the variables n, il, rl, i2, and r2 all have global scope, but the t variables in the two subroutines are only locally accessible in those subroutines and the for-loops within them.

C began to emerge in a range of programming applications in the late 1980s and early 1990s, although it was developed in 1972. Historically, the roots of C lie in a language called Algol that was developed in 1960. Standard C (also known as ANSI C) was formalized in 1982. PASCAL, another procedural language based on Algol, was originally developed as a language to teach programming, but with ever larger numbers of students trained in PASCAL, it began to be used as a language of choice across a wide range ofsciences and social sciences. It is possible that models written in PASCAL are still in use but these models would be encountered less frequently than FORTRAN or BASIC.

The C language introduced the use of structures to the larger programming community. Structures allow different variables to be grouped together, regardless of type. A structure is essentially a 'container' for a number of data variables of different types, but which are conceptually related to each other. By grouping variables together, the programmer can refer to all of that related data with a single reference. Many 'instances' of the same structure can be created to represent many variations of the same kinds of data. For example, a structure might be created to represent contact information. Such a structure might have variables for data such as names, addresses, telephone numbers, personnel id numbers, etc. A running program might create many instances of this same structure in computer memory, so that it could easily work with the contact information of several people at once. A language which uses structures, such as C, is also referred to as a structural language. Object-oriented programming expands upon structures, increasing the modularity of programming even further.

Was this article helpful?

0 0
10 Ways To Fight Off Cancer

10 Ways To Fight Off Cancer

Learning About 10 Ways Fight Off Cancer Can Have Amazing Benefits For Your Life The Best Tips On How To Keep This Killer At Bay Discovering that you or a loved one has cancer can be utterly terrifying. All the same, once you comprehend the causes of cancer and learn how to reverse those causes, you or your loved one may have more than a fighting chance of beating out cancer.

Get My Free Ebook


Post a comment