> From: John Dammeyer > >[cut] > >DAA is a bit tekno for this task. My philosophy when it comes to > >binary to decimal decoding is that decimal is almost always used > >by humans (or printers) and hence speed is not an overriding concern. > >I would go for the most compact code. Successive subtraction of > >powers of ten would be my first choice. Actual division is > >certainly unnecessary. Converting an unsigned 16-bit number to > >5-digit decimal requires a maximum of 27 2-byte subtractions, > >20 1-byte subtractions and 5 additions. > > Please do post it. ie: Put your opcodes down as evidence. So you want me to put my code where my mouth is? Well I've never needed to actually do this (let alone on a '57, since I've always used the '74 and '84). Time does not permit me to give a real implementation so you will have to be satisfied with pseudo C-code. This code would translate in a fairly straightforward way into 16C57 code. Obviously, some of the code could be 'commoned'. Contrary to my previous post this will take a worst case, for conversion of the number 5999x, of 26 2-byte subs, 10 1-byte subs, 3 2-byte adds and 2 1-byte adds -- not counting the digit increments. The subtractions could be replaced by additions of -ve numbers if that makes it easier. The conversion procedure is broken up into very small bites so that your non-interrupt-capable processor will have only a small latency between checking for I/O etc. enum state { start, finish, sub10k, sub1k, sub100, sub10 } int binary int divisor char digit[5] void bin2dec() { switch (state) { case start: state = sub10k digit[0] = digit[1] = ... = digit[4] = '0' divisor = 10000 return case sub10k: binary -= divisor if (binary < 0) [i.e. MSB of binary changed from 0 to 1] { binary += divisor state = sub1k divisor = 1000 } else digit[0]++ return case sub1k: binary -= divisor if (binary < 0) { binary += divisor state = sub100 divisor = 100 } else digit[1]++ return case sub100: binary -= divisor if (binary < 0) { binary += divisor state = sub10 divisor = 10 } else digit[2]++ return case sub10: binary -= divisor [S.B. - single byte op] if (binary < 0) { binary += divisor [S.B.] state = finish digit[4] += binary [S.B.] } else digit[3]++ return } } main() { ... binary = [binary value to decode] state = start while (state != finish) { bin2dec() [do something else e.g. service I/O] } print("decimal = %c%c%c%c%c", digit[0], digit[1], ..., digit[4]) } Regards, SJH Canberra, Australia