In a stunning response to the violence and turmoil sweeping South Africa, the House yesterday approved far-reaching legislation that would require the withdrawal of all U.S. firms operating in that racially divided country.

The measure, as passed by the Democratic-led House, is given virtually no chance of passage by the Republican-controlled Senate. But its approval by even one chamber -- the first time that disinvestment legislation has passed either house -- represents a clear setback to President Reagan's policy of "constructive engagement" toward South Africa. It is also a warning to South Africa's white-minority government of mounting congressional pressure for dismantlement of its apartheid system.

The legislation, offered by Rep. Ronald V. Dellums (D-Calif.) as a substitute for a milder economic sanctions bill the House was debating, was approved by voice vote with no recorded opposition.

It would require total U.S. disinvestment from South Africa 180 days after enactment, and impose a trade embargo against that country except for key minerals needed by the U.S. defense industry.

Election-year considerations played a role in the decision by Democratic supporters of sanctions legislation and Republican opponents not to demand a roll-call vote on the bill. But in the aftermath of the unexpected House action, there were sharply conflicting assessments of whether passage of the measure increased the chances that the United States will take tough new economic steps against South Africa.

Reps. Danny L. Burton (R-Ind.) and Mark D. Siljander (R-Mich.), who led opposition to sanctions legislation on the House floor, said the milder sanctions measure had been certain to pass and that they hoped to kill the issue by approval of "the worst possible bill."

Siljander predicted that the Senate, faced with the Dellums disinvestment measure, will refuse to pass sanctions legislation this year.

"This bill is dead; sanctions are dead," he said.

But Democrats, including Rep. William H. Gray III (Pa.), chief sponsor of the original sanctions bill, disagreed and said they were delighted by the approval of the much tougher Dellums substitute.

"The message that goes from the House to the United States Senate today is, do something -- get off the dime," Gray said. Acknowledging that the disinvestment bill has virtually no chance in the Senate, Gray said there would now be "irrepressible pressure" on the Senate to approve some form of sanctions and then compromise with the House in a conference committee.

Rep. Howard E. Wolpe (D-Mich.), chairman of the House Foreign Affairs subcommittee on Africa, said the House action should also increase pressure on other western nations to impose economic sanctions on South Africa.

The House action sets the stage for a possible replay of last year's developments on South Africa, when congressional pressure forced Reagan for the first time to impose limited sanctions. Reagan signed the executive order as the Senate neared approval of a House-passed sanctions bill.

Senate Foreign Relations Committee Chairman Richard G. Lugar (R-Ind.) has scheduled hearings on South Africa next month. Lugar, who was instrumental in forcing Reagan to act last year, has said he doubts that additional sanctions would be effective in ending apartheid but has not ruled them out.

"They do send a message," he said this week.

The White House made a last-minute attempt to rally opposition to sanctions legislation. In a letter to House Minority Leader Robert H. Michel (R-Ill.), national security affairs adviser John M. Poindexter said the administration had "grave misgivings and strong opposition" to attempts to legislate economic sanctions, which he said would "erode our capacity to promote negotiations in South Africa" and are "likely further to separate an already divided society."

Before yesterday's vote, however, Michel acknowledged that House passage of sanctions legislation was a foregone conclusion, and probably with more Republican support than the 56 GOP lawmakers who voted for sanctions last year. Michel and other GOP leaders apparently played no role in the decision by Burton, Siljander and others not to oppose Dellums' measure.

Poindexter's objections were aimed at the milder bill sponsored by Gray. It would have immediately banned all new U.S. loans and investments in South Africa, prohibited U.S. participation in South African energy development, stopped the import of South African uranium ore, coal and steel, and denied U.S. landing rights to South Africa's national airline.

The bill also called for complete disinvestment from South Africa by the U.S. computer industry within 18 months unless negotiations to dismantle apartheid had begun and South Africa had freed all political prisoners.

A measure almost identical to the Gray proposal has been introduced in the Senate.

After the vote on his substitute measure, Dellums, who said he was "shocked" by its sudden acceptance, embraced Rep. Mickey Leland (D-Tex.), the chairman of the Congressional Black Caucus. Leland said approval of the bill was a warning to South Africa and the Reagan administration "that it is time for decisive and aggressive action to isolate the proponents of apartheid."

Speaker Thomas P. (Tip) O'Neill Jr. (D-Mass.) called passage of the measure a sign of "the tidal wave of American revulsion against the discredited policy of 'constructive engagement.' "

Gray and other Democratic supporters of economic sanctions said they always supported the toughest possible measures against South Africa but had doubted that disinvestment legislation could be enacted. What changed the political chemistry in the House, Gray said, was the rising cycle of violence in South Africa and the government's increasingly repressive measures against the black majority.

"Congress looked at the carnage and the violations of human rights that have occurred in the last 10 days and said, that's it, we want to send the strongest possible message," he said.