C / C ++ macro for code repetition

Is there a way to repeat CN code times with a macro? Also N is a macro.
For example, if I have these macros:

#define N 5  
#define COODE "nop\n\t"
#define REPEAT [...]

When called again, the preprocessor writes CODE N times, so

 __asm__(REPEAT);

has become

__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");

I have an Arduino that has to wait for the exact (and small, around 10-15) number of hours. Each "nop" (without operation) takes exactly 1 clock cycle, which must be executed, and it does nothing. I canโ€™t just execute a loop, because each loop is executed in more than one operation (initialize the counter, increase the counter, check if it is reached), therefore, instead of writing "nop \ n \ t" manually, I would like to have a macro. That way, I can simply change N to change the program without rewriting it.

Thank you in advance

+4
3

define, :

//By Christopher Andrews, released under MIT licence.

template< unsigned N > struct Nops{
  static void generate() __attribute__((always_inline)){
    __asm__ volatile ("nop");
    Nops< N - 1 >::generate();
  }
};
template<> struct Nops<0>{ static inline void generate(){} };

void setup() {
  Nops<10>::generate();
}

void loop(){}

.

0000010a:
 10a: 00 00 nop
 10c: 00 00 nop
 10e: 00 00 nop
 110: 00 00 nop
 112: 00 00 nop
 114: 00 00 nop
 116: 00 00 nop
 118: 00 00 nop
 11a: 00 00 nop
 11c: 00 00 nop
 11e: 08 95 ret

TFT Arduino.

EDIT:

AVR, avr-gcc. , .

, GCC

void __builtin_avr_delay_cycles (unsigned long ticks) ticks - . , , . ;

: https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/AVR-Built_002din-Functions.html

+5

Boost Boost.Preprocessor, . From:

http://www.boost.org/doc/libs/1_57_0/libs/preprocessor/doc/index.html

BOOST_PP_REPEAT

:

#include <boost/preprocessor/repetition/repeat.hpp>

#define OP(z, n, text) text
...
__asm__( BOOST_PP_REPEAT(5, OP, "noop\n"\t);
0

The following code works with GNU C,

#define NOP __asm__("nop")

#define ten(a)     a;a;a;a;a;a;a;a;a;a
#define handred(a) ten(ten(a))


int
main()
{
    handred(NOP);
    return 0;
}

Compile and debug:

code@lab:~/debug$ gcc -g -o debug_NOP debug_NOP.c
code@lab:~/debug$ gdb -q --nh debug_NOP
Reading symbols from debug_NOP...done.
(gdb) set disassembly-flavor intel
(gdb) start
Temporary breakpoint 1 at 0x664: file debug_NOP.c, line 10.
Starting program: /home/code/debug/debug_NOP 

Temporary breakpoint 1, main () at debug_NOP.c:10
10      handred(NOP);
(gdb) disassemble 
Dump of assembler code for function main:
   0x0000555555554660 <+0>: push   rbp
   0x0000555555554661 <+1>: mov    rbp,rsp
=> 0x0000555555554664 <+4>: nop
   0x0000555555554665 <+5>: nop
   0x0000555555554666 <+6>: nop
   0x0000555555554667 <+7>: nop
   0x0000555555554668 <+8>: nop
   0x0000555555554669 <+9>: nop
   ....
   0x00005555555546c6 <+102>:   nop
   0x00005555555546c7 <+103>:   nop
   0x00005555555546c8 <+104>:   mov    eax,0x0
   0x00005555555546cd <+109>:   pop    rbp
   0x00005555555546ce <+110>:   ret    
End of assembler dump.
0
source

All Articles