President Abol Hassan Bani-Sadr warned neighboring Iraq Friday that Iranian forces would repel its troops and pursue them across the border in the event of any attack.

The Iranian president also called on Iraqi troops to desert and follow Iran's brand of Islamic revolution as preached by Ayatollah Ruhollah Khomeini, this nation's most powerful leader.

Bani-Sadr made the warning in a speech to a huge crowd at Tehran University march by hundreds of thousands of Iranians to demonstrate national unity against Iraq and the United States.

This latest salvo in Iran's war of words with Iraq came as Iranian news media reported what appeared to be minor clashes on the border between Iraqi troops and Iranian Revolutionary Guards.

On another front, the Iranian government sought to thwart U.S. attempts to enlist Western allies in joint economic sanctions against Tehran because of the Iranian refusal to release U.S. hostages seized in November.

Foreign Minister Sadegh Ghotbzadeh called in the ambassadors of the nine European Common Market countries and of Australia, New Zealand and Japan to urge them not to join in imposing the sanctions, which the United States initiated Monday after 11th-hour efforts to transfer the American hostages to Iranian government control broke down.

Ghotbzadeh's summons came in response to an appeal Thursday by the Common Market countries for the release of the hostages.

Diplomatic sources said Ghotbzadeh told the envoys that sanctions would only make Iranians more defiant on the hostage issue. He also hinted at impending new efforts to obtain the Americans' transfer to government custody. i

Today's demonstration rivaled some of the protest marches leading up to Iran's February 1979 revolution as Iranians turned out in massive numbers to answer Bani-Sadr's call for a rally against Iran's two main enemies.

But while speakers attacked Iraq and repeated some of the standard anti-American rhetoric, much of the oratory was directed against Iran's northern neighbor, the Soviet Union.

Thursday night, Iran's ruling Revolutionary Council decided after a meeting with the Cabinet that the country would boycott the Summer Olympics in Moscow, the official Iranian news agency reported Friday. It made no mention of U.S. efforts to spearhead such a boycott. Iran does not have a strong all-round Olympic team but its wrestlers and its soccer team, which played in the 1978 World Cup in Argentina, had been touted as candidates for medals in Moscow.

The Iranian ambassador to the Soviet Union, Mohammed Mokri, called upon the Soviets to halt arms supplies to Iraq as the Kremlin appeared to be moving toward support for Iran in its dispute with its Middle East neighbor, Reuter reported from Moscow.

[Mokri told a news conference that his appeal was a friendly request that "we hope . . . will be understood by the U.S.S.R." He did not indicate whether a formal request would be made to the Soviet government, but said it might be put "at the appropriate time."]

At Friday's rally, a leading rightist Moslem clergyman and Revolutionary Council member, Hashemi Rafsanjani, demanded that Moscow pull its troops out of Afghanistan, where Moslem insurgents are fighting a guerrilla war against the Soviet troops and forces loyal to the communist Afghan government.

Rafsanjani, a close associate of Khomeini, stressed that Iran's move away from the United States does not mean it would fall in the lap of the Soviet Union. He also said that Iran would not resume sales of gas via pipeline to the Soviet Union. Negotiations with Moscow on a new price for the gas broke down earlier this year, with the Soviets rejecting Iran's price as too high.

Addressing the rally from a podium with a large picture of Khomeini in the background, Bani-Sadr said, "If Iraqi soldiers attack us, Iranian soldiers and Revolutionary Guards will repel them and will certainly chase them inside enemy territory. I cannot prevent this."

Bani-Sadr said Iraqi President Saddam Hussein had sent envoys to Tehran three times to seek a reconciliation but that he had told them, "I will never work in the favor of the Iraqi government against the Iraqi nation."

Iran began openly encouraging the overthrow of Hussein's Baath Party government in Baghdad after Iraq demanded three weeks ago that Iran withdraw from three Persian Gulf islands occupied by forces of the now-deposed shah in 1971.

Relations deteriorated sharply in subsequent weeks as Iraq blamed Iran for an assassination attempt, in which Iraqi Deputy Prime Minister Tareq Aziz was wounded, and for several bombings in Baghdad that left at least three Iraqis dead. Earlier this week, Iraq began deporting ethnic Iranians, driving them in trucks to within a few miles of the border and then forcing them to walk across with hardly any of their possessions.

In his speech, Bani-Sadr alluded to air battles near the border. Reports from the border area also spoke of artillery duels, and Revolutionary Guards at the Iranian frontier town of Qasr-e-Shirin said Iraqi shellings wrecked three Iranian tanks. The guards said that Iranian forces returned the fire.

The official Pars News Agency reported that Iraqi forces had launched a rocket attack on a village near Qasr-e-Shirin. There was no mention of casualties.

The agency also reported that a bomb explosion Thursday night in the oil refinery town of Abadan in southwestern Iran killed one person and injured 20.