If you start with the mass of a substance, divide it by its molar mass to find the number of moles of the substance. Then, multiply the number of moles by Avogadro's number, 6.022*10^23, which gives the number of particles per mole of a substance.
Example:
Find the number of atoms in 10 g of CO2.
The mass of a single mole of CO2 is 54 g/mol. You can get this value by adding up the molar masses of the atoms in CO2(12+16+16).
Then divide 10 g by 54 g/mol to get the number of moles of CO2 in 10 g, which is 0.19 mol.
Finally, multiply 0.19 by Avogadro's number to get 1.1*10^23 molecules of CO2.