Iran's revolutionary leader Ayatollah Ruhollah Khomeini said tonight that if the United States attacks Iran, the Islamic militants occupying the U.S. Embassy would kill the American hostages inside and blow up the building.

Speaking on television, the 79-year-old Iranian leader for the first time personally endorsed the militants' death threats.

"If the United States wants to attack us," Khomeini said, "we cannot restrain the students, who are very emotional now, from blowing up the embassy. rWe will all be killed, and the hostages will also be killed."

The U.S. government Tuesday raised the possibility of military action against Iran if the remaining American hostages are not freed. A White House statement said a "peaceful solution" was preferable to "other remedies available to the United States." Khomeini's comments tonight appeared to come in response to that statement.

Only hours earlier, the students had released five non-U.S. hostages after a 19-day ordeal -- a Bangladeshi, a Korean, a Pakistani and two Filipinos.

That left 49 Americans facing continued detention and the prospect of espionage trials, an action threatened by their captors unless ousted Shah Mohammad Reza Pahlavi is extradited to Iran from the United States, where he is undergoing cancer treatment.

Late tonight, Khomeini's office accused "American spies" of distributing handbills calling on Iranians to occupy to Soviet Embassy.

The aytollah said in his message that "this new plot" was designed "to divert world opinion" from the "rightful campaign against the Americans." Occupying other embassies amounts to "treason," he said, and "whoever tries such an act is condemned as agent of the (U.S. ) Central Intelligence Agency."

Underlining Khomeini's hard line, radio-television chief Sadegh Ghotbzadeh warned that Iranian patience was wearing thin.

If the United States continues to refuse Iran's extradition demands, he said on television tonight, the threatened spy trials would be expedited.

Student leaders at the embassy said on the telephone they considered all the remaining Americans -- following the release of five women and eight black men earlier this week -- spies who should be tried.

A television announcer said authorities could not rule out executions for any American hostages found guilty of spying by the Islamic revolutionary courts.

Khomeini's new attitude contrasted with his mood of two weeks ago when the students first threatened to kill the hostages in the event of an American rescue operation. Khomeini then admonished them by stating, "We should not even think of killing the hostages."

Indicative of his militancy -- and Shiite Islam's emphasis on death and sacrifice, especially during the current month of mourning called Moharram -- the ayatollah said tonight, "We are not afraid of U.S. warships and planes. tMy nation wants me to pray for them so they can be martyred."

He called on "all Islamic countries' presidents and armies to mobilize and help the revolution" and insisted that "the United States cannot stand against the Moslems now because the world has focused its attention on us."

Speaking from his residence in the holy city of Qom, 80 miles south of Tehran, Khomeini said "all Moslems should rise" against the United States. t

"I am pleased to see the people in Pakistan have risen," he added in an apparent allusion to the sacking of the U.S. Embassy in Islamabad yesterday by Moslem crowds.

Much of his oration was devoted to encouraging Iranian morale.

"Because of propaganda people are afraid of superpowers," he said, "and they think that the superpowers cannot be touched."

He insisted the American people would not allow any military intervention here and suggested that "maybe the blacks in America would also rise."

"All Moslems should join us," he said, "because if this revolution fails, the East and Moslem countries will be destroyed as a result."

The release of hostages earlier this week had raised hopes for efforts here by foreigners and Iranians alike to find a way out of the deepening deadlock.

Involved in such efforts were Acting Foreign Minister Abol Hassan Bani-Sadr, U.S. Rep. George V. Hansen (R-Idaho), and Sean MacBride, a founder of Amnesty International and former Irish foreign minister and a winner of both the Nobel and Lenin peace prizes.

Hansen told reporters he paid his own way and was not here as a "government agent."

"The fact that I'm here and got some appointments," he added, "means I'm two steps ahead of the administration."

After "three weeks into this thing with no apparent progress," he said he hoped his presence here would help "defuse a frustrating situation which might escalate towards the use of force."

As a member of the House Banking Committee, he said he wants to initiate a congressional hearing into Iranian charges about the shah's allegedly criminal activities. He said such a hearing would represent some "middle ground between a kangaroo court and a coverup," he said.

That, he said might "open the door to get the hostages out."

He was rebuffed, however, in efforts to enter the U.S. Embassy and talk with the students. He later conferred with the acting foreign minister.

Bani-Sadr also received MacBride, had come at the invitation of the Iranian government to deliver a message from Amadou Makhtar M'bow, the director general of the United Nations Educational, Scientific and Cultural Organization in Paris.

MacBride was believed to be discussing various formulas designed to bridge the gap between Iran's demand for the shah's extradition and U.S. insistence that hostages be released.

One suggestion was understood to turn on allowing the Geneva-based International Commission of Jurists, over which MacBride presides, to investigate the shah's alleged crimes.

Another hinged on a possible American undertaking to avoid using the embassy here as a "spy center" in the future. The Iranians have charged that the embassy was an espionage center to justify their admitted violation of the Vienna Convention governing diplomatic immunity.

Bani-Sadr, in a news conference, mused aloud that since he and everyone else present were journalists, perhaps "journalists should take the initiative to solve" the crisis.

He said he was prepared to spend all day in a seminar to try to find a solution and promised "all the documents you need." That apparently was an allusion to documents the students claim to have uncovered at the embassy. Only a few of the documents have been released so far.

If the foreign press here were convinced of the shah's misbehavior, he reasoned, then it could persuade world public opinion, which in turn would convince Washington to change it s policy and hand over the former monarch.

Later talking to an Iranian television producer, Bani-Sadr added:

"If we could convince the ordinary American that we are right, and that someone has committed a multitude of crimes, has plundered the public wealth and has betrayed his nation and his history, has in fact betrayed all humanity, including America, and the American people, then we have been successful in breaking out of the isolation imposed on us by the United States in the first days after the attack on the embassy, and in creating a world public opinion emenable to achieving our aim."

In other developments, Nasser Minachi, the national guidance minister, denounced alleged distortion by the foreign press in reporting on the Iranian revolution. From now on, he told the news conference, foreign journalists would be returned to swear an oath to "tell the truth" and not to "fabricate stories discrediting the Islamic revolution.

The oath has been on the books for several months, but has been largely ignored. Similarly, the authorities will insist that foreign journalists traveling outside Tehran be accompanied by official guides. He bridled at suggestions that the proposal was a form of censorship.

Meanwhile, Tehran Radio variously reported that the Iranian Navy and an Air Force base at Shiraz had been put on alert. But the report lost some of its urgency in light of a statement by the Navy commander, Rear Adm. Ahmad Madani, that the Navy had been put on alert on Nov. 4, the day the students occupied the U.S. Embassy.

A caller who telephoned the U.S. Embassy to inquire if the hostages were enjoying any special meal for Thanksgiving was told by one of the Iranian occupiers, "You mean turkey? . . . No, we don't have any turkey right now."