# Assembly Packed Bcd

I was trying to make a subroutine called count that will count the number of occurences of the string ‘hello’ in the 64 KB external memory with 8051 microprocessor. What i wanted to make is that when the counter is over 1.000 it will be written in R1 and R2 as packed number like if the count is 1550, R1 will have 15 and R2 will have 50. How can i use R1:R2 for this problem? In other words, how can i keep track the counter approptiately when the counter have over 1000?

• Since you don't even specify what kind of processor you are using, it is impossible at this time to answer this. – tcrosley Nov 28 '11 at 19:46
• Sorry,8051 processor i am using – david Nov 28 '11 at 19:49
• anybody??????:( – david Nov 28 '11 at 20:09
• So apparently you want to convert a 16 bit binary value into decimal, and this counter and "hello" stuff is irrelevant. Look around. There are probably binary to decimal routines out there for the 8051. That used to be a common processor a long time ago, maybe before people started posting on on the web. – Olin Lathrop Nov 28 '11 at 20:35
• @david - Relax, it's only been an hour. It can take a day or two to get answers. In the meantime, you might take some of the suggestions from "How can I get answers fast". 0x6d64 has already retagged your question; you could benefit greatly from a code sample describing precisely what you've tried. – Kevin Vermeer Nov 28 '11 at 20:49

There are two general approaches one might take to obtaining a count in base-10, base-100, or other non-power-of-two-base format. One may either perform the count in binary format and convert the result to the desired base, or one may count things directly in the desired base.

If one is writing machine code on the 8051, and wants to have the count in base-100 format, the best approach would probably be to have the lower count registers hold 100 minus the "real" value, in which case the routine to "increment" the register pair r1:r2 would be:

  djnz r2,noWrap
inc  r1
mov  r2,#100
noWrap:


If one wishes to have r2 count up rather than down:

  inc r2
cjne r2,#100,noWrap
inc r1
mov r2,#0
noWrap:


The former code will take five bytes and will execute in two cycles on a "standard" 8x51 in the no-wrap case or four cycles in the wrap case. The latter code will take seven bytes and will execute in three cycles on a "standard" 8x51 in the no-wrap case and five cycles in the "wrap" case. On "accelerated" 8x51 clones, the time difference between the two will probably be the time to execute two minimal-cost instructions, since the latter code both adds an extra (probably minimal-cost) instruction and replaces a two-byte djnz instruction with a three-byte cjne instruction.