On Sat, 1 Apr 2000, Thomas McGahee wrote: > Scott, > Here is my brute force method for converting a two-byte > ascii decimal representation to the actual binary equivalent. > For example, ascii '3', '7' which is hex 0x33, 0x37 > will become binary b'00100101' which is hex 0x25 > > ;enter with ascii_msd and ascii_lsd loaded with ascii > ;representation of decimal value to be converted to hex. > ;Ascii must be in range 0x30 to 0x39. ( "0" to "9" ) > > ;exit with hex byte in ascii_lsd (or w). > ;Numerical value will be between 0 and 99 (decimal). > > movlw 0x0f ;set up mask. w has 0x0f > andwf ascii_lsd,f ;ascii_lsd has hex/dec lsd. w has 0x0f > andwf ascii_msd,f ;ascii_msd has hex/dec msd. w has 0x0f > bcf status,c ;carry=0 (so 0 gets rotated in next step) > rlf ascii_msd,w ;w has hex/dec msd*2, and ascii_msd has hex/dec msd > movwf ascii_msd ;w has hex/dec msd*2, and ascii_msd has hex/dec msd*2 > rlf ascii_msd,f ;w has hex/dec msd*2, and ascii_msd has hex/dec msd*4 > rlf ascii_msd,f ;w has hex/dec msd*2, and ascii_msd has hex/dec msd*8 > addwf ascii_msd,w ;w has hex/dec msd*10, and ascii_msd has hex/dec msd*8 > addwf ascii_lsd,f ;ascii_lsd has msd*10 + lsd. > ;ascii_lsd now contains the desired hex byte > > ; or use addwf ascii_lsd,w if you want the hex byte in w. > > > I have an explicit clearing of the carry flag > prior to the rlf instruction. I notice that your (Scott's) > ascii to hex byte solution does not explicity clear the > carry flag. I don't see how you can be *sure* that the carry > is clear prior to your first rlf instruction. Or am I > missing something here? Yes you are (missing something). Immediately following the rlf is an ANDLW instruction. If the carry is set (before the rlf), then the ANDLW 0x1e will clear the lsb that got corrupted. At the same time, the RLF clears the carry because David says the input consist of ASCII encode decimal digits. Here's the routine again with additional comments: ;ascii1 and ascii2 are the tens and ones digit of a number we wish ;to convert to binary ; ; In C: ; ; binary = (ascii1 & 0xf) * 10 + (ascii2 & 0xf); ; ; (I'd be interested to see how a compiler would generate the asm for this.) ; movlw 0x0f andwf ascii2,f ;Clear the upper nibble of the one's digit ; Multiply the ones digit by 10. rlf ascii1,w ;2*tens ;Note that the carry is also cleared because ;ascii1 is an ASCII number between 0x30-0x39 andlw 0x1e ;Clear upper nibble of tens digit ;In addition, we clear the shifted in carry movwf ascii1 ;W = ascii1 = 2*original ascii1 rlf ascii1,f ;ascii1 = 4*original ascii1 rlf ascii1,f ;ascii1 = 8*original ascii1 addwf ascii1,w ;W = 2*original ascii1 + 8 *original ascii1 ; = 10*original ascii1 addwf ascii2,w ; Scott