Sample Program [ Compiler Library/XL Reference Manual ] MPE/iX 5.0 Documentation
Compiler Library/XL Reference Manual
Sample Program
This section contains a sample program to demonstrate how to call the
packed-decimal library routines. The program takes three large numbers
(U.S. federal government annual appropriations for the years 1977 through
1979), converts them from ASCII to packed-decimal, adds them, converts
the sum to ASCII, and prints the answer.
There are four versions of the program. The first example is written in
HP C/XL, the second in HP Pascal/XL, the third in HP FORTRAN 77/XL, and
the fourth in SPL/V, using ASSEMBLE statements. The SPL/V example is
included to show how the procedure calls would have been done on an MPE V
based HP 3000. This may be helpful to users converting SPL code to C,
Pascal, or FORTRAN.
Program Output
All four versions of the program produce the following output:
1977 046655980996406
1978 050778229148999
1979 056396083378825
Total 153830293524230
HP C/XL Example
/*
* This program calculates total appropriations by the
* United States federal government for the years 1977-1979
*/
#pragma intrinsic HPPACADDD
#pragma intrinsic HPPACCVAD
#pragma intrinsic HPPACCVBD
#pragma intrinsic HPPACCVDA
typedef struct {char c [15]} ascii_num_15; /* 15-digit numbers in ASCII */
typedef struct {char c [8]} packed_num_15; /* 8-character field holds */
/* 15 digits and sign
static ascii_num_15 yearly [3] = /* Annual appropriations, in cents */
{"046655980996406",
"050778229148999",
"056396083378825"};
main ()
{
short int zero=0;
int year;
packed_num_15 packed_sum, packed_temp;
ascii_num_15 ascii_sum;
HPPACCVBD (&packed_sum, 15, &zero, 1); /* Initialize sum */
for (year=1977; year<=1979; year++)
{
printf ("%d %.15s\n", /* Echo annual data */
year, &yearly [year-1977]);
HPPACCVAD (&packed_temp, 15, /* Convert ASCII to decimal */
&yearly [year-1977], 15);
HPPACADDD (&packed_sum, 15, /* Add to sum */
&packed_temp, 15);
}
HPPACCVDA (&ascii_sum, 15, /* Convert sum to ASCII, */
&packed_sum, 1); /* suppressing plus sign */
printf ("Total %.15s\n", &ascii_sum);
}
HP Pascal/XL Example
{
This program calculates total appropriations by the
United States federal government for the years 1977-1979
}
$notes off$
$standard_level 'ext_modcal'$
program appropriations (output);
type
ascii_num_15 = { 15-digit numbers in ASCII }
packed array [1..15] of char;
nibble = 0..15; { Half a byte, holds a digit or sign }
packed_num_15 = { Holds 15 digits and sign }
crunched record
digits : crunched array [1..15]
of nibble;
sign : nibble;
end;
year_type = 1977..1979;
yearly_table = array [year_type]
of ascii_num_15;
const
yearly = yearly_table { Annual appropriations, in cents }
['046655980996406',
'050778229148999',
'056396083378825'];
var
zero : shortint;
year : year_type;
packed_sum, packed_temp : packed_num_15;
ascii_sum, ascii_temp : ascii_num_15;
procedure HPPACADDD; intrinsic;
procedure HPPACCVAD; intrinsic;
procedure HPPACCVBD; intrinsic;
procedure HPPACCVDA; intrinsic;
begin
zero := 0;
HPPACCVBD (packed_sum, 15, zero, 1); { Initialize sum }
for year := 1977 to 1979 do
begin
writeln (year:1, ' ', yearly [year]); { Echo annual data }
ascii_temp := yearly [year]; { Can't pass const as var }
HPPACCVAD (packed_temp, 15, ascii_temp, 15); { Convert ASCII to decimal}
HPPACADDD (packed_sum, 15, packed_temp, 15); { Add to sum }
end;
HPPACCVDA (ascii_sum, 15, { Convert sum to ASCI, }
packed_sum, 1); { suppressing plus }
writeln ('Total ', ascii_sum);
end.
HP FORTRAN 77/XL Example
C
C This program calculates total appropriations by the
C United States federal government for the years 1977-1979
C
$STANDARD_LEVEL SYSTEM
SYSTEM INTRINSIC HPPACADDD, HPPACCVAD, HPPACCVBD, HPPACCVDA
INTEGER*2 ZERO
INTEGER YEAR
CHARACTER*15 YEARLY (1977:1979) ! 15-digit numbers in ASCII
CHARACTER*15 ASCII_SUM
CHARACTER*8 PACKED_SUM ! 8-character field holds
CHARACTER*8 PACKED_TEMP ! 15 digits and sign
DATA YEARLY/ ! Annual appropriations, in cents
1 '046655980996406',
2 '050778229148999',
3 '056396083378825'/
DATA ZERO/0/
CALL HPPACCVBD (PACKED_SUM, 15, ! Initialize sum
> ZERO, 1)
DO YEAR = 1977, 1979
PRINT *, YEAR, ' ', YEARLY (YEAR) ! Echo annual data
CALL HPPACCVAD (PACKED_TEMP, 15, ! Convert ASCII to decimal
> YEARLY (YEAR), 15)
CALL HPPACADDD (PACKED_SUM, 15, ! Add to sum
> PACKED_TEMP, 15)
END DO
CALL HPPACCVDA (ASCII_SUM, 15, ! Convert sum to ASCII,
> PACKED_SUM, 1) ! suppressing plus sign
PRINT *, 'Total ', ASCII_SUM
STOP
END
SPL/V Example
<<
* This program calculates total appropriations by the
* United States federal government for the years 1977-1979
>>
BEGIN
BYTE ARRAY YEARLY (0:44) := << Annual appropriations, in cents >>
"046655980996406",
"050778229148999",
"056396083378825";
INTEGER ZERO:=0, YEAR;
BYTE ARRAY PACKED'SUM (0:7); << 8-character field holds >>
BYTE ARRAY PACKED'TEMP (0:7); << 15 digits and sign >>
BYTE ARRAY ASCII'SUM (0:14); << 15-digit number in ASCII >>
ARRAY PRINT'BUFFER'W (0:10);
BYTE ARRAY PRINT'BUFFER'B (*) = PRINT'BUFFER'W;
INTRINSIC ASCII, PRINT;
<< Initialize sum >>
TOS := @PACKED'SUM; TOS := 15;
TOS := @ZERO; TOS := 1;
ASSEMBLE (CVBD);
FOR YEAR := 1977 UNTIL 1979 DO
BEGIN
<< Echo annual data >>
ASCII (YEAR, 10, PRINT'BUFFER'B);
MOVE PRINT'BUFFER'B (4) := " ";
MOVE PRINT'BUFFER'B (7) := YEARLY (15 * (YEAR-1977)), (15);
PRINT (PRINT'BUFFER'W, -22, %40);
<< Convert ASCII to decimal >>
TOS := @PACKED'TEMP; TOS := 15;
TOS := @YEARLY (15 * (YEAR-1977)); TOS := 15;
ASSEMBLE (CVAD);
<< Add to sum >>
TOS := @PACKED'SUM; TOS := 15;
TOS := @PACKED'TEMP; TOS := 15;
ASSEMBLE (ADDD);
END;
<< Convert sum to ASCII, suppressing plus sign >>
TOS := @ASCII'SUM; TOS := 15;
TOS := @PACKED'SUM;
ASSEMBLE (CVDA ABS);
MOVE PRINT'BUFFER'B := "Total ";
MOVE PRINT'BUFFER'B (7) := ASCII'SUM, (15);
PRINT (PRINT'BUFFER'W, -22, %40);
END.
MPE/iX 5.0 Documentation