I read a datasheet and I found variable defined as unsigned char near Sample_X
. What is this, and how does this differ from unsigned char Sample_X
?
2 Answers
The MCU specified here is a Freescale MC9C08 series, which uses a slightly enhanced version of their HC08 architecture. This is an 8-bit core, which (like many such) has shorter instructions and faster access to "zero page" addresses than others. Zero-page addresses are only 8 bits long instead of 16 bits, so instructions referencing them can be 2 bytes instead of 3, and take 1 cycle less to execute as a result.
The "near" keyword instructs the compiler to put the variable in "zero page" if possible, for better performance. Compilers specifically written for embedded development, such as this one, usually implement such extensions to the language (here described as "C Support for Zero Page").
-
\$\begingroup\$ I like this answer, but do you have a source for your information? \$\endgroup\$– ClonkexCommented Apr 17, 2018 at 0:37
-
1\$\begingroup\$ I edited the answer to provide more detail and references. In fact it's from Freescale's newer HCS08 architecture, but this is code-compatible with the older HC08 and HC05 architectures. The (now positively ancient) 6800 and 6502 architectures are also closely related. \$\endgroup\$ Commented Apr 17, 2018 at 1:27
-
-
\$\begingroup\$ So, its usage is very similar to the
register
keyword (for cases where you are really sure that this is where you need optimization), but slightly less extreme? \$\endgroup\$– vszCommented Apr 17, 2018 at 4:24 -
\$\begingroup\$ More or less. The difference is mainly due to the fact that 8-bit CPUs typically don't have a register bank per se, only a single accumulator and a couple of index registers. But it's also analogous to the use of "near" vs. "far" pointers on x86, in that there is a difference in the size of the address and how long it takes to complete an access. \$\endgroup\$ Commented Apr 17, 2018 at 13:13
Depending on the CPU architecture there may be different instructions to access data at different address. Here is an example from Keil for one of their compilers.
The near access has a certain memory limit, so you may give hints to the compiler to place some frequently used variables in an area that is accessible by shorter instructions (obviously describing access to an 32-bit address space is larger than 16-bit address space). This can translate to smaller/faster code.
-
7\$\begingroup\$ There's more information in What is the difference between far pointers and near pointers? \$\endgroup\$ Commented Apr 16, 2018 at 11:50
-
5\$\begingroup\$ @AndrewMorton: That question relates to
far
andnear
pointers on the largely-obsolete 8086 architecture. The same keywords are used in related but different fashion on 8-bit microcontrollers. \$\endgroup\$– supercatCommented Apr 16, 2018 at 16:02 -
\$\begingroup\$ @supercat The question just asks generally, although the answers focus on the old architecture. Someone knowledgeable should write an answer on it explaining how they are used for 8bit microcontrollers. \$\endgroup\$ Commented Apr 17, 2018 at 3:20
-
\$\begingroup\$ @curiousdannii: The code in question was written for an 8-bit micro. \$\endgroup\$– supercatCommented Apr 17, 2018 at 13:57
near
, is an example of a storage class specifier. It tells the compiler something about how or where the storage for the variable should be allocated. (See filo's answer, below, for more aboutnear
). \$\endgroup\$