//---------------------------------------------------------------- // int2asc.s // // This file defines a generally useful conversion-routine. // It can be linked to assembly language applications which // are able to transmit parameters in general cpu registers // (rather than via the stack). The procedure expects that // register EAX will contain the unsigned integer for which // a conversion into a decimal digit-string is desired, and // that register EDI will contain the memory-address of the // buffer-area where the string of numerals may be written. // This function will append a null-byte at the end of that // string of numerals, as a string terminator. // // EXPECTS: EAX = integer to be converted // EDI = address for digit string // // RETURNS: EDI = address of string's null // // programmer: ALLAN CRUSE // written on: 14 OCT 2003 //---------------------------------------------------------------- .section .data radix: .int 10 # base of decimal number-system .section .text int_to_ascii: pushl %eax # preserve work registers pushl %ecx pushl %edx xorl %ecx, %ecx # initialize digit-counter nxdiv: xorl %edx, %edx # extend dividend to 64 bits divl radix # divide by the number base pushl %edx # save remainder on the stack incl %ecx # and increment digit-count orl %eax, %eax # was the quotient zero? jnz nxdiv # no, do another division nxdgt: popl %edx # recover saved remainder addb $'0', %dl # and convert it to ascii movb %dl, (%edi) # store digit into buffer incl %edi # and advance the pointer loop nxdgt # convert all remainders movb $0, (%edi) # append a null at the end popl %edx # restore saved registers popl %ecx popl %eax ret .globl int_to_ascii # makes the label public