WASHINGTON (AP) _ A computer operator in the Houston constable's office who said she hoped someone would assassinate President Reagan was unlawfully fired, the Supreme Court ruled today.

By a 5-4 vote, the justices said the woman's free-speech rights were violated.

Justice Thurgood Marshall, writing for the court, said, ''Vigilance is necessary to ensure that public employers do not use authority over employees to silence discourse, not because it hampers public functions but simply because superiors disagree with the content of employees' speech.''

Marshall said a threat to kill the president would be grounds for firing a public employee. But he said the comment by computer operator Ardith McPherson was not intended as a threat.

Ms. McPherson's First Amendment right to express herself on a subject of public interest must be weighed against her employer's need to maintain order and efficiency, Marshall said.

''While McPherson's statement was made at the workplace, there is no evidence that it interfered with the efficient function of the office,'' he said. ''Where, as here, an employee serves no confidential, policy-making or public contact role, the danger to the agency's successful function from that employee's private speech is minimal.''

Ms. McPherson, 19 at the time and a probationary employee, was on duty in Constable Walter H. Rankin's office on March 30, 1981, the day Reagan was shot here by would-be assassin John Hinckley.

While listening to radio reports about the incident, Ms. McPherson remarked to a co-worker, ''I hope if they go for him again they get him.''

Rankin was told of the comment and called Ms. McPherson into his office and asked her if she meant it.

There was conflicting testimony about her response. She said she did not mean the remark literally. Rankin testified that she said she did.

Rankin fired her and Ms. McPherson sued him and Harris County, Texas, which runs the constable office.

A federal judge threw out the lawsuit, but it was reinstated by the 5th U.S. Circuit Court of Appeals

The Supreme Court today upheld the appeals court ruling, ordering the judge in the case to come up with an appropriate remedy for Ms. McPherson.

In a dissenting opinion, Justice Antonin Scalia said it was reasonable for Rankin to fire Ms. McPherson.

Quoting a lawyer for the constable, Scalia said, ''No law enforcement agency is required by the First Amendment to permit one of its employees to 'ride with the cops and cheer for the robbers'.''

Scalia said law enforcement officials should be allowed to fire someone who makes a threatening remark against the president.

He was joined by Chief Justice William H. Rehnquist and Justices Byron R. White and Sandra Day O'Connor.

Joining Marshall were Justices William J. Brennan, Lewis F. Powell, John Paul Stevens and Harry A. Blackmun.

The case is Rankin vs. McPherson, 85-2068.