The amount of particles (atoms, molecules, ions, subatomic particles) in one mole is a constant known as Avogadro's constant. The mass of any species amounting to one mole depends on the species and is known as the molar mass (in terms of g/mole) This allows for the interconversion between mass and moles. If you have a certain mass of particles (12 g of Mg), you can calculate the amount of moles from that (12 g/24 g/mole is 0.5 moles). If you have a certain amount of moles, you can calculate the mass from that (0.5 moles * 40 g/mole is 20 g).
Your idea is good, but you explained it incorrectly. You do not search for 'what is one mole for 12 g of Mg', but 'how many moles are in 12 g of Mg'.