home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASOCT.ZIP / PASOCT.PAS
Encoding:
Pascal/Delphi Source File  |  1986-11-08  |  7.4 KB  |  244 lines

  1. {Some library functions to deal with octal numbers}
  2. {Function OCT converts an integer to a string representing
  3. the octal number. Example  x= oct(integer)
  4. Function STR_OCT converts a string representing an octal
  5. number to an integer. Example x = str_oct(string)}
  6.  
  7.  
  8.  
  9.  
  10. {The following procedures help in providing some machine level
  11. control by allowing a programmer to set specific bits in an
  12. integer (procedure setbit) or clear specific bits in an integer
  13. (procedure clearbit). The procedures expect two integer values
  14. to be passed. The first integer is the integer in which to
  15. manipulate the bit. This integer is treated as a variable
  16. parameter. The second integer should represent the bit number
  17. from 0 to 15 with bit 0 being the rightmost bit. A fatal error
  18. will occur if the bit number is > 15.}
  19.  
  20. procedure setbit(var number : integer ; bit_number : integer);
  21.  
  22.   const
  23.         bit_0 = $0001;
  24.         bit_1 = $0002;
  25.         bit_2 = $0004;
  26.         bit_3 = $0008;
  27.         bit_4 = $0010;
  28.         bit_5 = $0020;
  29.         bit_6 = $0040;
  30.         bit_7 = $0080;
  31.         bit_8 = $0100;
  32.         bit_9 = $0200;
  33.         bit_10 = $0400;
  34.         bit_11 = $0800;
  35.         bit_12 = $1000;
  36.         bit_13 = $2000;
  37.         bit_14 = $4000;
  38.         bit_15 = $8000;
  39.  
  40.   var
  41.      x : integer;
  42.  
  43.   begin
  44.         if bit_number >= 16 then
  45.            begin
  46.                 writeln;
  47.                 writeln('FATAL ERROR IN SETBIT PROCEDURE');
  48.                 writeln('BIT INDEX IS > 15');
  49.                 writeln('Program TERMINATING');
  50.                 halt;
  51.            end;
  52.     case bit_number of
  53.          0 : x := bit_0;
  54.          1 : x := bit_1;
  55.          2 : x := bit_2;
  56.          3 : x := bit_3;
  57.          4 : x := bit_4;
  58.          5 : x := bit_5;
  59.          6 : x := bit_6;
  60.          7 : x := bit_7;
  61.          8 : x := bit_8;
  62.          9 : x := bit_9;
  63.          10 : x := bit_10;
  64.          11 : x := bit_11;
  65.          12 : x := bit_12;
  66.          13 : x := bit_13;
  67.          14 : x := bit_14;
  68.          15 : x := bit_15;
  69.          end;
  70.     number := number and (not x);
  71.     number := number + x;
  72.  
  73. end;
  74.  
  75. procedure clearbit(var number : integer; bit_number : integer);
  76.  
  77.   var
  78.      x : integer;
  79.  
  80.     begin
  81.          if bit_number >= 16 then
  82.             begin
  83.             writeln;
  84.             writeln('FATAL ERROR IN CLEARBIT PROCEDURE');
  85.             WRITELN('BIT NUMBER > 15');
  86.             writeln('BIT NUMBER = ',bit_number);
  87.             WRITELN('PROGRAM TERMINATING');
  88.             END;
  89.          case bit_number of
  90.               0 : x := not $0001;
  91.               1 : x := not $0002;
  92.               2 : x := not $0004;
  93.               3 : x := not $0008;
  94.               4 : x := not $0010;
  95.               5 : x := not $0020;
  96.               6 : x := not $0040;
  97.               7 : x := not $0080;
  98.               8 : x := not $0100;
  99.               9 : x := not $0200;
  100.               10 : x := not $0400;
  101.               11 : x := not $0800;
  102.               12 : x := not $1000;
  103.               13 : x := not $2000;
  104.               14 : x := not $4000;
  105.               15 : x := not $8000;
  106.               end;
  107.          number := number and x;
  108. end;
  109.  
  110.  
  111. {This function provides a means of viewing an octal representation
  112. of an integer. The function expects an integer as input
  113. and returns a 6 digit string which is an octal representation
  114. of the integer.}
  115.  
  116. type
  117.     str6 = string[6];
  118.  
  119. function oct(number : integer): str6;
  120.  
  121.     var
  122.        result : string[6];
  123.        x, y, bit_mask, temp1 : integer;
  124.        subresult : char;
  125.  
  126.    begin
  127.         result := '      ';
  128.         bit_mask := $8000;
  129.         x := 0;
  130.         x := bit_mask and number;
  131.         if x = 0 then subresult := '0'
  132.         else subresult := '1';
  133.         result[1] := subresult;
  134.         bit_mask := $4000;
  135.         for y := 1 to 5 do
  136.             begin
  137.                  temp1 := 0;
  138.                  if y <> 1 then bit_mask := bit_mask div 2;
  139.                  x := bit_mask and number;
  140.                  if x <> 0 then setbit(temp1,2);
  141.                  bit_mask := bit_mask div 2;
  142.                  x := bit_mask and number;
  143.                  if x <> 0 then setbit(temp1,1);
  144.                  bit_mask := bit_mask div 2;
  145.                  x := bit_mask and number;
  146.                  if x <> 0 then setbit(temp1,0);
  147.                  case temp1 of
  148.                       0 : subresult := '0';
  149.                       1 : subresult := '1';
  150.                       2 : subresult := '2';
  151.                       3 : subresult := '3';
  152.                       4 : subresult := '4';
  153.                       5 : subresult := '5';
  154.                       6 : subresult := '6';
  155.                       7 : subresult := '7';
  156.                   else
  157.                       begin
  158.                            writeln;
  159.                            writeln('FATAL ERROR IN OCTAL FUNCTION');
  160.                            WRITELN('    PROGRAM TERMINATING      ');
  161.                            HALT;
  162.                       end;
  163.                  end;
  164.                  result[y+1] := subresult;
  165.             end;
  166.       oct := result;
  167. end;
  168.  
  169.  
  170. {function str_oct provides a means of converting a string representing}
  171. {an octal number to be converted to an integer.                       }
  172. {the function expects no more than a 6 character string and returns an}
  173. {integer result. example : y := str_oct(x)  where y is an integer and }
  174. {x is a string of no more than 6 characters representing an octal number}
  175.  
  176. type
  177.     anystring = string[6];
  178.  
  179. function str_oct(num_string : anystring ):integer;
  180.  
  181.    var
  182.       w , x , y , z ,str_oct1 ,most_flag : integer;
  183.       temp1 : char;
  184.  
  185.    begin
  186.         str_oct1 := 0;
  187.         most_flag := 0;
  188.         x := length(num_string);
  189.         if x > 6 then
  190.            begin
  191.            writeln('Fatal ERROR in Function Str_oct');
  192.            writeln('String length is > 6');
  193.            writeln('String = ',num_string);
  194.            writeln('Program Terminating');
  195.            halt;
  196.            end;
  197.         if x = 6 then
  198.            begin
  199.            temp1 := num_string[1];
  200.            case temp1 of
  201.                 '0' : most_flag := 0;
  202.                 '1' : setbit(most_flag,15);
  203.                 else
  204.                     begin
  205.                     writeln('FATAL ERROR IN STR_OCT FUNCTION');
  206.                     WRITELN('CHARACTER 6 > 1');
  207.                     WRITELN('NUM_STR = ', num_string);
  208.                     WRITELN('PROGRAM TERMINATING');
  209.                     HALT;
  210.                     END;
  211.               end;
  212.            end;
  213.  
  214.      if x = 6 then w := 2 else w := 1;
  215.  
  216.      for y := w to x do
  217.            begin
  218.            temp1 := num_string[y];
  219. {the following line is handy for debugging}
  220. {writeln('y= ',y,'  temp1 = ',temp1,'  str_oct1 = ',str_oct1);}
  221.            case temp1 of
  222.                 '0' : z := 0;
  223.                 '1' : z := 1;
  224.                 '2' : z := 2;
  225.                 '3' : z := 3;
  226.                 '4' : z := 4;
  227.                 '5' : z := 5;
  228.                 '6' : z := 6;
  229.                 '7' : z := 7;
  230.                 else
  231.                     begin
  232.                     writeln;
  233.                     writeln('FATAL ERROR IN FUNCTION STR_OCT');
  234.                     writeln('Invalid Number in string');
  235.                     writeln('STRING = ', num_string);
  236.                     writeln('Program TERMINATING');
  237.                     halt;
  238.                     end;
  239.                 end;
  240.             str_oct1 :=(str_oct1 * 8) + z;
  241.            end;
  242.        str_oct := str_oct1 or most_flag;
  243. end;
  244.