View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
Why we use #BYTE? |
Posted: Thu Aug 28, 2014 2:28 pm |
|
|
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
|
|
Posted: Thu Aug 28, 2014 6:24 pm |
|
|
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:
With your method you have to do this:
Code: | value = *access_pointer;
// or
value = *PORTB;
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Aug 29, 2014 3:29 am |
|
|
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
|
|
Posted: Fri Aug 29, 2014 7:00 am |
|
|
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
|
|
Posted: Fri Aug 29, 2014 2:42 pm |
|
|
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
|
|
Posted: Fri Aug 29, 2014 6:21 pm |
|
|
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
|
|
Posted: Fri Aug 29, 2014 11:47 pm |
|
|
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
|
|
Posted: Sat Aug 30, 2014 12:50 am |
|
|
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. |
|
|
|