English 中文(简体)
你们如何控制你们的汇编者的最佳选择?
原标题:How do you control what your C compiler Optimizes?

I am writing the firmware for an embedded device in C using the Silicon Labs IDE and the SDCC compiler. The device architecture is based on the 8051 family. The function in question is shown below. The function is used to set the ports on my MCU to drive a stepper motor. It gets called in by an interrupt handler. The big switch statement just sets the ports to the proper value for the next motor step. The bottom part of the function looks at an input from a hall effect sensor and a number of steps moved in order to detect if the motor has stalled. The problem is, for some reason the second IF statement that looks like this if (StallDetector > (GapSize + 20)) { HandleStallEvent(); } always seems to get optimized out. If I try to put a breakpoint at the HandleStallEvent() call the IDE gives me a message saying "No Address Correlation to this line number". I am not really good enough at reading assembly to tell what it is doing but I have pasted a snippet from the asm output below. Any help would be much appreciated.

void OperateStepper(void)
{
    //static bit LastHomeMagState = HomeSensor;
    static bit LastPosMagState = PosSensor;
    if(PulseMotor)
    {
        if(MoveDirection == 1) // Go clockwise
        {
            switch(STEPPER_POSITION) 
            {
                case  A : 
                     STEPPER_POSITION =  B ;
                     P1 = 0xFD;
                     break;
                case  B :
                     STEPPER_POSITION =  C ;
                     P1 = 0xFF;
                     break;
                case  C :
                     STEPPER_POSITION =  D ;
                     P1 = 0xFE;
                     break;
                case  D :
                     STEPPER_POSITION =  A ;
                     P1 = 0xFC;
                     break; 
                default:
                     STEPPER_POSITION =  A ;
                     P1 = 0xFC;
            }   //end switch
        }
        else                // Go CounterClockwise
        {
            switch(STEPPER_POSITION) 
            {
                case  A : 
                     STEPPER_POSITION =  D ;
                     P1 = 0xFE;
                     break;
                case  B : 
                     STEPPER_POSITION =  A ;
                     P1 = 0xFC;
                     break;
                case  C : 
                     STEPPER_POSITION =  B ;
                     P1 = 0xFD;
                     break;
                case  D : 
                     STEPPER_POSITION =  C ;
                     P1 = 0xFF;
                     break; 
                default: 
                     STEPPER_POSITION =  A ;
                     P1 = 0xFE;
            }   //end switch
        }   //end else

        MotorSteps++;
        StallDetector++;

        if(PosSensor != LastPosMagState)
        {
            StallDetector = 0;

            LastPosMagState = PosSensor;
        }
        else
        {
            if (PosSensor == ON) 
            {
                if (StallDetector > (MagnetSize + 20))
                {
                    HandleStallEvent();
                }
            }
            else if (PosSensor == OFF) 
            {
                if (StallDetector > (GapSize + 20))
                {
                    HandleStallEvent();
                }
            }
        }

    }   //end if PulseMotor
}

......以及这一职能下部分的产出......

;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:653: if(PosSensor != LastPosMagState)
    mov c,_P1_4
    jb  _OperateStepper_LastPosMagState_1_1,00158$
    cpl c
00158$:
    jc  00126$
    C$MotionControl.c$655$3$7 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:655: StallDetector = 0;
    clr a
    mov _StallDetector,a
    mov (_StallDetector + 1),a
    C$MotionControl.c$657$3$7 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:657: LastPosMagState = PosSensor;
    mov c,_P1_4
    mov _OperateStepper_LastPosMagState_1_1,c
    ret
00126$:
    C$MotionControl.c$661$2$8 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:661: if (PosSensor == ON) 
    jb  _P1_4,00123$
    C$MotionControl.c$663$4$9 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:663: if (StallDetector > (MagnetSize + 20))
    mov a,_MagnetSize
    mov r2,a
    rlc a
    subb    a,acc
    mov r3,a
    mov a,#0x14
    add a,r2
    mov r2,a
    clr a
    addc    a,r3
    mov r3,a
    clr c
    mov a,r2
    subb    a,_StallDetector
    mov a,r3
    subb    a,(_StallDetector + 1)
    jnc 00130$
    C$MotionControl.c$665$5$10 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:665: HandleStallEvent();
    ljmp    _HandleStallEvent
