Ayatollah Ruhollah Khomeini said today for the first time that the Soviet Union represents as great a threat to Iran as the United States does, and President Abol Hassan Bani-Sadr demanded that Moscow unconditionally withdraw its troops from Afghanistan " as rapidly as possible."

The clearly anti-Soviet tone of both speeches made a hardening by Iran's top leadership. Khomeini, Iran's chief spiritual leader, has condemned both superpowers, but he has attempted to blame the United States, which he calls the "great Satan," for most of the country's woes.

The prevalent anti-American sentiment of the Iranian leadership following the revolution overthrowing the shah has showed evidence of changing since the Soviets invaded Afghanistan in late December.

Bani-Sadr has accused the Soviets of seeking to seize parts of Iran and has offered to aid Afghan rebels. These shifting attitudes were also echoed yesterday when the defense minister said Iran would expect U.S. military aid if the Soviets attacked.

Khomeini's speech, marking the Persian new year, was devoted mainly to instructing the president and ruling Revolutionary Council to instill discipline in the armed forces, police, press, Revolutionary Guards, government administration, universities and courts.

But Khomeini's message, read by his son Ahmad at the Behest-Zahra cemetery where many of the revolution's martyrs are buried, appeared to fall short of the detailed mandate Bani-Sadr had sought to reinforce his authority against competing power centers.

Specifically, the message made no mention of the Moslem militants who are still holding the estimated 50 Americans hostage at the U.S. Embassy despite the Bani-Sadr's repeated efforts to enlist Khomeini's support in asserting control there.

Apparently balancing that fact was Khomeini's uncharacteristic omission of the demand for the return of the deposed shah. Mohammad Reza Pahlavi, and his fortune as well as admission by the United States of guilt for its past interference in Iran's domestic affairs.

Khomeini provided no explanation for his relatively mild condemnation of the United States or his outspoken criticism of the Soviet Union.

But analysts noted the failure earlier this week of bilateral negotiations during which Iran unsuccessfully sought to persuade a Soviet delegation to quintuple the price of Iranian natural gas used in much of the southern Soviet Union.

Khomeini's criticism was also expected to provoke reactions from the Iranian left -- the pro-Moscow Tudeh Party as well as the Marxist-Leninist Segayan and the leftist Islamic Mujaheddin.

They have concentrated their criticism on the United States. Khomeini also attached the leftists as "dictators" bent on stifling debate once in power and ordered Iranians to "fight them in all fields."

"We are in conflict with international communism to the same extent as we are against Western exploiters, headed by America and Israel." Khomeini said without specifically naming the Soviet Union. "You should know that the danger of communist power is not less than that of American."

"I severely condemn the brutal intervention in Afghanistan by looters and occupiers," he said. "I hope the . . . Moslem people of Afghanistan will soon achieve victory and real independence, and we'll get rid of these so-called supporters of the working class."

Addressing a crowd of about 50,000, Bani-sadr told the Soviets that "if you are listening, we call on you to declare your readiness unconditionally to withdraw from Afghanistan as rapidly as possible. It does not require negotiation."

The president recounted how the Soviets had rebuffed his suggestion for a five-member international commission to verify Moscow's claims that its intervention in Afghanistan was motivated by American, Pakistani and Chinese interference in that country.

"If America is not there," he added, "and this is just an excuse on your part, you can pull out."

Informed by the Soviets that the Afghanistan government opposed Soviet troop withdrawal, the president said, "We cannot put up with the presence of your forces in our vicinity."

"As an Islamic duty, we must advise against intervention by foreign troops in a Moslem's country. Second, as a revolutionary nation we cannot tolerate your presence, so you must withdraw from Afghanistan."

He balanced his anti-Soviet attack by accusing the United States of "instructing" Iraq to blow up oil and gas pipelines in the southwestern petroleum-producing province of Khuzestan, once claimed by Baghdad.

Following Khomeini's directive to "establish law and order," Bani-Sadr said industrial production had fallen and prices had risen. He stressed the need for economic recovery to avoid dangerous public frustration.

"Then you will come and grasp your throats," he said, "and since I don't want my throat grasped, I will try to do my best."

In his remarks on discipline, Khomeini outlined a 13-point program and said this was "the year when security should be restored and the noble people of Iran should live in peace."

Khomeini specifically backed up Bani-Sadr's military authority.

"I will no longer put up with indiscipline," in the armed forces, he said, warning that "any one who disrupts," its affairs "should be exposed as a counterrevolutionary."

On Saturday, 500 Air Force sergeants demonstrated against Lt. Gen. Amir Bargheri, the Air Force commander. He later was transferred quietly to another position.

Branching out from his usual concerns, Khomeini indirectly warned workers to end strikes and slowdowns. He denounced the armed leftist groups as "dictators" responsible for misleading the workers.

He also called for more discipline among the Revolutionary Guards and complained of "the amazing number of slowdowns" among police, especially in Khuzestan.

The Revolutionary Guards, often accused to dispensing arbitrary justice, were told to follow "constitutional law" and to "judge with patience."