Considerations for programming on the big and small ends

Source: Internet
Author: User

Generally, all we encounter is a small-end scenario. A typical X86 processor is a small-end scenario.
 
However, many powerpc processors can be configured in the big-end mode or small-end mode. Therefore, the previous code is often run well, to the new board, or board configuration
 
If a change occurs, the result is incorrect and debugging is difficult. Because it is not considered wrong at all.
 
The following is an issue that we have detected after a long time in the code.
 
Another function:
 
I2c_write (..., unsigned char * reg ,...)
 
The reg type here is an unsigned char pointer, representing the Register address of the i2c device.
 
In the previous code, a cTest with the int type will be declared before passing the parameter, and then the following call will be made:
 
I2c_write (..., (unsigned char *) & cTest ,...);
 
On the previous simba Board, there was no problem with this. Simba is a board of the MPC8572 on the powerpc platform. It was configured as a small-end model.
 
Yes. In this case, it seems that there will be no problems.
 
For example
 
Int cTest = 2;
 
That is, cTest = 0x00000002, the passed Pointer Points to the address where 0x02 is located, that is, the lowest cTest address.
 
Now it is changed to tembo, which is a P4080 board on the powerpc platform and configured as the big-end mode.
 
Still yes
 
Int cTest = 2;
 
That is, cTest = 0x00000002. Does the pointer still point to the address where 0x02 is located?
 
Apparently not!
 
We can see (unsigned char *) & cTest this operation, this C Standard certainly has a provision, will get a pointer to the lowest address of & cTest unsigned char type.
 
At this time, the priority bit of & cTest is the highest valid bit, and the content is 0x00. Therefore, no correct result will be obtained!
 
It took a long time to solve such a small problem. Ultimately, it is caused by programming habits.
 
It is too casual to write code, regardless of the platform or parameter type. In fact, it is best to include signed and unsigned In the Parameter definition.
 
It cannot be assumed that the compiler considers the char type to be signed or not, although it is signed in most cases.
 
There is also strict parameter passing. It is best to define what type of parameters are needed. Do not make any assumptions about the compiler!
 
 
 
From Jing chaotian's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.