Is it illegal to write on a flag?:
1) 
It is protected by the first amendment of the Bill of Rights:

'Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the Government for a redress of grievances.'
-Speech also implies expression

2)
Desecration of the U.S. flag so far is legal.  Although recently (2005) the House passed a law making in illegal to desecrate the American flag.  It is still awaiting further approval through our legal system.