Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Confused About Pointers (C And Assembly)

align assembly

  • Please log in to reply
1 reply to this topic

#1 KansaiRobot

KansaiRobot

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 10 April 2012 - 11:47 PM

As part of the embedded project I am involved in and which combines code in both C and assembler I have been reading some books. In one of them (about the SuperH controller) I found this:

Code in Assembly defining some sections in memory:

	 .EXPORT  _D_BGN
	 .EXPORT  _R_BGN
	 .EXPORT  _R_END
	 .EXPORT _B_BGN
	 .EXPORT _B_END
	 .SECTION D,DATA,ALIGN=4
	 .SECTION R,DATA,ALIGN=4
	 .SECTION B,DATA,ALIGN=4
	 .SECTION C,DATA,ALIGN=4
_D_BGN: .DATA.L  (STARTOF D)   ;D Begin Address
_R_BGN: .DATA.L  (STARTOF R)   ;R Begin Address
_R_END: .DATA.L  (STARTOF R) +(SIZEOF R)  ;R End Address
_B_BGN: .DATA.L  (STARTOF <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />   ;B Begin Address
_B_END: .DATA.L  (STARTOF <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> +(SIZEOF <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />   ;B End Address
	   .END

and then some code in C to initialize those sections (Copy R to D and set B to 0)

extern int *D_BGN, *R_BGN, *R_END, *B_BGN, *B_END;
void INITSCT(void)
{
  int *p, *q;
for(p=R_BGN,q=D_BGN;p<R_END;p++,q++)
   *p=*q;  /*section D Initialize */
for(p=B_BGN;p<B_END;p++)
   *p=0;  /*Section B Initialize */
}

My confusion comes when checking the pointers (p and q in particular)

In the assembly code _D_BGN (and the others) is the address of memory at which is stored another address which is where region D begins.

In the C code though, p is just a pointer (address) to a region of memory and it is this region of memory the one that is modified.

How this can be? I am starting to doubt my knowledge about what a pointer in C is....

Any help will be greatly appreciated

Thanks

Kansai
  • 0

#2 BlackCase

BlackCase

    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 11 April 2012 - 02:16 AM

In the assembly code _D_BGN contains the address of the beginning of the region D, so when you pass this value to p, in the C code, you are forcing the pointer *p to point to the same address contained in _D_BGN.. So increasing the value of p you are moving through the region D.. You have to keep in mind that *p is the pointer to the memory region identified by the address contained in p..
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download