//---------------------------------------------------------------- // asc2int.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 ESI will contain the memory-address of a string // of decimal numerals; it returns with the numerical value // represented by that string in the EAX register (provided // that number does not overflow the EAX register). The CF // bit in the EFLAGS register indicates an overflow error. // // EXPECTS: ESI = address of decimal string // RETURNS: EAX = integer value represented // // programmer: ALLAN CRUSE // written on: 14 OCT 2003 //---------------------------------------------------------------- .section .data radix: .int 10 # base of decimal number-system .section .text ascii_to_int: # # This procedure converts a string of decimal digits, located at # the memory-address found in register %esi, into its numerical # value, and returns that value in register %eax. Upon return, # register %esi points to the first non-digit that occurs in the # string (or else to the digit that caused the overflow-error). # If successful the carry-flag is cleared; otherwise it is set. # The contents of all the other general registers is unchanged. # pushl %ecx # preserve work registers pushl %edx xorl %eax, %eax # initialize accumulator nxdgt: movb (%esi), %cl # fetch next ascii-code cmpb $'0', %cl # is it below '0'? jb cvtxx # yes, not a digit cmpb $'9', %cl # is it above '9'? ja cvtxx # yes, not a digit andl $0xF, %ecx # convert digit to integer mull radix # ten times previous total addl %ecx, %eax # plus the newest integer jc cvtxx # return if sum overflowed incl %esi # advance source pointer jmp nxdgt # check for more digits cvtok: clc # return with CF clear cvtxx: popl %edx # restore saved registers popl %ecx ret .globl ascii_to_int # makes the label public