MEXICO CITY - President Obama on Friday delivered an impassioned appeal to change U.S. immigration rules so that "immigrants get on the right side of the law."

Speaking in the plaza of a museum holding Mexico's most-treasured archaeological relics, Obama hailed the contributions of Mexican immigrants to the United States and acknowledged that the strong support of Hispanics helped lift him to the presidency.

He called for a "well-regulated border" but said the best way to combat illegal immigration "is a growing, prosperous Mexico that creates more jobs and opportunity right here."

He lamented the current U.S. immigration system, saying it doesn't represent U.S. values.

"It separates families when we should be reuniting them. It's led millions of people to live in the shadows," he said as he prepared to depart for Costa Rica, the last stop on his three-day trip.

People of Mexican heritage residing in the United States number about 34 million, comprising some 11 percent of the population. Millions of Mexicans lack documentation, although the net flow of Mexican migrants toward the United States is believed to have slowed or even stopped.

The crowd reacted warmly, giving ovations at Obama's attempts to speak Spanish.

As he did on his arrival Thursday, Obama largely stayed away from the issue of public security.

More than 60,000 Mexicans have been slain since 2006, when criminal gangs unleashed unremitting bloodshed that has only stabilized in the past year or so.

The demand for narcotics in the United States is the root cause of much of the violence, Obama said.

"I do not believe that legalizing drugs is the answer," he added.

He said a new U.S.-Mexican partnership in higher education could bring thousands of students to cross the border in both directions for university training.

"When we study together," he said, "we prosper together."