A Long Integer Package for BDS-C Rob Shostak August, 1982 This package adds long (32-bit) signed integer capability to BDS-C much in the same spirit as Bob Mathias's floating point package. Addition, subtraction, multiplication, division, and modulus routines are provided as well as comparison, assignment, and various kinds of conversion. Each long integer is stored as an array of four characters. integer x is thus declared by: A long char x[4]; The internal representation is two's complement form, with the sign (most significant) byte as the first byte of the array. For most purposes, however, you needn't be concerned with the internal representation. Most of the routines that operate on longs take three arguments, the first of which points to where the result is to be stored, and the other two of which give the operands. For example, given longs x, y, and z (all declared as char[4]), ladd(z,x,y) computes the sum of x and y and stores it into z, which is returned as the value of the call. Note that the result argument may legitimately be the same as one (or both) of the operand arguments (for instance, ladd(x,x,x) does "the right thing"). The package is written partly in C and partly (for speed and compactness) in 8080 assembly language. To use it, simply link LONG.CRL into your program. A description is given below for each routine. ladd(r,op1,op2) char r[4]; Stores the sum of longs op1 and op2 into r, and returns r. op1 or op2 may be used for r. lsub(r,op1,op2) char r[4]; char op1[4],op2[4]; Similar to ladd but computes op1 - op2. lmul(r,op1,op2) char r[4]; char op1[4],op2[4]; Similar to ladd but computes op1 * op2. ldiv(r,op1,op2) char r[4]; Similar to ladd but computes the integer quotient op1 / op2. If op2 is zero, zero is computed as the char op1[4],op2[4]; result. lmod(r,op1,op2) Similar to ladd but computes op1 mod op2. char r[4]; is zero, zero is computed as the result. char op1[4],op2[4]; If op2 lcomp(op1,op2) Compares longs op1 and op2, and returns one of char op1[4],op2[4]; (the ordinary integers) 1, 0, -1, depending on whether op1 > op2, op1 = op2, or op1 < op2, respectively. lassign(dest,source) Assigns long source to long dest, and returns char source[4],dest[4]; pointer to dest. itol(l,i) char l[4]; int i; Stores the long representation of the 16-bit integer i into l, and returns l. ltoi(l) char l[4]; Returns the integer representation of long l. l must be representable as a 16-bit integer. atol(l,s) char l[4]; char s[]; Stores the long representation of the Ascii string s into l, and returns l. The general form of s is a string of decimal digits, possibly preceded by a minus sign, and terminated by any non-digit. ltoa(s,l) char s[]; char l[4]; Stores the Ascii representation of long l into string s, and returns s. The representation consists of a null-terminated string of Ascii decimal digits, preceded by a minus sign if l is negative. s must be large enough to receive the conversion. ltou(l) char l[4]; Converts long l to unsigned (by truncation). l must be representable as a 16-bit unsigned number. This function is identical in effect with ltoi; the two may be used interchangably. utol(l,u) char l[4]; unsigned u; Stores the long representation of unsigned u in l, and returns pointer to l. Implementation Details: Most of the work in the routines above is done by a single 8080 assembly-language function called long, the source for which is found in the file LONG.CSM. The remainder of the package resides in LONG.C. Note that most of the primitives described above simply call long, passing it a function code (that tells it what operation is to be performed) together with the arguments to be manipulated. The file LONG.CRL contains both the assembled function long and the compiled functions given in LONG.C. If you wish to make changes to long, edit the file LONG.CSM, then reassemble it using the CASM facility. Save the resulting .CRL image into some file other than LONG.CRL (say LONG1.CRL), then use CLIB to transfer the new version of long in LONG1.CRL to LONG.CRL. If you wish to change some of the functions in LONG.C, first rename that file to LONG1.C, make the edits, recompile, then use CLIB to transfer the new versions of the functions you changed to LONG.CRL.