	.section	.text
	.intel_syntax
	.global	itoa
itoa:
	push	%ebp
	mov	%ebp, %esp
	sub	%esp, 24		# Allocate space
	mov	dword ptr [%ebp - 12], 0	# index = 0
	cmp	dword ptr [%ebp + 8], 0
	jne	itoa.not_zero		# number <> 0
	mov	%eax, [%ebp + 12]	# Load string
	mov	dword ptr [%eax - 4], 1	# Set length of string
	mov	byte ptr [%eax], 48	# string[0] = '0'
	mov	byte ptr [%eax + 1], 0	# string[1] = 0
	mov	%eax, 1
	jmp	itoa.end
itoa.not_zero:
	mov	%eax, [%ebp + 8]
	mov	%edx, 0
	div	dword ptr [%ebp + 16]	# number base /
	mov	[%ebp - 16], %edx	# Save remainder
	cmp	dword ptr [%ebp - 16], 9
	jbe	itoa.less_than_ten	# 9 >= remainder
	lea	%edx, [%edx + 87]	# %edx = remainder 10 - 'a' +
	jmp	itoa.divide_number
itoa.less_than_ten:
	lea	%edx, [%edx + 48]	# %edx = remainder '0' +
itoa.divide_number:
	mov	%eax, [%ebp - 12]
	lea	%ecx, [%eax + 1]
	mov	[%ebp - 12], %ecx	# index = index + 1
	mov	%ecx, [%ebp + 12]
	add	%eax, %ecx
	mov	[%eax], %dl		# string[index] = %dl
	mov	%eax, [%ebp + 8]
	mov	%edx, 0
	div	dword ptr [%ebp + 16]	# number base /
	mov	[%ebp + 8], %eax	# Replace number with quotient
	cmp	dword ptr [%ebp + 8], 0
	jne	itoa.not_zero		# number <> 0
	mov	%edx, [%ebp + 12]
	mov	%eax, [%ebp - 12]
	mov	[%edx - 4], %eax	# string length = index
	add	%eax, %edx
	mov	byte ptr [%eax], 0	# string[index] = 0
	push	[%ebp + 12]		# string
	call	strrev
	add	%esp, 4			# Clean stack
	mov	%eax, [%ebp - 12]
itoa.end:
	mov	%esp, %ebp		# Deallocate space
	pop	%ebp
	ret
