picnoob wrote: > subroutine gets two digits of the num in the W register, and places > them in UNITS and TENS. Well, yes... But look at what you're doing: You've got a loop that subtracts one from your input number 10 times, and you count how many times you can execute that entire loop. The count of loop-executions goes in TENS, and then you add that count to itself ten times in order to multiply it by ten, then subtract it from your input number to get UNITS. Your code has a bug (hint: try it with the input number set to 10), but even without that bug, it's pretty inefficient: Your first loop iterates [input number] times, then your second loop always iterates ten times... And you need three registers in addition to TENS and UNITS. It doesn't scale well, either. Think about how you would modify it if you needed to do the hundreds' digit, too... Or, heaven forbid, operate on 16-bit numbers and do the thousands' and ten-thousands' digits. How familiar are you with the operation of the carry flag? Using it can make code like this much more efficient. After a subtraction, the carry flag is set to 1 if the result is non- negative and cleared to 0 if the result is negative... So you can do your binary-to-decimal thing by repeatedly subtracting ten from your input number, incrementing TENS and storing the result back in UNITS each time the result is non-negative. When the result finally is negative, you just exit: TENS contains the tens' digit because you've incremented TENS each time you successfully subtracted ten from the input number, and UNITS contains the ones' digit because you've already subtracted ten from it TENS times. Here's the code I posted earlier, modified to take the input number in W and commented so you can see what's happening: LIST R=DEC ;CONSTANTS DEFAULT TO DECIMAL. TENS EQU 0X0C UNITS EQU 0X0D ; ENTER WITH ORIGINAL 8-BIT VALUE (0-99) IN W. EXITS ; WITH ONES' DIGIT IN "UNITS", TENS' DIGIT IN "TENS". TENSUNITS: MOVWF UNITS ;UNITS = INPUT NUMBER. CLRF TENS ;INITIALIZE TENS TO 0. DO10S: MOVLW 10 ;W = UNITS - 10. SUBWF UNITS,W ; BNC DONE ;IF W IS NEGATIVE (I.E., UNITS ;WAS LESS THAN 10), GO EXIT. MOVWF UNITS ;OTHERWISE, UNITS = W. INCF TENS ;INCREMENT TENS. GOTO DO10S ;LOOP BACK AND TRY TO SUBTRACT ;ANOTHER 10. DONE: RETURN ;RETURN WITH TENS' DIGIT IN TENS, ;ONES' DIGIT IN UNITS. "BNC xxx" is a pseudo-opcode built into MPASM; it translates to: BTFSS STATUS,C ;If the carry flag is set, skip ahead. GOTO xxx ;Otherwise, jump to xxx. Notice that this routine just needs one loop, and it iterates only input/10 + 1 times. No extra variables are needed, and it takes just ten words of code space. -Andy === Andrew Warren - fastfwd@ix.netcom.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist