President Barack Obama has challenged the international community to confront the causes of Middle East turmoil, saying the world faces "a choice between the forces that would drive us apart and the hopes we hold in common".

His speech to an annual gathering of world leaders at the UN General Assembly was his last before the November election, and campaign politics shadowed his words as he also spoke forcefully about Iran's nuclear programme, the peace prospects between Israelis and Palestinians, and the tensions that can come with freedom of speech.

"I do believe that it is the obligation of all leaders, in all countries, to speak out forcefully against violence and extremism," Mr Obama said.

He condemned the amateur anti-Muslim video made in the US that helped spark the recent protests which killed dozens of people, including the US ambassador to Libya, Chris Stevens, calling it "cruel and disgusting".

"There is no speech that justifies mindless violence," Mr Obama said.

But he strongly defended the US Constitution's protection of the freedom of expression, "even views that we profoundly disagree with".

Mr Obama also said time was running out to peacefully curb the Iranian nuclear crisis. Iran insists its nuclear programme is peaceful, but fears it is pursuing nuclear weapons have led Israel to threaten an attack. Mr Obama said there is "still time and space" to resolve the issue through diplomacy, but he said that time was not unlimited.

"Make no mistake: a nuclear-armed Iran is not a challenge that can be contained. It would threaten the elimination of Israel, the security of Gulf nations and the unravelling of the nuclear non-proliferation treaty," he said.

Republican presidential nominee Mitt Romney has accused Mr Obama of not being tough enough on Iran and of turning his back on Israel and other allies in the Middle East. Mr Romney also has said he does not have much faith in peace prospects between Israelis and Palestinians.

Mr Obama told the UN: "Among Israelis and Palestinians, the future must not belong to those who turn their backs on the prospect of peace."

In his address, Mr Obama mentioned US ambassador Mr Stevens several times.

"Today, we must declare that our future will be determined by people like Chris Stevens and not by his killers. Today, we must declare that this violence and intolerance has no place among our United Nations," he said.

"The strongest weapon against hateful speech is not repression, it is more speech – the voices of tolerance that rally against bigotry and blasphemy and lift up the values of understanding and mutual respect," he added.

The president said the US would never have just banned the offensive video, as some leaders in the Muslim world have advocated.

"Like me, the majority of Americans are Christian, and yet we do not ban blasphemy against our most sacred beliefs," he said.

"Moreover, as president of our country and commander-in-chief of our military, I accept that people are going to call me awful things every day, and I will always defend their right to do so," he said, to laughter from his audience.