$compact debug pw(79) optimize(0) utility_mod: DO; $INCLUDE (:RMX:inc/rmxplm.ext) DECLARE E$OK LITERALLY '0', FALSE LITERALLY '0', TRUE LITERALLY '0FFH', MAX$STRING LITERALLY '255', WAIT$FOREVER LITERALLY '0FFFFH'; /************************************************************************ * * PROCEDURE: strlen * * INTERFACE VARIABLES: * string$ptr -- a pointer to a NULL-terminated * ASCII string buffer * * ABSTRACT: * strlen finds the first occurrance of a NULL character * in an ASCII string. If no NULL character is found, it * returns a value of zero. Note that this is not an iRMX * style string, in which the count of bytes in the string * is the first byte of the string. * **********************************************************************/ strlen: PROCEDURE (string$ptr) BYTE PUBLIC; DECLARE string$ptr POINTER, string BASED string$ptr (1) BYTE, index BYTE, result BYTE; index = 0; DO WHILE ((string(index) <> 0) AND (index < MAX$STRING)); index = index + 1; END; IF (string(index) = 0) THEN result = index; ELSE result = 0; RETURN (result); END strlen; /************************************************************************ * * PROCEDURE: btoa * * INTERFACE VARIABLES: * chr -- a BYTE containing the binary * value to be converted * string$ptr -- a pointer to a buffer that will * receive the NULL-terminated * ASCII string * * ABSTRACT: * btoa converts a BYTE from its binary value to a * NULL-terminated ASCII string. Note that this is not * an iRMX style string, in which the count of bytes in * the string is the first byte of the string. * **********************************************************************/ btoa: PROCEDURE (chr, string$ptr) PUBLIC; DECLARE chr BYTE, string$ptr POINTER; DECLARE count BYTE, digit (80) BYTE, index BYTE, quotient BYTE, remainder BYTE, string BASED string$ptr (1) BYTE; index = 0; remainder = chr; IF (chr = 0) THEN DO; digit(0) = '0'; digit(1) = 0; index = 1; END; /* chr = 0 */ ELSE /* chr <> 0 */ DO WHILE (remainder > 0); digit(index) = (remainder MOD 10) + '0'; remainder = (remainder - (remainder MOD 10)) / 10; index = index + 1; digit(index) = 0; END; /* DO WHILE (remainder > 0) */ /* This string must be reversed to have the most dignificant * digit output first at the terminal. */ count = 0; DO WHILE (index > 0); string(count) = digit(index - 1); count = count + 1; index = index - 1; END; string(count) = 0; RETURN; END btoa; /************************************************************************ * * PROCEDURE: atob * * INTERFACE VARIABLES: * string$ptr -- a pointer to a NULL-terminated * ASCII string buffer * status$ptr -- a pointer to a WORD that will * receive the status code * * ABSTRACT: * atob converts a NULL-terminated ASCII string to its * binary value. An incorrectly terminated string or * any invalid characters (non-decimal) within the string * will result in a status of 0FFFFH. Note that this is * not an iRMX style string, in which the count of bytes * in the string is the first byte of the string. * **********************************************************************/ atob: PROCEDURE (string$ptr, status$ptr) BYTE PUBLIC; DECLARE string$ptr POINTER, status$ptr POINTER, chars BASED string$ptr (1) BYTE, status BASED status$ptr WORD; DECLARE index BYTE, value BYTE; index = 0; value = 0; status = E$OK; /* Check for an invalid ASCII string. */ IF (((chars(index) < '0') OR (chars(index) > '9')) AND (chars(index) <> 0)) THEN status = 0FFFFH; ELSE /* The first character in the ASCII string is valid. */ DO WHILE ((chars(index) >= '0') AND (chars(index <= '9'))); /* The current character in the ASCII string is valid. */ value = (value * 10) + (chars(index) - '0'); index = index + 1; END; /* Check for an incorrectly terminated string. */ IF (chars(index) <> 0) THEN status = 0FFFFH; RETURN (value); END atob; /************************************************************************ * * PROCEDURE: calc$mean * * INTERFACE VARIABLES: * count -- a BYTE indicating the number * of samples in the data set * buffer$ptr -- a pointer to an array of bytes * containing the data set * * ABSTRACT: * calc$mean calculates the average value of the data set. * **********************************************************************/ calc$mean: PROCEDURE (count, buffer$ptr) BYTE PUBLIC; DECLARE count BYTE, buffer$ptr POINTER, buffer BASED buffer$ptr (1) BYTE; DECLARE index BYTE, sum DWORD; sum = 0; DO index = 0 to (count - 1); sum = sum + buffer(index); END; RETURN (BYTE (sum / count)); END calc$mean; /************************************************************************ * * PROCEDURE: calc$median * * INTERFACE VARIABLES: * count -- a BYTE indicating the number * of samples in the data set * buffer$ptr -- a pointer to an array of bytes * containing the data set * * ABSTRACT: * calc$median calculates the midpoint value of the data set. * **********************************************************************/ calc$median: PROCEDURE (count, buffer$ptr) BYTE PUBLIC; DECLARE count BYTE, buffer$ptr POINTER, buffer BASED buffer$ptr (1) BYTE; DECLARE index BYTE, number$data$points BYTE, values (100H) BYTE; /* Set the count of all data points to zero. */ CALL SETB (0, @values, SIZE (values)); /* Loop once for each data point. */ DO index = 0 to (count - 1); /* Increment the count of this data value. */ values (buffer (index)) = values (buffer (index)) + 1; END; number$data$points = 0; index = 0; DO WHILE (number$data$points <= (count / 2)); /* Increment the number of data points below index value by * the number of data points at index value. */ number$data$points = number$data$points + values (index); /* Check the next data value. */ index = index + 1; END; /* index is now one data value beyond the median. */ RETURN (index - 1); END calc$median; /************************************************************************ * * PROCEDURE: calc$mode * * INTERFACE VARIABLES: * count -- a BYTE indicating the number * of samples in the data set * buffer$ptr -- a pointer to an array of bytes * containing the data set * * ABSTRACT: * calc$mode calculates the most frequently occurring data * value in the data set. * **********************************************************************/ calc$mode: PROCEDURE (count, buffer$ptr) BYTE PUBLIC; DECLARE count BYTE, buffer$ptr POINTER, buffer BASED buffer$ptr (1) BYTE; DECLARE index BYTE, max BYTE, values (100H) BYTE; /* Set the count of all data points to zero. */ CALL SETB (0, @values, SIZE (values)); /* Loop once for each data point. */ DO index = 0 to (count - 1); /* Increment the count of this data value. */ values (buffer (index)) = values (buffer (index)) + 1; END; max = 0; /* Loop once for each possible data value. */ DO index = 0 to 0FFH; /* Check if the count of the current value is greater than * the count of the most frequently occurring value so far. */ IF (values (index) > values (max)) THEN max = index; END; /* max is the value, and values(max) is the number of occurrances * of the data point max. */ RETURN (max); END calc$mode; END utility_mod;