Skip to content

Instantly share code, notes, and snippets.

@sebbekarlsson
Created November 9, 2020 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebbekarlsson/a0fbeda7fc107eabe0bfeb7d78be8a02 to your computer and use it in GitHub Desktop.
Save sebbekarlsson/a0fbeda7fc107eabe0bfeb7d78be8a02 to your computer and use it in GitHub Desktop.
# Convert number to string
# (number, buffer) => buffer
.type itoa, @function
itoa:
pushl %ebp
movl %esp, %ebp
movl $12, %edi
leal (%esp, %edi, 1), %ebx # buffer
movl 8(%esp), %eax # number
movl $0, %edi # counter
pushl $0x0
jmp itoa_loop
itoa_loop:
movl $0, %edx
movl $10, %ecx
div %ecx
addl $48, %edx
pushl %edx
movb (%esp), %cl
movb %cl, (%ebx, %edi, 1) # append byte to buffer
test %eax, %eax
je itoa_end
dec %edi
jmp itoa_loop
itoa_end:
leal (%ebx, %edi, 1), %eax # pointer to start of buffer
movl %ebp, %esp
popl %ebp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment