CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Why we use #BYTE?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

Why we use #BYTE?
PostPosted: Thu Aug 28, 2014 2:28 pm     Reply with quote

Hello fellows.
Reading some of the posts inside the forum, something made me curious.
Here it is:
Why do we use
Code:
#BYTE MY_ID = getenv("SFR:PORTB")
, instead of:
Code:
int * const access_pointer = getenv("SFR:PORTB")
? They both do the same, right?

Of course access_pointer might be either 8bit or 16bit depending the device architecture.

Thank you
_________________
A person who never made a mistake never tried anything new.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 6:24 pm     Reply with quote

My guess is that it allows you to refer to register as a named variable.
You don't have to de-reference the pointer. You can do this:
Code:
value = PORTB;

With your method you have to do this:
Code:
value = *access_pointer;
// or
value = *PORTB;
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 3:29 am     Reply with quote

Internally, it is very different, but the optimiser would handle this.

The #byte version, creates a variable 'at' the address.

The pointer, creates a pointer 'pointing to' the address.

So without the optimiser, the access to the pointer would be more complex, involving loading the address, and then doing an indirect access to the data at the address.
However with a constant pointer, the optimiser could then simplify it to the same result, which it does.
So:
Code:

   int fred;

   fred=MY_ID;
   fred = *(access_pointer);


Both result in the same code, but I think the former is much neater code, especially if the names used were made more informative.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 7:00 am     Reply with quote

Sometimes you have to look at the generated code:
Code:
#include <18F458.h>

#BYTE PORTB = getenv("SFR:PORTB")   
int * const access_pointer = getenv("SFR:PORTB");

void main()
{
   int8 value;
   
   value = PORTB;   
   value = *access_pointer;
}

Code:
....................    int8 value;
....................     
....................    value = PORTB;   
0014:  MOVFF  PORTB,value
....................    value = *access_pointer;
0018:  MOVFF  PORTB,value
.................... }
No difference...
On a PIC16 the code is different but both are still equal.

Perhaps it is a leftover from the past. With current compilers there is no difference in generated code. I find the #byte notation easier to read but there is something to say in favour of the (more) portable constant-pointer notation.

Right now I would say to choose whichever you feel more comfortable with.
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 2:42 pm     Reply with quote

Thank you all guys!

Just to clarify something. Ttelmah, do you mean that in case of using #BYTE we actually do not allocate any GPR memory space for the MY_ID since it is assigned name to a SFR that is "allocated" anyway? For difference, the access_pointer being used we allocate addition memory for the very pointer(access_pointer)?
So, if I understood correct, in the first case (#BYTE) we spare a bunch of memory.
_________________
A person who never made a mistake never tried anything new.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 6:21 pm     Reply with quote

nope... same memory use !

as ckielstra's post clearly shows, exact same code and memory used for either way, though the first 'looks nicer, easier to understand'(at least to me)...


jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 11:47 pm     Reply with quote

The key is the optimiser.

The point is that this 'works out' that since you are using a constant pointer, it doesn't have to do pointer accesses, and substitutes the same code as for #byte.
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Sat Aug 30, 2014 12:50 am     Reply with quote

I see now. I am also tend to use #BYTE due to the reasons you provide me. Dereferencing the pointer makes the code more likely to wrong. In other hand #BYTE is cleaner and safer way from programmer's perspective.

Thanks to all of you.
_________________
A person who never made a mistake never tried anything new.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group