Pledging total fidelity to the policies of slain president Anwar Sadat and the "sword of the law" to those resorting to violence, Mohammed Hosni Mubarak, 53, was sworn in before a heavily guarded National Assembly today as Egypt's fourth president.

The ceremony took place a day after a nationwide referendum overwhelmingly approved him as Sadat's successor and eight days almost to the hour after the former president's assassination at a military parade by a small group of Moslem fanatics.

Wearing a black suit and tie in mourning for Sadat, Mubarak told his shaken nation that Egypt was in "deep pain" and the situation of the country was "critical." But, he added, "we shall not give in; we shall not surrender."

He promised to remain loyal to the principles and commitments of the late president, specifically mentioning the American-sponsored Camp David accords and Egypt's peace treaty with Israel. He also declared Egypt's determination not to go back on its decisions, "even if they were disliked by bigger powers."

This appeared to be a reference to the Soviet Union, which has vehemently attacked the peace accords and the treaty.

"Camp David, and peace with Israel, will continue in all its letter and commitment," he said, seeking to reassure both the United States and Israel. "We shall continue the autonomy negotiations to put the Palestinians on the beginning of the road to get their lawful rights."

He also said he had received "categoric assurances," presumably from the Israeli government, that Israel's withdrawal from the occupied Sinai Peninsula would take place on schedule, "without any delay."

On April 25, he said, the Egyptian flag will be "fluttering over Rafah, Sharm el-Sheikh and every inch of our sacred Sinai territory."

Shortly after the speech, Mubarak appointed himself prime minister and said he would retain Sadat's Cabinet, the official Middle East News Agency reported.

Mubarak was still wearing a small bandage on his left hand from an injury he received in the attack on Sadat. The assassination clearly was still on his mind and at one point he broke briefly into tears when he read the words:

"But such is my fate that I should stand in front of you, in his Sadat's place. The order has been given by the people of Egypt to choose me to follow him and to continue his march. In this responsibility, which is great and heavy for me, I shall always continue in his principles."

"I swear by almighty God to preserve the constitution and the law and safeguard the security of the state and the safety of its territory," Mubarak pledged before the 392-member assembly.

Sudanese President Jaafar Nimeri, a close friend of Sadat, gave a moving eulogy to the late Egyptian leader, calling on the two Nile Valley nations to continue their close cooperation.

Among the explicit assurances Mubarak gave Egyptians and Westerners was one to continue unaltered Sadat's "open-door" policy seeking to liberalize Egypt's once heavily socialized economy. He said not only would the door remain open but he would encourage more Arab and other foreign investment to accelerate the pace of economic development.

Mubarak, himself a military man, had reassuring words as well for the armed forces and said the presence of "one traitor" among them should not be allowed to blemish their record of valor in numerous wars. The "traitor" he referred to was the reported leader of the Moslem fanatics who killed Sadat, Lt. Khaled Ahmed Shawki Islambouly, said by the government to have been the only active officer involved. Islambouly is reported to be in a hospital suffering from wounds he suffered in the attack and its aftermath.

Mubarak gave Egyptians the first hint of the tough stand he is widely expected to take in cracking down on Moslem extremists, who were involved in the assassination of Sadat, a bloody two-day insurrection in the city of Asyut in southern Egypt last week and a shootout with police near the pyramids yesterday.

He said no one, no matter what his rank, wealth or privilege, was above the "sword of the law."

"I say from here," he added, "I say to all those who exploit the freedom of the people and their safety, that the fire of the people is worse, and that anyone who thinks violence against the people, the decision of the people, will not be turned back and they shall not escape fierce punishment."

He promised peace, security and stability but said his powers under the state of emergency declared after Sadat's killing would be resorted to only "in necessities" and "limited to the minimum." Already, the Interior Ministry has issued an order to all policemen to shoot on sight anyone attempting to stir up disorder.

Mubarak exuded strength, and several Egyptians said his tone of voice, his physical stature and even one expression he used ("Oh, citizens") stirred memories of the late president Gamal Abdel Nasser. But his delivery was more concise and less emotional than that of Nasser.

Just how popular Mubarak will be remains to be seen. Yesterday's referendum gave him 98.46 percent of the votes cast. The official turnout for the government-organized plebiscite was only 81 percent of the 12 million eligible voters, with roughly 2.3 million staying away from the polls.

The number of registered "no" votes was 149,650.

Reporters who monitored the voting both in Cairo and in the villages of the delta found a generally far larger turnout than in another referendum held three weeks ago.

Mubarak, a tough-minded former bomber pilot and Air Force commander, is the fourth Egyptian president since the overthrow of the old monarchy in July 1952 by a group of officers led by Nasser and Sadat.

The first president, Mohammed Naguib, was an outsider to the "Free Officers" group that staged the coup and lasted less than two years before Nasser took over. He ruled Egypt with extraordinary popularity at home for 18 years until his death in September 1970.

Sadat followed and remained as president for 11 years until his assassination last Tuesday.

While both Nasser and Sadat belonged to the Free Officers who led the 1952 revolution, Mubarak was only 24 when it began and belongs to a different generation of Egyptian military leadership. He is regarded as one of the heroes of the 1973 war with Israel, which Egyptians view as their greatest modern military victory, despite its ending in near disaster for the Egyptian Army.

Mubarak was then Air Force commander and he was handpicked by Sadat in 1975 to be groomed for the job he has inherited so suddenly.