00123$:
    C$MotionControl.c$668$2$8 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:668: else if (PosSensor == OFF) 
    jnb _P1_4,00130$
    C$MotionControl.c$670$4$11 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:670: if (StallDetector > (GapSize + 20))
    mov a,#0x14
    add a,_GapSize
    mov r2,a
    clr a
    addc    a,(_GapSize + 1)
    mov r3,a
    clr c
    mov a,r2
    subb    a,_StallDetector
    mov a,r3
    subb    a,(_StallDetector + 1)
    jnc 00130$
    C$MotionControl.c$672$5$12 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:672: HandleStallEvent();
    C$MotionControl.c$678$2$1 ==.
    XG$OperateStepper$0$0 ==.
    ljmp    _HandleStallEvent
00130$:
    ret

看起来像汇编者一样,如果从时间角度讲的话,NOT优化了第二次发言,但如果是这样的话,那么,民主选举学会为什么不允许我在那里打断点呢? 也许这只是一个umb子。

最佳回答

综合框架声明没有得到最佳利用。 这是法典。

    C$MotionControl.c$670$4$11 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:670: if (StallDetector > (GapSize + 20))
    mov a,#0x14       ; r2,r3 = 20 + GapSize
    add a,_GapSize    ; (adding a 16-bit number in two 8-bit steps)
    mov r2,a
    clr a
    addc    a,(_GapSize + 1)
    mov r3,a
    clr c
    mov a,r2          ; subtracting in two 8-bit steps
    subb    a,_StallDetector
    mov a,r3
    subb    a,(_StallDetector + 1)
    jnc 00130$        ; jump if carry not set (fall through if carry set)
    C$MotionControl.c$672$5$12 ==.
;   C:SiLabsOptec ProgramsHSFW_HID_SDCC_2MotionControl.c:672: HandleStallEvent();
    C$MotionControl.c$678$2$1 ==.
    XG$OperateStepper$0$0 ==.
    ljmp    _HandleStallEvent    ; it knows HandleStallEvent does not return!
00130$:                            ; or rather, it knows this handler cannot return, so there s no need to call.
    ret

我注意到,在第3行至最后一行,正在发生一些不公事件。 它没有履行要求<代码>的职能。 HandleStall 活动。 它正在做长篇大事,因此它显然知道,<代码>HandleStallEvent不能返回。 我也看到,在以上两行中,它正在确定与跳跃指示有关的组装符号。 因此,其编号为第678条。 如果民主选举学会赢得笔记,那么你就可以在第678条线上设定一个突破点,或许你可以找到第678条线的中子地址,并将之定在日间地址。 您可能尝试的另一件事是,在这条线路之前插入一个地方变量定义,如<代码>int Breakhere = 1,并看这是否给你一些指示。

BTW, 你可以看到,万国邮联认为8点数字,这样,如果你能够使用果园而不是短 short,就会节省指示。 节省的时间是否值得,这取决于该机器在该法中所占的多少时间。

BTW2,如果你想把业绩挤出这一陷阱,那么我所依赖的是,当我从事嵌入式工作时,随机阻止了民主选举学会(或英特尔“Blue Box” ICE)。

问题回答

它称为“尾声优化”。

操作员在接手要求后做不到任何事情,因此,没有回去的余地。 你们只是把教育与职业教育与培训结合起来,这浪费了教学内容AND/em>。

阅读“Lambda:The final ......”麻省理工学院的拉比蚊,了解更多细节。

• 定期,而不是呼吁打破你对手脚的 break点。

You can usually tweak the optimizer with #pragma statements. I don t know the exact syntax for your compiler, but you should be able to find it in the docs which come with your compiler/ide.

类似情况

#pragma optimize( "", off )
//now the function which should not be optimized
#pragma optimize( "", on )

如何配置优化取决于汇编者。 rel=“nofollow noreferer”>manual for SDCC有其优化选择,列于第3.28节。 在源代码一级,你可以使用指挥线两种选择,也可以使用pragmas。 在全球范围进行脱节优化,看看你是否发挥同样的作用。 通常,在残疾者最佳化情况下通过守则,将消除无法确定休息点的问题。 如果这项工作得到罚款,你可以尝试在职能一级将疑犯最佳化物与花.混为一谈,以找出可能引发这一问题。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...