/* * C version of 9_9_1.x68 * * Converts an ASCII string-representation of a decimal number to binary. It * lacks the error detection that assembly allows. */ #include "stdio.h" unsigned short ascii_to_bcd(char * ascii, char * bcdspc) { unsigned short n = 0; for(;;++n) { char c = *ascii++; if (!c) break; c -= 0x30; *bcdspc++ = c; } return n; } long bcd_to_binary(unsigned short n, char * bcdspc) { long acc = 0; unsigned short i; for(i = 0; i < n; ++i) { // should catch overflows, possible in assembly acc *= 10; acc += (unsigned long) bcdspc[i]; } return acc; } static char * ASCIIEX = "1979"; static char BCDSPC[256]; int main(char ** args, int argv) { unsigned short n; long d; n = ascii_to_bcd(ASCIIEX, BCDSPC); d = bcd_to_binary(n, BCDSPC); printf("%d\n", d); }