Strong performances from a handful of companies including Barbie maker Mattel, and Coca-Cola pushed the Dow Jones average up for only the third day this month. The closely-watched index closed up 78 points to land at 12,805.
Mattel jumped 9 percent, more than any other company in the S&P. The country's biggest toy maker said net income rose because of better sales of Barbie dolls and lower advertising costs. Its stock climbed $3.01 to $34.05.
Coca-Cola posted higher income and revenue than Wall Street had expected, thanks in part to booming business overseas. Coke rose $1.21, or 1.6 percent, to $77.69.
The S&P rose 10.03 points to 1,363.67. The Dow Jones industrial average gained 78.33 points to 12,805.54, only its third increase of the month. Concern about corporate earnings and slower economic growth have weighed on the market.
The stock market wavered between gains and losses in morning trading as investors kept an eye on Federal Reserve Chairman Ben Bernanke's first of two days of testimony before Congress.
Bernanke said weaker economic growth probably means unemployment will remain stubbornly high. But he offered no signs that the Fed was ready to take action to bolster growth soon.
"The big question here isn't whether the Fed will act," said Randy Frederick, managing director of active trading and derivatives at Charles Schwab. "We know they will. The question is how bad do things have to deteriorate before they act."
As the earnings season got under way last week, analysts had expected quarterly profits for companies in the Standard & Poor's 500 index to fall 1 percent compared with the year before, according to S&P Capital IQ, the research arm of S&P. That would break a streak of higher earnings that started in the last quarter of 2009.
Ablin joked that it was similar to how, as a child, he tried to convince his parents his grades were going to be awful.
"That way, anything I brought home was a relief," he said.
Goldman Sachs also reported earnings and revenue that beat Wall Street's forecasts. The bank said it bundled more mortgages into bonds, leading to a 37 percent increase in sales from mortgage and commodity trading. Its stock gained 30 cents to $97.98.
The gains were broad. All 10 industries in the S&P 500 rose, led by health care companies. The Nasdaq composite index gained 13.10 points to 2,910.04.
Among other stocks making big moves:
— Mosaic jumped 5 percent. The fertilizer maker's net income beat Wall Street's forecasts, with the help of stronger phosphate sales and higher prices for potash. Mosaic also doubled its quarterly dividend to 25 cents per share. The stock gained $2.84 to $58.21.
— Reports that HSBC allowed Mexican drug cartels to launder billions through its U.S. banks helped push the bank's stock down. A Senate investigation also said some HSBC bank affiliates ignored U.S. government bans against financial transactions with Iran and other countries. HSBC fell 20 cents to $43.28.
— Walt Disney led the Dow, gaining $1.49, or 3.1 percent, to $49.35 after getting an upgrade by analysts at Bank of America.