NEW YORK - U.S. stocks added to their recent rally on Friday after upbeat forecasts from Apple and Amazon.com, and the S&P 500 and Nasdaq posted their biggest monthly percentage gains since 2020.

Most S&P 500 sectors ended higher, with energy rising 4.5%, the most of any S&P sector. Chevron Corp rose 8.9% and Exxon Mobil shares jumped 4.6% after the companies reported record quarterly revenues.

Apple Inc shares gained 3.3% after the company said parts shortages were easing and that demand for iPhones was continuing. Amazon.com Inc shot up 10.4% after it forecast a jump in third-quarter revenue from bigger fees from its Prime loyalty subscriptions.

"In today's market, the Amazon and Apple numbers are giving the market support (on) the idea that two large companies that are a large part of the S&P seem so far to be able to navigate through these tougher times," said Rick Meckler, partner at Cherry Lane Investments, a family investment office in New Vernon, New Jersey.

Stocks have also rallied this week on investor speculation that the Federal Reserve may not need to be as aggressive with interest rate hikes as some had feared.

The Dow Jones Industrial Average rose 315.5 points, or 0.97%, to 32,845.13; the S&P 500 gained 57.86 points, or 1.42%, to 4,130.29 and the Nasdaq Composite added 228.10 points, or 1.88%, to 12,390.69.

All three major indexes gained for the month and for the week. The S&P 500 gained about 9.1% for July in its biggest monthly percentage gain since November 2020, while the Nasdaq jumped about 12.3% in July in its biggest monthly gain since April 2020.

In other earnings, Intel Corp shares fell 8.6% after the company cut annual sales and profit forecasts and missed second-quarter estimates.

Second-quarter U.S. corporate results have mostly been stronger than expected.

Of the 279 S&P 500 companies that have reported earnings so far, 77.8% have exceeded expectations. Earnings for S&P 500 companies now are expected to have increased 7.1% in the quarter versus an estimated 5.6% at the start of July, according to IBES data from Refinitiv.

The day's economic data showed U.S. labor costs increased strongly in the second quarter as a tight jobs market boosted wage growth.

But on Thursday, a government report showed the American economy unexpectedly contracted in the second quarter, suggesting to some investors that the economy was on the cusp of a recession. They said it might deter the Fed from continuing to aggressively increase rates as it battles high inflation.

Volume on U.S. exchanges was 11.35 billion shares, compared with the 10.79 billion-share average for the full session over the last 20 trading days.

Advancing issues outnumbered declining ones on the NYSE by a 2.92-to-1 ratio; on Nasdaq, a 1.44-to-1 ratio favored advancers.

The S&P 500 posted three new 52-week highs and 33 new lows; the Nasdaq Composite recorded 63 new highs and 82 new lows.

(Additional reporting by Shreyashi Sanyal and Aniruddha Ghosh in Bengaluru; Editing by Arun Koyyur, Anil D'Silva and Jonathan Oatis)