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\$
6
  • 3
    \$\begingroup\$ perhaps allows the compiler to use small relative offsets? \$\endgroup\$
    – Neil_UK
    Commented Apr 16, 2018 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
    Commented Apr 16, 2018 at 11:58
  • 3
    \$\begingroup\$ It should be noted that this is a compiler extensions, not standard C \$\endgroup\$
    – PlasmaHH
    Commented Apr 16, 2018 at 13:03
  • 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\$ Commented Apr 16, 2018 at 14:25
  • 1
    \$\begingroup\$ Is this really on-topic here? \$\endgroup\$
    – user541686
    Commented Apr 16, 2018 at 23:26

2 Answers 2

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\$
5
  • \$\begingroup\$ I like this answer, but do you have a source for your information? \$\endgroup\$
    – Clonkex
    Commented 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\$
    – Chromatix
    Commented Apr 17, 2018 at 1:27
  • \$\begingroup\$ Nice, much better! \$\endgroup\$
    – Clonkex
    Commented Apr 17, 2018 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
    Commented 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\$
    – Chromatix
    Commented Apr 17, 2018 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\$
4
  • 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 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
    Commented 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\$
    – supercat
    Commented Apr 17, 2018 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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