5

This question came to my mind while writing some firmware for a PIC microcontroller.

There are two methods I know to initialize registers in a microcontroller. Say for an example, if we are initializing a port as outputs, one way is to write a command like the following and it will assign 1 to every bit in TRISx register

Method 1

TRISX = 0xFF;

The same thing can be done by assigning bits individually.

Method 2

_TRISX0 = 1;
_TRISX1 = 1;
_TRISX2 = 1;
...
_TRISX7 = 1;

My question is, will it get treated as same by compiler and the time taken to complete both the operations are same? Or does method 1 take one clock cycle while method 2 takes 8 (I mean ~8 times slower)?

I tried reading X16 compiler guide but couldn't find any tips.

3
  • 4
    Why not inspecting the generated machine code?
    – MrTux
    Jun 18, 2019 at 5:52
  • Machine code generated by compiler is a hex file right?
    – Padmal
    Jun 18, 2019 at 6:19
  • 1
    A hex file is rather the binary interpretation of the raw machine code, expressed in an ASCII format. They are not human-friendly reading. What you should be looking at instead is the disassembly - any decent tool chain has a disassembler option. Good ones show the disassembly together with the C source. Some good debuggers also have this option. At the very least you'll be able to see the raw disassembly in any debugger.
    – Lundin
    Jun 18, 2019 at 7:00

3 Answers 3

11

Hardware registers are always volatile qualified and the compiler is not allowed to optimize code containing volatile access. So if you write to them 8 times, then 8 writes is what you get. This is of course much slower than 1 write.

In addition, it is very bad practice to write to registers several times in a row just as if they were a temporary variable in RAM. Hardware registers tend to have all manner of subtle side-effects. They can have "write-once" attribute or only accept writes in certain modes. By writing to them in several steps, you make a habit of creating all manner of crazy, subtle problems caused by incorrect register setups.

Correct practice is to write to registers once, or as few times as necessary.

For example, you may think that a data direction register as in your example is a pretty dumb one with no side-effects. But often GPIO hardware needs some time to toggle the port circuits, from the point where you write to the data direction register to the point where you access the I/O port. So it is possible that several writes would stall the port needlessly.

Assuming REGISTER is the name of the memory-mapped, volatile-qualified hardware register, then...

Don't do this:

MASK1 = calculation();
REGISTER |= MASK1;
MASK2 = calculation();
REGISTER |= MASK2;

Do this:

uintx_t reg_val=0; // temp variable in RAM
MASK1 = calculation();
reg_val |= MASK1;
MASK2 = calculation();
reg_val |= MASK2;
REGISTER = reg_val; // single write to the actual register
11
  • Beware that your answer is not applicable to everything in the embedded world. Some processors (and compilers) support bit manipulation; and, for example, some Renesas C compiler can optimize subsequent bit-writes on the same register (coalesces them). Jun 19, 2019 at 9:16
  • 1
    @linuxfan Nope, the (conforming) compiler isn't allowed to do that on a volatile-qualified register. Suppose I want to disable an interrupt, then clear a status flag, and these bits are part of the same register. Merging those two bit instructions into one would change the meaning of the code. The intention is to let a pending interrupt finish and then clear the flag that the interrupt had set. If done in a single instruction, the interrupt would never be executed.
    – Lundin
    Jun 19, 2019 at 9:23
  • Or an even more obvious example: suppose I want to bit-bang something to shift registers through GPIO.
    – Lundin
    Jun 19, 2019 at 9:25
  • For example, this C compiler: renesas.com/us/en/doc/products/tool/002/rej10j1995_nc30_u.pdf says, at page 19, "-Ono_bit -ONB Suppresses optimization based on grouping of bit manipulations", or even "-ONLOC Suppresses the optimization that puts consecutive OR together. I am not saying that this compiler is perfect, but consider that C language has scarce support for bits. Jun 19, 2019 at 10:13
  • @linuxfan The options you describe say just the same thing as me: suppress optimization, when optimization would break the intended program behavior.
    – Lundin
    Jun 19, 2019 at 10:41
7

It will depend on the processor instruction set and the compiler. For the PIC18F45K20, for example, the sdcc compiler compiles the following

TRISDbits.TRISD0 = 1;

to

BSF _TRISDbits, 0

while it compiles

TRISD = 0xFF;

to

MOVLW   0xff
MOVWF   _TRISD

So in this case setting an individual bit is faster, because it does not involve placing a temporary value in the working register.

Not all instruction sets include a BSF instruction however, and some architectures would not require the use of the working register for the latter task.

P.S. The above examples are based on the output of the sdcc compiler, but I imagine the xc8 and xc16 compilers yield similar results.

P.P.S. When inspecting the generated assembly, bear in mind that some instructions consume more processor cycles than others. See datasheet for details.

1

One thing is, you haven't provided C code to show how are those bits actually referenced. But let's say it's through union & struct of bit-fields.

The best way is to actually examine the ASM that the compiler generates. You do need to know your hw arch, but you would still need to look at the generated ASM to really know.

To assign just a single bit , say _TRISX0=1; vs TRISX = 0x01;, depending on the arch and compiler, it is possible that compiler can generate more efficient (less cycles and may be less instructions) code for just single bit assignment than entire register. There is at least one such MCU/DSP processor and compiler, from TI, for which I know this is true.

For case when you have multiple (>1) statements, your Method 2, with individual bit assignments, it is likely that your one-liner register assignment will be more or as efficient: if compiler deduces - wrongly or not - that all of those bit assignments assign to same register in sequence, it may replace them with one-liner as you could have in method 1.

I do not have PIC specifically in mind. I'm advising to examine ASM for any MCU, when you care.

4
  • If the CPU supports bit instructions but generates a less efficient program for C code doing byte access of the register, the compiler is pretty horrible. You have the same situation on normal, byte-wise oriented CPUs with direct writes versus read-modify-write instructions. It is the compiler's job to pick the right one, not the C programmer.
    – Lundin
    Jun 18, 2019 at 6:57
  • @Lundin, by "byte-wise oriented CPU" do you mean CPU with 8-bit instructions and 8-bit registers? I understand "It is the compiler's job to pick the right one, not the C programmer" but this does not work always for hardware programming.
    – v01d
    Jun 18, 2019 at 23:57
  • @Lundin "If the CPU supports bit instructions but generates a less efficient program for C code doing byte access of the register, the compiler is pretty horrible." This depends on architecture - addressing modes, available instructions, and the register you accessing. An efficient/1-cycle bitwise OR instruction to a byte register on an arch with otherwise 16-bit bit instructions may break your assertion. A C's byte you thinking in - i.e. uint8_t - may not even map directly to hardware word or directly addressable memory.
    – v01d
    Jun 19, 2019 at 1:30
  • The situation you talk about where writing a single bit instead of a whole register is only true for cores with single bit instructions, that are faster than byte-wise instructions.
    – Lundin
    Jun 19, 2019 at 6:23

Your Answer

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

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