新闻  |   论坛  |   博客  |   在线研讨会
一个有趣的关于C宏的题目及巧解。(建议您不要看答案,先尝试自己做做看)
张阳 | 2008-06-03 17:11:35    阅读:1672   发布文章

 给大家出道题吧

定义一个C宏,实现插入0~255个NOP指令。
几天后公布答案。
比如:_nopx(3);// 插入3个NOP

(建议您不要看答案,先尝试自己做做看。做完后向下翻看匠人的答案。)

……

……

……

……

……

……

……

……

……

(不要偷看答案!)

……

……

……

……

……

……

……

……

……

……

(你确定要看答案吗?)

……

……

……

……

……

……

……

……

(好吧,现在公布答案。)//宏定义方法:
#define __NOP1__ asm("nop");
#define __NOP2__ __NOP1__ __NOP1__
#define __NOP4__ __NOP2__ __NOP2__
#define __NOP8__ __NOP4__ __NOP4__
#define __NOP16__ __NOP8__ __NOP8__
#define __NOP32__ __NOP16__ __NOP16__
#define __NOP64__ __NOP32__ __NOP32__
#define __NOP128__ __NOP64__ __NOP64__


#define __NOPX__(a)             \
    if ((a)&(0x01))    {__NOP1__}                    \
    if ((a)&(0x02))    {__NOP2__}                    \
    if ((a)&(0x04))    {__NOP4__}                    \
    if ((a)&(0x08))    {__NOP8__}                    \
    if ((a)&(0x10))    {__NOP16__}                    \
    if ((a)&(0x20))    {__NOP32__}                    \
    if ((a)&(0x40))    {__NOP64__}                    \
    if ((a)&(0x80))    {__NOP128__}                    

//宏引用方法(举例):

    __NOPX__(13)

//编译结果:
138:                   __NOPX__(13)
  051D    0000     NOP
  051E    0000     NOP
  051F    0000     NOP
  0520    0000     NOP
  0521    0000     NOP
  0522    0000     NOP
  0523    0000     NOP
  0524    0000     NOP
  0525    0000     NOP
  0526    0000     NOP
  0527    0000     NOP
  0528    0000     NOP
  0529    0000     NOP

//说明,此宏在PICC中编译通过

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客