The code generator, the backend of compiler, generates the object code from Intermediate Representation of source code. This paper suggests a code generation algorithm and implements a code generator based on the algorithm. There have been many researches on the code generation algorithm which empolys the tree-pattern matching algorithm. An efficient tree-pattern matching devised by Ramesh and Ramakrishnan is used in pattern matching. When more than one sequence of pattern are matched, the selection of optimal pattern is generated by dynamic programming technique. The code generator of this paper generates optimal object code in linear time by dynamic programming technique and tree-pattern matching algorthm.