The energy sector cut its losses late in the session after Dow Jones reported Halliburton is in talks to buy Baker Hughes. Halliburton ended up 1 percent after earlier falling as much as 4.6 percent, while Baker Hughes rallied to close up 15.3 percent at $58.75.

They were, however, the only two stocks to finish higher in the energy sector of the S&P 500, which closed down 1.3 percent. Energy shares fell as Brent crude slid below $78 a barrel while U.S. crude tumbled 3.6 percent to $74.39, both at their lowest in more than four years.

Wal-Mart closed up 4.7 percent at $82.94 after posting higher same-store sales for the first time in seven quarters. It earlier hit a high of $83.06.

"Wal-Mart is showing strength, showing the natural relation it should have with (lower) energy prices," said Art Hogan, chief market strategist at Wunderlich Securities in New York.

Consumer-related companies are expected to see more demand as lower gasoline costs free up cash.

"But while we're waiting for (other) consumer stocks to rise, lower energy prices continue to be a headwind," Hogan said.

The Dow Jones industrial average rose 40.59 points, or 0.23 percent, to 17,652.79, the S&P 500 gained 1.08 points, or 0.05 percent, to 2,039.33 and the Nasdaq Composite added 5.01 points, or 0.11 percent, to 4,680.14.

Despite the slight gains, declining issues outnumbered advancing ones on the NYSE 1,974 to 1,085, for a 1.82-to-1 ratio on the downside; on the Nasdaq, 1,800 issues fell and 893 advanced for a 2.02-to-1 ratio.

Apple rose as high as $113.45 to a fresh record while Microsoft, up 1.7 percent to $49.61, swelled to a market cap above $408 billion. It overtook Exxon Mobil as the second-largest publicly traded U.S. company, behind Apple.

DreamWorks Animation shares jumped 14.1 percent to $25.52 after the New York Times said toymaker Hasbro Inc was in talks to buy the Hollywood studio. Hasbro shares lost 4.3 percent to $54.98.

U.S.-traded BlackBerry shares jumped 7 percent to $12.06 after it unveiled its new mobile-device management and security platform and struck wide-ranging partnerships.

The S&P 500 posted 67 new 52-week highs and 2 new lows; the Nasdaq Composite recorded 139 new highs and 63 new lows.

Volume on U.S. exchanges hit 6.4 billion, below the month-to-date average of 6.55 billion, according to BATS Global Markets data.

(Editing by Bernadette Baum and Nick Zieminski)

By Rodrigo Campos