Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/18/06 23:13
Read: times


 
#129702 - Indexing
Responding to: ???'s previous message
Hello David,

The only sort-of indexing instructions available on the 8052 are movc @a+dptr and movc @a+pc, both of which only apply to fetching from the code memory (eg. for accessing hardcoded tables). Any other indexing you will have to do by laboriously adding the index to the base address.
Your doubt is about adding an 8-bit index to a 16-bit base address, but this is really very simple. If for instance you want to add 22h to 1234h, this is the same as wanting to add 0022h to 1234h. And the dptr is accessible as separate high- (dph) and low (dpl) bytes in the sfr area. Get the picture? So suppose your accu holds the 22h, and dptr holds the 1234h, you would do:
	add	a,dpl	;This adds 22h to 34h, and it may produce a carry.
	mov	dpl,a	;Store the sum in dpl
	clr	a	;Move 00h to the accu
	addc	a,dph	;This adds 00h to 12h, and it propagates the carry to the msb
	mov	dph,a	;Store the sum in dph

And that's it. There are obvious disadvantages:
- The original contents of dptr is destroyed, so is the index contained in a.
To get around this, you could save the contents of dptr on the stack around this routine:
	push	dph	;Save base address
	push	dpl
	add	a,dpl	;This adds 22h to 34h, and it may produce a carry.
	mov	dpl,a	;Store the sum in dpl
	clr	a	;Move 00h to the accu
	addc	a,dph	;This adds 00h to 12h, and it propagates the carry to the msb
	mov	dph,a	;Store the sum in dph
	..		;Use the calculated address
	..
	pop	dpl	;Restore base address
	pop	dph

Remember that stack operations are slooooooow, so it is up to you if you want to save the base address on the stack, or if you'd rather reload it when needed with a short and sweet mov dptr,#... The latter is much more efficient if the base address is an obvious constant.
On top of all that, addc-ing 00 to dph can be abbreviated. You're never going to add anything to dph anyway; all you need to do is account for the carry which may be produced by the first addition:
	add	a,dpl	;This adds 22h to 34h, and it may produce a carry.
	mov	dpl,a	;Store the sum in dpl
	jnc	noinc
	inc	dph	;This propagates the carry to the msb
noinc:	..		;Use the calculated address
	..

Sorry I can't make your life more easy...

Hans


List of 22 messages in thread
TopicAuthorDate
newbie: offsetting the dptr            01/01/70 00:00      
   additional info            01/01/70 00:00      
   do you have external data memory at all?            01/01/70 00:00      
      Exteranal or Internal RAM            01/01/70 00:00      
   In response to Jan and Neil            01/01/70 00:00      
      MOVX @DPTR,A            01/01/70 00:00      
         In response to Jon            01/01/70 00:00      
            Indexing            01/01/70 00:00      
               In response to Hans            01/01/70 00:00      
                  Indexing            01/01/70 00:00      
   In response to Hans            01/01/70 00:00      
      the reason is            01/01/70 00:00      
         In response to Russell            01/01/70 00:00      
            Be careful who you listen to!            01/01/70 00:00      
            movx a,@dptr            01/01/70 00:00      
               In response to Neil            01/01/70 00:00      
                  It Adds            01/01/70 00:00      
               ??????            01/01/70 00:00      
                  oopps no            01/01/70 00:00      
               error            01/01/70 00:00      
               error            01/01/70 00:00      
                  movx/movc            01/01/70 00:00      

Back to Subject List