A sharply divided Supreme Court yesterday upheld a 40-year sentence for a Virginia man convicted of possession and sale of nine ounces of marijuana, and issued a stern warning to federal judges not to interfere with sentences handed out under state laws.

The court also issued an unusually strong rebuke to the 4th U.S. Circuit Court of Appeals in Richmond, which had wiped out the sentence as grossly disproportionate to the crime. The justices said the appellate court had "ignored" a Supreme Court precedent that "must be followed by the lower federal courts no matter how misguided the judges of those courts may think it to be."

Three dissenters, in a statement issued by Justice William J. Brennan Jr., called the 40-year term a "cruel and painful excess" and said the decision was "profoundly disturbing."

Brennan, joined by Justices Thurgood Marshall and John Paul Stevens, attacked the majority for issuing yesterday's five-page unsigned opinion without hearing full arguments. The practice, increasingly frequent on sensitive cases, is a "patent abuse of our judicial power," the dissenters said.

The opinion reflected the court's increasing inclination to stop federal intervention in the state courts, as well as its tolerance of hard-line sentencing laws. In 1980, the court upheld a life term for a man convicted under a chronic offender statute of $120.75 worth of fraud.

The Virginia case was heavily publicized in the Washington area when it arose in 1973 in part because of the harshness of the sentence and because it had racial overtones. The convicted man, Roger Trenton Davis, now 35, is a black man who lived in the small rural southwestern town of Wytheville. He had been harassed by local whites for dating and then marrying a white woman, and a cross was burned on his lawn.

Police started investigating Davis, who had already been convicted of selling LSD tablets, after a prison inmate tipped them that his wife was being supplied marijuana by Davis. Police hid a microphone on the inmate, who then bought marijuana from Davis. A jury ultimately convicted Davis of two counts of sale and possession of nine ounces. He was given two consecutive 20-year sentences and fined $20,000.

Even the prosecutor thought that was too harsh and pleaded unsuccessfully for its reduction. In 1977, U.S. District Judge James C. Turk agreed and struck down the sentence as a "cruel and unusual" punishment prohibited by the Eighth Amendment to the Constitution. The full 4th Circuit endorsed Turk's ruling.

In 1980, after the Supreme Court had upheld the life sentence for the $120 fraud conviction in Rummel vs. Estelle, it told the appeals court to reconsider the Davis case in light of the Rummel opinion. The appellate court, by a tie vote, stuck to its earlier ruling overturning the 40-year sentence.

Yesterday, the justices said the Rummel ruling should have been read to uphold Davis' sentence. The lower courts that wiped out the sentence, the justices said, "sanctioned an intrusion into the basic line-drawing process that is properly within the province of legislatures, not courts."

The 4th Circuit "could be viewed as having ignored, consciously or unconsciously, the hierarchy of the federal court system created by the Constitution and Congress," the justices said. "Unless we wish anarchy to prevail within the federal judicial system, a precedent of this court a reference to the Rummel case must be followed by the lower federal courts no matter how misguided the judges of those courts may think it to be."

Justice Lewis F. Powell Jr. agreed with the ruling in Hutto vs. Davis, but said he, too, viewed the Davis sentence as "unjust and disproportionate to the offense."

Davis, the father of one child, has been working in Roanoke since his release from prison during the appeals process. He may be eligible for parole after serving one-sixth of his sentence, according to state officials.