In around 2000 bce, the Amorites (Westerners), a semi-nomadic people from Syria, swept across Mesopotamia, replacing local rulers with Amorite sheikh dynasties in many of the city-states. By the early 18th century bce, the three most powerful Amorite kings were pre-eminent Shamshi-Adad in the north, Rim-Sin in Larsa in the south, and Hammurabi in Babylon in the center. Over the course of his long reign, Hammurabi consolidated all of southern Mesopotamia into his kingdom and eventually extended his power as far up the Tigris as Nineveh, and as far up the Euphrates as Tuttul, on the junction with the river Balikh. He personally supervised the construction of many temples and other buildings.
The prelude to his code, a tribute to Hammurabi, and a long historical record of his conquests, boasts that his leadership was divinely sanctioned by the gods who passed control of humanity to Marduk (deity of Babylon), and so to its king. It also reveals he saw his role as the guarantor of a just and orderly society.