Home * Programming * Languages * Modula-2

Modula-2 is a general purpose procedural programming language, designed and developed between 1977 and 1980 by Niklaus Wirth at ETH Zurich, as successor to his earlier programming language Pascal and influenced by the Mesa programming language. Modula-2 was sufficiently flexible to do system programming. Object oriented Modula-2 was co-developed by Elmar Henne [1][2].

Sample Code

A CRC-32 [3] implementation in Modula-2 by Alexander Naumov [4]:
IMPLEMENTATION MODULE CRC32;
 
FROM SYSTEM IMPORT
  CARD8, CARD32, SET8, SET32, CAST, LOC, ADDRESS, SHIFT, ADR, ADDADR;
 
CONST
  crcPoly = 0EDB88320H;
 
VAR
  Table: ARRAY [0..255] OF SET32;
 
PROCEDURE Init (VAR crc: CARD32);
BEGIN
  crc := 0FFFFFFFFH;
END Init;
 
PROCEDURE Get (crc: CARD32): CARD32;
BEGIN
  RETURN CAST(CARD32, {0..31}-CAST(SET32, crc));
END Get;
 
PROCEDURE Update (VAR crc: CARD32; buf: ARRAY OF LOC);
BEGIN
  RawUpdate(crc, ADR(buf), HIGH(buf)+1);
END Update;
 
PROCEDURE RawUpdate (VAR crc: CARD32; buf: ADDRESS; len: CARDINAL);
VAR
  c: SET32;
BEGIN
  c := CAST(SET32, crc);
  WHILE len > 0 DO
    c := Table[CAST(CARD8, CAST(SET8, c) / CAST(SET8, buf^))] / SHIFT(c, -8);
    buf := ADDADR(buf, SIZE(SET8));
    DEC(len, SIZE(SET8));
  END (*WHILE*);
  crc := CAST(CARD32, c);
END RawUpdate;
 
PROCEDURE InitTable ();
VAR
  i, j: CARDINAL;
  c   : SET32;
BEGIN
  FOR i := 0 TO 255 DO
    c := CAST(SET32, i);
    j := 8;
    REPEAT
      IF 0 IN c THEN
        c := SHIFT(c, -1) / CAST(SET32, VAL(CARD32, crcPoly));
      ELSE
        c := SHIFT(c, -1);
      END (*IF*);
      DEC(j);
    UNTIL j = 0;
    Table[i] := c;
  END (*FOR*);
END InitTable;
 
BEGIN
  InitTable();
END CRC32.

External Links


References

  1. ^ MAXCAD / CAD600 from Thomas Nitsche Projects by Thomas Nitsche
  2. ^ Modula-2 Standardization
  3. ^ Cyclic Redundancy Check (pdf), a sample chapter from Henry S. Warren, Jr. Hacker's Delight
  4. ^ The Sources Store for Modula-2 and Oberon-2 Programming Languages

What links here?


Up one Level