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?
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.