Although, today, sir is commonly used as a title of respect for any man, when it first entered the language in the late thirteenth century (as a shortened form of sire, which preceded it by a scant hundred years) it was employed solely as the title of a knight or baronet.
With the growth of the language, it followed shortly that a man, not necessarily knighted but who comported himself in knightly fashion, came to be described as sirly, that is, he was “like a sir.”
But, as has been remarked so many times, spelling in those days was much more of an art than a science, and individual authors followed their own whims.
Thus sir was sometimes spelled sur, and sirly became surly, the latter becoming fixed.
And the meaning changed from “knightly” to “haughty” to “arrogant” to “rude,” which is its present sense.