The S&P 500 and the Nasdaq fell on Tuesday as investors booked profits following the stock market's recent run-up, while a weaker-than-expected reading in a measure of consumer confidence sparked caution about the spending recovery.

Even so, the Dow clung to a modest gain as shares of Exxon Mobil and Chevron , both set to report quarterly results later this week, rose on the back of BP Plc's strong earnings.

Baidu Inc's disappointing outlook added to the negative tone. The semiconductor index <.SOXX> shed 2.3 percent, while the Nasdaq lost more than 1 percent.

The Chinese Web Search company's stock, down 13 percent at $376.50, was the top drag on the Nasdaq, behind Apple Inc , down 2.4 percent at $197.71. Baidu forecast a sequential decline in fourth-quarter revenue when it posted results late on Monday.

People want to take some profits. The consumer confidence numbers weren't great, said Stephen Carl, principal and head of U.S. equity trading at The Williams Capital Group LP in New York. We had a good run with the Dow topping around 10,000 over the last week. Many people feel the market could be overvalued.

The Dow Jones industrial average <.DJI> gained 28.04 points, or 0.28 percent, to 9,896.00. The Standard & Poor's 500 Index <.SPX> slipped 2.23 points, or 0.21 percent, to 1,064.72. The Nasdaq Composite Index <.IXIC> dropped 23.31 points, or 1.09 percent, to 2,118.54.

The S&P 500 is now up 57 percent from the 12-year closing low of March 9, having slipped from its recovery peak when it was up 62.3 percent from that significant low.

The Conference Board's index of consumer confidence fell to 47.7 in October, weaker than economists had forecast. The data showed consumers were increasingly concerned about job market conditions.

Both the S&P retail index <.RLX>and the S&P consumer discretionaries index <.GSPD> dropped 1.4 percent.

Other data showed home prices rose for the fourth-straight month in August. The Standard & Poor's/Case-Shiller composite index of home prices in 20 metropolitan areas rose more than expected in August.

(Editing by Jan Paschal)