Etwas leichter und übersichtlicher wird die Programmierung, wenn man einen Assembler verwendet, bei dem man anstelle der Bytes die Befehle in Textform (als mnemonische Codes) eingibt, z.B. MOV, ADD, SUB, JMP, JSR, RET. Doch erlauben die Assembler-Befehle nur sehr primitive Operationen wie das Verschieben von Daten (MOV), das Addieren bzw. Subtrahieren ganzer Zahlen (ADD, SUB), Sprünge (JMP) und Verzweigungen in Unterroutinen (Subroutines, JSR, RET).
Effizienter ist die Verwendung problemorientierter höherer Programmiersprachen, die wesentlich mächtigere Funktionen bereitstellen (z.B. sqrt, exp, sin). Man kann sie nach verschiedenen Kriterien einteilen (z.B. deklarativ, prozedural (imperativ), objektorientiert). Der Programmierer erstellt zunächst den Quellcode im menschenlesbaren (ASCII- oder Unicode-) Klartext. Für die Umsetzung dieses Quellcodes in Maschinencode gibt es drei Möglichkeiten:
Programmablaufsteuerung: die wichtigsten Konstrukte sind bedingte Anweisungen (IF ... THEN ... ELSE ...) und Schleifen (Zählschleifen: FOR ...; WHILE ...; DO ... WHILE ...).
Im folgenden Kurs soll auf eine systematische Behandlung der Programmierung verzichtet werden - siehe dazu z.B.
Statt dessen sollen einige Programmiersprachen kurz vorgestellt und anhand von Beispielen diskutiert werden. Kleine eigene Programme sollen in BASIC erstellt werden, weil Anfänger damit am schnellsten zum Ziel kommen.
100 PRINT "Hello world" 110 END
Aufruf über einen BASIC-Interpreter (z.B. basic, gwbasic, bwbasic) - der Bywater BASIC Interpreter (bwbasic) steht auf den SGI-Workstations zur Verfügung:
bwbasic hello.bas system
PRINT "Hello world" END
Aufruf z.B. mit dem Bywater BASIC Interpreter (bwbasic) oder über Turbo-Basic (TB, in O:\TB):
bwbasic hello2.bas system
Fortran 77 (f77) zeigt noch seine Abstammung aus dem Lochkarten-Zeitalter: Der eigentliche Programmcode muss in den Spalten 7 - 72 stehen. Die neuere Version Fortran 90 (f90) ist in dieser Hinsicht ggf. flexibler, außerdem werden Elemente der objektorientierten Programmierung unterstützt.
PROGRAM hello
PRINT *, 'Hello world'
END
Compilation und Aufruf (unter Unix, auf SGI-Workstations oder unter SuSE-Linux):
f77 -o hello hello.f hello
program hello;
begin
writeln('Hello world');
end.
#include <stdio.h>
void main()
{
printf("Hello world\n");
}
Compilation und Aufruf (unter Unix, auf SGI-Workstations oder unter SuSE-Linux):
cc -o hello hello.c hello
Java wurde ausgehend von C++ entwickelt und hat daher große Ähnlichkeiten damit.
/**
* The hello class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class hello {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
Compilation und Aufruf (unter Unix, auf SGI-Workstations oder unter SuSE-Linux):
(SGI-Workstations: setenv PATH {$PATH}:/usr/java/bin.)
javac hello.java java hello
Java als Applet: hellojav.html mit Hello2.java (bzw. Hello2.class)
#!/usr/local/bin/perl print "Hello world\n";
100 REM Comments start with "REM" 110 REM Pascal triangle 120 OPTION BASE 1 :REM arrays start with element 1 (default: 0) 130 DIM basis(13) :REM array "basis" with 13 elements (1..13) 140 spaces = 23 150 REM Now initialize the array elements with 1 (loop: FOR .. TO .. NEXT): 160 FOR i=1 TO 13 170 basis(i) = 1 180 NEXT i 190 REM Now print first three elements: 200 PRINT USING " Pascal triangle #####"; basis(13) 210 PRINT USING " ##### ####"; basis(12), basis(13) 220 REM three nested loops follow (FOR .. TO .. NEXT): 230 FOR i=12 TO 2 STEP -1 :REM step downward from 12 to 2 240 FOR j=i TO 12 :REM step 1 250 basis(j) = basis(j) + basis(j+1) 260 NEXT j 270 spaces = spaces - 2 280 REM prepend proper number of spaces: 290 PRINT SPACE$(spaces); 300 FOR k=i-1 TO 13 310 PRINT USING "#####"; basis(k); 320 NEXT k 330 PRINT :REM end of line (line feed) 340 NEXT i 350 END
Ohne Zeilennummern: pascal2.bas.
C Comments start with "C" (or "*") in the first column
C fixed-form style: statements are written in columns 7 - 72
C columns 1-5: label, column 6: continuation line if non-blank
C00000000111111111122222222223333333333444444444455555555556666666666777
C23456789012345678901234567890123456789012345678901234567890123456789012
PROGRAM pascalt
INTEGER basis(13), spaces
CHARACTER*10 fmt
DATA basis /13*1/, spaces /23/
WRITE (*, 1111) basis(13), basis(12), basis(13)
C output of first three elements
1111 FORMAT (5X,'Pascal triangle',4X,I6/22X,I6,I5)
DO 20 i=12,2,-1
DO j=i,12
basis(j) = basis(j) + basis(j+1)
END DO
spaces = spaces - 2
WRITE (fmt, 22) spaces
22 FORMAT ('(',I2,'X,13I5)')
WRITE (*, fmt) (basis(k), k=i-1,13)
20 CONTINUE
END
{ This is a comment }
{ Pascal triangle }
program pascalt;
var i,j,k,spaces : integer; { declaration of integer varables }
basis : array [1..13] of integer; { array with elements 1..13 }
begin
spaces := 23; { initialization }
{ initialize the array elements: loop (for .. to .. do) }
for i:=1 to 13 do basis[i] := 1;
{ Now print first three elements: writeln = write line (with line feed) }
writeln(' Pascal triangle ', basis[13]:6);
writeln(' ', basis[12]:6, basis[13]:5);
{ three nested "for"-loops follow }
for i:=12 downto 2 do { step: -1 ! }
begin
for j:=i to 12 do
basis[j] := basis[j] + basis[j+1];
spaces := spaces - 2;
{ prepend proper number of spaces: }
write(' ':spaces);
for k:=i-1 to 13 do
write(basis[k]:5);
writeln; { end of line (line feed) }
end;
end.
/* This is a comment */
/* Pascal triangle */
#include <stdio.h> /* "header" with definitions for standard input/output */
/* each C program must have one function "main" */
int main()
{ /* '{' means "BEGIN", '}' means "END" */
int i,j,k; /* declaration of integer varables */
int spaces=23;
int basis[14]; /* array with elements 0..13 (!) */
/* initialize the array elements: loop for(initialization; condition; step) */
for(i=1; i<=13; i++) /* "i++" means i = i+1 */
{
basis[i] = 1;
}
/* Now print first three elements: */
/* "%6d" means "decimal integer, width 6", "\n" means "line feed" */
printf(" Pascal triangle %6d\n", basis[13]);
printf(" %6d%5d\n", basis[12], basis[13]);
/* three nested "for"-loops follow */
for(i=12; i>=2; i--) /* i-- means: i = i-1 */
{
for(j=i; j<=12; j++)
{
basis[j] += basis[j+1]; /* means: basis[j] = basis[j] + basis[i]; */
}
spaces -= 2; /* means: spaces = spaces - 2 */
/* prepend proper number of spaces: */
printf("%*s", spaces, " "); /* yields "%21s" etc. */
for(k=i-1; k<=13; k++)
{
printf("%5d", basis[k]);
}
printf("\n"); /* end of line (line feed) */
}
return 0;
} /* '}' means "END" */
Kleine Programmieraufgabe: 10! (10 Fakultät) mit Turbo-BASIC berechnen.