Ok..a challenge..from Nicolai...no chance of beating him I guess :) ..but here goes 44'ish instructions ( not counting setup of input ) executes in the same amount of cycles (i.e 44 ) : ; *********************************************************************** ; TEST_ASC_TO_BIN ; Routine to test ascii to 16 binary conversion ; TEST_ASC_TO_BIN GLOBAL TEST_ASC_TO_BIN ; load test value MOVLW D'1' MOVWF TenK,B MOVLW D'2' MOVWF Thou,B MOVLW D'0' MOVWF Hun,B MOVLW D'7' MOVWF Ten,B MOVLW D'5' MOVWF Ones,B NOP ; these are not needed only used during dev. test CLRF Hi,B CLRF Lo,B ; bcd to binary conversion 16 bits ( i.e. max input is 65535 ) ; uses nibble math there lowest nibble i calculated first ; the 'overflow' added to the calculation for the second nibble ; etc. upto the third nibble. Then the last additions for the ; fourth nibble which only is affected by ( apart from overflow from lower nibbles ) ; the input byte tenk. ; uses factorisation of the decimal weight contants as per this table: ; 0000000000000001=3D 1 =3D 1 ; 0000000000001010=3D 10 =3D 8+2 ; 0000000001100100=3D 100 =3D 64+32+4 ; 0000001111101000=3D 1000 =3D 512+256+128+64+32+8 ; 0010011100010000=3D 10000 =3D 8192+1024+512+256+32 ; ; calculation perform according to the following pseudo code: ; Separate each nibble: ; ; 0000 0000 0000 0001=3D 1 =3D 1 ; 0000 0000 0000 1010=3D 10 =3D 8+2 ; 0000 0000 0110 0100=3D 100 =3D 64+32+4 ; 0000 0011 1110 1000=3D 1000 =3D 512+256+128+64+32+8 ; 0010 0111 0001 0000=3D 10000 =3D 8192+1024+512+256+32 ; = ; Use the variable lo to calculate the result of the lowest nibble: ; lo =3D Thou*8 + Hun*4 + Ten*(8+2) + Ones ; the result and overflow ( top nibble ) is saved and calculation is perfromed on the second nibble ; by using the variable hi: ; hi =3D Tenk + Thou*(8+4+2) + Hun*(4+2) ; The low nibble from the result is added to the high nibble from the ; previous calculation of lo: ; lo =3D hi<<4 + lo ; etc. etc.. ; MOVF Thou,W ; w =3D thou ADDWF Ten,W ; w =3D thou + ten MOVWF Lo ; lo =3D thou + ten ADDWF Lo,W ; w =3D 2*(thou + ten) ADDWF Hun,W ; w =3D 2*(thou + ten) + hun MOVWF Lo ; lo =3D 2*(thou + ten) + hun ADDWF Lo,W ; w =3D 2*(2*(thou + ten) + hun) ADDWF Ten,W ; w =3D 2*(2*(thou + ten) + hun)+ten MOVWF Lo ; lo =3D 2*(2*(thou + ten) + hun)+ten ADDWF Lo,W ; w =3D 2*(2*(2*(thou + ten) + hun)) ADDWF Ones,W ; w =3D 2*(2*(2*(thou + ten) + hun)) + ones MOVWF Lo ; lo =3D 2*(2*(2*(thou + ten) + hun)) + ones =3D 8*Thou + 10*ten + 4*hun + ones ; low byte lowest nibble now fully calculated in lo ; now we calculate high nibble lo byte by using hi as temp storage MOVF Thou,W ; w =3DThou = ADDWF Thou,W ; w =3D 2 * Thou ADDWF Hun,W ; w =3D 2 * Thou + hun ADDWF Thou,W ; w =3D 2 * Thou + hun + thou MOVWF Hi ; hi =3D 2 * Thou + hun + thou ADDWF Hi,W ; w =3D 2*(2 * Thou + hun + thou) =3D 6*thou + 2*hun ADDWF Thou,W ; w =3D 6*thou + 2*hun + thou ADDWF Hun,W ; w =3D 6*thou + 2*hun + thou + hun MOVWF Hi ; hi =3D 6*thou + 2*hun + thou + hun ADDWF Hi,W ; w =3D 2* ( 6*thou + 2*hun + thou + hun )) =3D 14*thou + 6*hun ADDWF TenK,W ; w =3D 14*thou + 6*hun + tenk MOVWF Hi ; hi =3D 14*thou + 6*hun + tenk ; low byte high nibble fully calculated in hi ; now we need to add the low nibble to previous content in lo ( from low nibble calc. ) SWAPF Hi,W ; swap nibbles ( lownibble->high ) ANDLW 0xF0 ; mask out high nibble ADDWF Lo,F ; add result to lo byte ( note carry checked 4 rows down ! ) SWAPF Hi,W ; swap nibbles ( high nibble->low ) = ANDLW 0x0F ; mask out low nibble BTFSC STATUS,C ; now we check the carry from the addition of the lo byte = ADDLW 0x01 ; in case of ripple carry we add one to hi byte MOVWF Hi ; Hi =3D overflow(lo) ; now we have overflow from lo byte caculation in Hi therefor we cannot use the hi byte temporarily ; the following calculation must be performed in w only ( I just whish an temp was available :) = ; or one could trash the input....) = RLF TenK,W ; w =3D 2*tenk ADDWF TenK,W ; w =3D 3*tenk = ADDWF TenK,W ; w =3D 4*tenk = ADDWF TenK,W ; w =3D 5*tenk = ADDWF TenK,W ; w =3D 6*tenk = ADDWF TenK,W ; w =3D 7*tenk = ADDWF Thou,W ; w =3D 7*tenk + thou ADDWF Thou,W ; w =3D 7*tenk + 2*thou ADDWF Thou,W ; w =3D 7*tenk + 3*thou ADDWF Hi,F ; add it to the overflow from lo = SWAPF TenK,W ; w =3D 32 * tenk ADDWF Hi,F ; hi =3D 7*tenk + 3*thou + 32*Tenk ADDWF Hi,F ; hi =3D 7*tenk + 3*thou + 32*Tenk + 32*Tenk =3D 7*tenk + 3*thou + 64*Tenk = NOP RETURN But I just happen to know that Nicolai has an 37 cycle waiting, :( ...anyway..this beat's his 91 cycle version... Have fun, /Tony Tony K=FCbek, Flintab AB = =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2 E-mail: tony.kubek@flintab.com =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2 -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics