Thousands of Pakistanis, inflamed by rumors that the United States had invaded Mecca, burned the U.S. Embassy here, trapping about 100 Americans and embassy employes for five hours in the heavily secured top-floor code room. One Marine guard was shot and killed during the attack.

With the brick embassy building burning around them, the employes, who had gathered in an upper floor room, escaped tonight through a steel hatch to the roof. Pakistani Army troops had just cleared the roof of demonstrators, who had been firing rifles down the ventilator shafts.

Ambassador Arthur W. Hummel, who was home at lunch when the embassy was invaded shortly after noon, said he spent hours trying to get the Pakistani government to send troops to quell the mob. He said that President Mohammed Zia ul-Haq, the head of Pakistan's Army-run martial law government, insisted as early as 2 p.m. that help was coming.

"The response was not very good," one U.S. diplomat said.

The burning of the embassy, still blazing at midnight, was the worst episode in a wave of anti-American violence that swept through Pakistan today.

Mobs attacked the consulate in Karachi, destroyed the American Library in Lahore and burned the American Center and British library in Rawalpindi. The Amrican Library in Hyderabad was reported to be "in trouble." The Bank of America office in Islamabad was also set on fire.

No serious injuries were reported in the attacks.

All 110 Americans working at the embassy, including those who were outside the embassy building but within the compound, eventually were accounted for. One Agency for International Development employe, Thomas G. Putscher, 32, was taken from the compound and held hostage by students for five hours before being released unharmed.

In Washington Wednesday night, the State Department said plans would be made Thursday to evacuate all "nonessential" embassy personnel and U.S. dependents in Islamabad. A spokeman said the move was "not because of the security situation," which he said Pakistani anthorities had under control, but because the embassy building and housing for at least 50 persons in the compound had been destroyed $.)

The actions come at a time when U.S.-Pakistan relations are at a low ebb over Washington's belief that Pakistan is working on an atomic bomb. But Zia's government is trying to win back Americans' confidence and has been working both publicly and behind the scenes to persuade Iran's ruler Ayatollah Ruhollah Komeini to release all the hostages held in the American Embassy in Tehran.

Embassy public affairs officer James Thurber said rumors had spread through Islamabad and the neighboring city of Rawalpindi this morning that it was American troops who had invaded the Great Mosque in the Islamic holy city of Mecca.

By some accounts, the rumors began with a radio report of undetermined origin. Although Khomeini later accused the United States and Israel of complicity in the Mecca attack, his broadcast was not made over Tehran radio until two hours after the embassy seige here had begun.

The rumors were compounded by reports of President Carter's statement Tuesday night indicating that the United States might use force to free the hostage being held in the embassy in Tehran.

The initial attack on the embassy here came shortly after noon today when about 300 to 400 students from Quidi-Azam University pulled up in buses to the embassy compound, which is protected by a 12-foot-high wall of brick columns, heavy metal grills and a cyclone fence.

Police guards easily dispersed that group, embassy officials said. But it was not long before more buses and vans pulled up around the embassy.

The crowd, mostly students, quickly overwhelmed the police, push down the grills and poured into the compound.

"They were able to physically destroy the fence" Thurber said."The grill and brick columns just went right down, I literally saw one of those go thud under the pressure of the people behind it."

There were no reliable estimates of the number of people who attacked the embassy. One Army officer said 50,000, but a Pakistani reporter on the scene estimated the mob at closer to 20,000. He said the roads from Rawalpindi to Islamabad were filled with buses, truck and vans carrying people to join the attack.

The slain Marine, Cpl. Steve Crowley, 19, of Queens, N.Y., died in the first 15 minutes of the battle when he was shot in the head with a rifle that reportedly had been grabbed from a Pakistani police officer by a demonstrator. Embassy officials said Crowley was on top of the roof of the embassy with another member of the eight-man Marine contingent.

Crowley was reported to have bled to death during the seige. If he could have gotten medical attention at a hospital, persons trapped in the embassy said, he could have been saved.

Information officer William Miller said embassy workers did not take the threat seriously until the mob fired on Crowley. "From that point on," he said, everything disintergrated."

Marines and embassy security guards moved about 100 people into the 20-foot by 30-foot top floor secure room that houses the embassy's coding machines.

Among these in the code room -- called the vault -- were between 50 and 60 Pakistani employes.

From 2 p.m., all lay packed like sardines on the floor while fire raged around. The room was filled with smoke and tear gas fumes, and the tile floor started buckling from the fires below. They could hear the ping of bullets as youths on the roof fired rifles down the ventilator shafts.

"I didn't think we'd ever get out," Miller said.

As the heat got worse, embassy officials got word that the roof had been cleared by Pakistan Army troops, including some who flew over the compound in helicopters.

Some observers, however, felt that heat from the burning embassy building played a greater part in driving the demonstrators from the roof than the troops.

Three Marines, led by Master Sgt. Lloyd Miller, who headed the detachment, pushed up the steel hatch that led from the code room to the roof. They were the first out, with some Pakistani employees, to see if it was safe to make an escape.

Women first, followed by the Pakistani embassy employes and the American men, climbed to the roof and ran for covver. They climbed down two sets of ladders to get to the ground, where Army troops had finally gained control.

"Another 15 minutes and we would have been fried in there," said Lucy Gibbs, an embassy secretary. She added that no one panicked, even though people were gagging and vomiting from the smoke and fumes.

Lloyd Miller was one of the last out, carrying the body of the dead Marine.

For a good part of the siege, political officers in the embassy were calling the Pakistan Foreign Ministry demanding that troops come to rescue them. At the same time, they were in touch by radio with Ambassador Hummel in his home.

"The fact is," Hummel added, "the martial law administration obviously were not prepared for anything of this sort. I don't think anyone was prepared for anything of this sort."

Nonetheless, he and other embassy officials raised questions about the govenment's slow response -- especially since major Army bases are within a 20-minute drive of here.

"I don't want to speculate," Hummel said. "I am trying to be very restrained in my assessment. I don't honestly know if a few hundred regular Army Troops could have helped in any way, but I don't condone the late arrival of the troops. The effective forces did not show up for many hours."

The ambassador said Pakistan government officals said the "restraint" they showed was well advised because of the vast numbers of people and the probability that security officers in the embassy compound has fired on the demonstrators. Hummel and Marines denied Americans fired any shots.

Pakistan officials still were unsure that the city was safe as late as mid-night. Even through all appeared quiet, the Americans rescured from the British Embassy, surrounded by armed gaurds while government security officials checked to see if their homes were safe.

Some of the diplomats had blood on their shirts, one Marine appeared dazed. Others dozed under blankets on reception room couches.

There were no reports last night that any of the demonstrators had been arrested. A noncommissioned Army officer guarding the still-burning embassy late tonight said, "with so many it is impossible."

While President Zia went on television tonight to urge calmness, government information officers said nothing. Ahmed Hassan Sheik, thee government's principal information officer, said, "I have no idea what happened here." CAPTION:

Picture 1, CPL. STEVEN CROWLEY . . . slain on embassy roof; Picture 2, A Pakistani Army helicopter flies over burning U.S. Embassy during attack by anti-American demonstrators.AP; Map, Crisis in the Middle East By Richard Furno -- The Washington Post; Picture 3, Arthur W. Hummel, Jr. . . . U.S. ambassador to Pakistan; Picture 4, Pakistani demonstrators burn American Center in Rawalpindi in series of protests against alleged United States Involvement in Mecca attack. AP