please dont rip this site

PIC Microcontoller Radix Math Method

2 digit ASCII decimal to 8 bit Binary

Scott Dattalo and Dmitry Kiryashov

;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  ;

; Or use this one that saves a cycle:

   rlf   tens,w
   andlw 0x1e      ;w=2*tens
   movwf temp
   rlf   temp,f    ;temp=4*tens
   rlf   temp,f    ;temp=8*tens
   addwf ones,w
   addlw -'0'      ;convert from ASCII to decimal
                   ;w=tens*2 + ones
   addwf temp,w    ;w=10*tens + ones

;Dmitry Kiryashov [zews at AHA.RU] says:
;Destructive way to save one more clock ;-)

        movfw   tens    ;*1
        addwf   tens,F  ;*2
        rlf     tens,F  ;*4
        addwf   tens,F  ;*5
        rlf     tens,W  ;*10
        addwf   ones,W
        addlw   low     -11.*'0'

;Why so ? tens is '0' + x , ones is '0' + y , where x and y are in range
;of 0..9 So finally '0'*(10+1)=48.*11.=528.=0x210 . it is constant so we
;can subtract it back from result. Here we gone ;)

file: /Techref/microchip/math/radix/a2b-2d8b-sd.htm, 2KB, , updated: 2002/7/30 10:45, local time: 2024/7/13 17:51,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Microcontoller Radix Math Method </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!