12
\$\begingroup\$

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?

\$\endgroup\$
  • 3
    \$\begingroup\$ perhaps allows the compiler to use small relative offsets? \$\endgroup\$ – Neil_UK Apr 16 '18 at 11:41
  • 6
    \$\begingroup\$ Based on part experience with x86 in DOS, I would expect this with some pointer type and in a 16bit environment. Yet what the OP quoted does not look like a pointer and the datasheet link would imply some MCU. Search for the keyword "near" in the following two links: microchip.com/forums/m549709.aspx barrgroup.com/Embedded-Systems/How-To/Efficient-C-Code \$\endgroup\$ – frr Apr 16 '18 at 11:58
  • 3
    \$\begingroup\$ It should be noted that this is a compiler extensions, not standard C \$\endgroup\$ – PlasmaHH Apr 16 '18 at 13:03
  • \$\begingroup\$ It may be about CCM. Considering that code is mainly targeted for a MCU, it may be an indirect suggestion to linker by the support of compiler for placing it on CCM (core coupled memory) if available. \$\endgroup\$ – Ayhan Apr 16 '18 at 13:40
  • 1
    \$\begingroup\$ FYI, the keyword, 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 about near). \$\endgroup\$ – Solomon Slow Apr 16 '18 at 14:25
20
\$\begingroup\$

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").

\$\endgroup\$
  • \$\begingroup\$ I like this answer, but do you have a source for your information? \$\endgroup\$ – Clonkex Apr 17 '18 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\$ – Chromatix Apr 17 '18 at 1:27
  • \$\begingroup\$ Nice, much better! \$\endgroup\$ – Clonkex Apr 17 '18 at 1:30
  • \$\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\$ – vsz Apr 17 '18 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\$ – Chromatix Apr 17 '18 at 13:13
15
\$\begingroup\$

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.

\$\endgroup\$
  • 7
    \$\begingroup\$ There's more information in What is the difference between far pointers and near pointers? \$\endgroup\$ – Andrew Morton Apr 16 '18 at 11:50
  • 5
    \$\begingroup\$ @AndrewMorton: That question relates to far and near pointers on the largely-obsolete 8086 architecture. The same keywords are used in related but different fashion on 8-bit microcontrollers. \$\endgroup\$ – supercat Apr 16 '18 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\$ – curiousdannii Apr 17 '18 at 3:20
  • \$\begingroup\$ @curiousdannii: The code in question was written for an 8-bit micro. \$\endgroup\$ – supercat Apr 17 '18 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.