A algorithm called the O-algorithm is introduced for automatic test program generation of microprocessors in a user environment. To eliminate redundant tests, a weighted-digraph model is used to model the signal flow of the general microprocessors. Improved functional fault models of microprocessors are derived from the Turing machine model. The O-algorithm is then constructed on the basis of the signal flow model and functional fault models. Simulation has shown that the fault coverage is better than 97%.