U.S. stocks shifted higher on Tuesday, reversing earlier losses and pushing the Standard & Poor's 500 Index to a four-month high, after Fannie Mae calmed investors about its financial situation and oil companies rallied on record crude prices.

Fannie Mae gained the most in more than a week on a regulatory decision that will allow the largest mortgage- finance company to buy more home loans. The company revealed a $2.2 billion first-quarter loss, while Swiss banking giant UBS posted an $11 billion loss and Merrill Lynch reported a 69 percent increase in so-called Level 3 assets, which are notoriously hard to price and even harder to sell.

As of 2:50 p.m. EST, the Dow Jones Industrial Average rose 46.00 points, or 0.4 percent to 13,015.54, the Standard & Poor's 500 index gained 10.06 points, or 0.7 percent to 1,417.55 and the Nasdaq Composite Index picked up 18.49 points, or 0.8 percent to 2,482.61.

Tech stocks were bolstered as hopes were revived that Yahoo might attempt to revive talks with would-be suitor Microsoft, which ended discussions during the weekend. Shares of Yahoo gained 6.2 percent, while Microsoft was up 2 percent

Fannie Mae, which owns or guarantees one of every five U.S. home loans, needs new capital to weather credit and derivative losses that rose fivefold to $8.9 billion. Regulators said they will loosen restrictions on the company's capital once the company has raised $6 billion in new capital. Fannie Mae climbed $1.33, or 4.7 percent, to $29.56 after dropping as much as $2.05.

A warning from Federal Reserve Chairman Ben Bernanke weighed on sentiment earlier in the session, after he said late Monday that increasing home foreclosures could further harm the economy.

Bernanke stated housing price declines and mortgage delinquencies may have substantial spillover effects into other markets, hurting the overall economy, said Alex Meister, an analyst with Wachovia Corp., in a report.

Energy companies gained the most among the industry groups, adding 2 percent. Crude climbed soared to a new record above $122 a barrel for the first time on threats to supplies in Nigeria and Iraq and growing global fuel consumption.

Exxon Mobil Corp., the largest U.S. energy company, rose 50 cents to $90.01. Chevron Corp., the second-biggest, added 66 cents to $96.28.

D.R. Horton rose 2 percent after the nation's largest home builder revealed a loss of $1.31 billion in its fiscal second quarter. Revenue declined 38 percent to $1.62 billion but did top estimates from Thomson Reuters of $1.36 billion.