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

; 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
addlw -'0'      ;convert from ASCII to decimal
;w=tens*2 + ones

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

movfw   tens    ;*1
rlf     tens,F  ;*4
rlf     tens,W  ;*10

;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 09:45, local time: 2023/2/7 07:33, TOP NEW HELP FIND:  34.225.194.102:LOG IN

 ©2023 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! PIC Microcontoller Radix Math Method

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.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.