  We construct the partition function of 1/8 BPS dyons in type II string theory
on T^6 from counting of microstates of a D1-D5 system in Taub-NUT space. Our
analysis extends the earlier ones by Shih, Strominger and Yin and by Pioline by
taking into account the walls of marginal stability on which a 1/8 BPS dyon can
decay into a pair of half-BPS dyons. Across these walls the dyon spectrum
changes discontinuously, and as a result the spectrum is not manifestly
invariant under S-duality transformation of the charges. However the partition
function is manifestly S-duality invariant and takes the same form in all
domains of the moduli space separated by walls of marginal stability, -- the
spectra in different domains being obtained by choosing different integration
contours along which we carry out the Fourier transform of the partition
function. The jump in the spectrum across a wall of marginal stability,
calculated from the behaviour of the partition function at an appropriate pole,
reproduces the expected wall crossing formula.
