A Maryland Penitentiary inmate accused of murdering a prison guard testified today that he stabbed the guard in "absolute self-defense" after a group of guards started attacking him in retaliation for an earlier incident.

"I just wanted to get those guys off of me as soon as possible because they were beating my brains out," said Nathaniel Appleby, 25, a convicted murderer.

Appleby faces a possible death penalty if convicted of the first-degree murder of Herman Toulson, 39, the first Maryland prison guard killed in the line of duty. He also is charged with the attempted murder of guard Willie J. Newkirk, who was stabbed during the Oct. 6, 1984 incident in the prison's South Wing, which houses the state's most violent and troublesome inmates.

Newkirk testified earlier in the trial, now in its third week, that Appleby turned on Toulson and stabbed him without provocation, then attacked Newkirk when Newkirk ran to his colleague's aid.

But Appleby testified today that Toulson, Newkirk and several other guards beat him while he was outside his cell during the allotted daily half-hour exercise period.

The veteran guards, he said, had been threatening him since an incident in late September in which rookie guards tried unsuccessfully to subdue an inmate armed with a knife.

To save face with their fellow guards, he said, the rookies had blamed their failure on Appleby and another inmate, whom they accused of blocking their paths.

Appleby said that on Oct. 6 he was standing in the narrow walkway outside the row of cells, using a twisted bedsheet to hoist a bag of homemade wine from an inmate on a lower floor, when a group of officers approached him.

" . . . You know what time it is. Didn't we tell you, you had a . . . whipping coming to you?" Newkirk said, according to Appleby.

The guards ordered him back to his cell, but started to push and hit him when he tried to squeeze down the corridor past them to the cell, Appleby said.

"From that point on, it was just chaos," he testified. After guards wielding wooden riot sticks broke his arm, Appleby testified, he remembered the knife he always carried with him for protection when he left his cell.

In addition to the broken arm, Appleby suffered injuries to his head and face requiring 108 stiches during the incident.

Prosecutors have contended that those injuries took place when guards, enraged at the attack on fellow officers, beat Appleby after the incident.

Defense lawyer Anton J.S. Keating has argued that Appleby was beaten twice -- once in the dispute that he said prompted the stabbings, and afterwards in retaliation.

Appleby testified that he heard a supervisor, Capt. Irvin Hawkins, order guards to beat him, saying, "Don't stop till he's on a stretcher or dead."

Hawkins has denied that he was present during any beatings.

The picture of Appleby that emerged during his testimony yesterday was that of an intelligent and talented young man who spent some time at a camp for juvenile delinquents, where he earned his high school equivalency diploma.

A native of Salisbury, Md., Appleby described his "culture shock" upon entering the maximum-security prison where, he said, "everybody was in mobs and gangs and cliques, and if you weren't in a mob, you were fair game.

A "goon squad" of guards regularly beat disruptive inmates," he said.

Appleby said he did not join a gang because "I felt like I had recognized the mistakes I had made to get there, and I didn't want any idiot who had been in and out of there all his life, making those mistakes for me."

Under cross-examination, which is to continue tomorrow, Appleby also began to appear short-tempered, combative and arrogant.

"Have you ever been in one of those group sessions?" he asked prosecutor Donald J. Giblin, who was questioning Appleby about his brief stay at Patuxent Institution, the prison system's mental hospital.

"Yes," Giblin said.

"Then that probably explains why you're the way you are," Appleby retorted.