# -------------------------------------------- # FUNCTION: int2str # PURPOSE : Convert a 64 bit integer into ASCII # INPUTS : rdi = 64 bit integer # rsi = address of buffer # OUTPUTS : rax = address of string (points into the provided buffer) # rdx = length of string # CLOBBERS: rax, rcx, rdi, rsi, r8, r9, r10 # -------------------------------------------- .section .text .globl int2str .type int2str, @function int2str: pushq %rbx # save callee-saved register movq %rdi, %rax # integer to convert movq %rsi, %rbx # buffer base movq %rsi, %r9 # save original buffer leaq 19(%rbx), %r8 # pointer to last char position movq $10, %rcx # divisor xorq %r10, %r10 # sign flag (0 = positive) # handle sign testq %rax, %rax jns .int2str_convert negq %rax # if the number negative we negate it and movq $1, %r10 # set sign flag .int2str_convert: testq %rax, %rax jnz .int2str_loop # check if the number is 0 # if the number is 0, we just set the char to '0', and length to 0. movb $'0', (%r8) movq %r8, %rax movq $1, %rdx jmp .int2str_shift_left # shift the result leftmust .int2str_loop: xorq %rdx, %rdx # clear rdx before division divq %rcx # divide rdx:rax by 10 addb $'0', %dl # add ASCII base movb %dl, (%r8) # move digit into buffer decq %r8 # decrement pointer testq %rax, %rax # while quotient is not 0, loop jnz .int2str_loop incq %r8 # move back to first digit # add '-' if negative testq %r10, %r10 jns .int2str_calc_len decq %r8 movb $'-', (%r8) .int2str_calc_len: # count length of the string movq %r8, %rax # string start leaq 20(%r9), %rdx # end of buffer subq %rax, %rdx # string length .int2str_shift_left: # shift string to leftmost in buffer if not already there cmpq %rax, %r9 je .int2str_done # already leftmost cld # Clear direction flag (forward copy) movq %rdx, %rcx # rcx = length of string movq %rax, %rsi # rsi = source :: start of string movq %r9, %rdi # rdi = destination :: original buff start rep movsb # copy string left # Found this online. It's a way to copy down values in memoery. # movbs = Move String Byte.. # It copies 1 byte from memoery to memoery and # incrementing each pointer. # Source :: %rsi # Dest :: %rdi # Direction Flag :: DF ~ 0 for inc and 1 dec # rep = repeat wow. # It's a prefix that repeats an isntructution rcx times # https://stackoverflow.com/questions/27804852/assembly-rep-movs-mechanism movq %r9, %rax # return pointer to start .int2str_done: popq %rbx ret