gpt4 book ai didi

c - 在 Microchip C18 中,为什么插入 NOP 会导致更大的代码?

转载 作者:太空狗 更新时间:2023-10-29 16:43:51 25 4
gpt4 key购买 nike

我在 ISR 中有一些代码。给出代码是为了完整性,问题仅与注释掉的 __asm_ block 有关。

没有 __asm_ block ,它被编译成 82 条指令。对于 __asm_ block ,结果是 107 条指令。为什么差别这么大?

这是 C 代码:

if (PIR1bits.SSPIF)
{
spi_rec_buffer.read_cursor = 0;
spi_rec_buffer.write_cursor = 0;

LATAbits.LATA4 ^= 1;
// _asm nop nop _endasm
LATAbits.LATA4 ^= 1;

while (!PORTAbits.NOT_SS && spi_rec_buffer.write_cursor < spi_rec_buffer.size)
{
spi_rec_buffer.data[spi_rec_buffer.write_cursor] = SSPBUF;
SSPBUF = spi_out_msg_buffer.data[spi_out_msg_buffer.read_cursor];
PIR1bits.SSPIF = 0;
spi_rec_buffer.write_cursor++;
spi_out_msg_buffer.read_cursor++;
if (spi_out_msg_buffer.read_cursor == spi_out_msg_buffer.write_cursor)
LATAbits.LATA4 = 0;
LATBbits.LATB1 = 1;
while (!PORTAbits.NOT_SS && !PIR1bits.SSPIF);
LATBbits.LATB1 = 0;
}

spi_message_locked = true;
spi_message_received = true;

}

没有 NOP:

BTFSS     0x9e,0x3,0x0      if (PIR1bits.SSPIF)
BRA 0x2ba
{
MOVLB 0xf spi_rec_buffer.read_cursor = 0;
CLRF 0x4,0x1
CLRF 0x5,0x1
CLRF 0x6,0x1 spi_rec_buffer.write_cursor = 0;
CLRF 0x7,0x1
BTG 0x89,0x4,0x0 LATAbits.LATA4 ^= 1;
BTG 0x89,0x4,0x0 LATAbits.LATA4 ^= 1;
MOVF 0x80,0x0,0x0 while (!PORTAbits.NOT_SS && spi_rec_buffer.write_cursor < spi_rec_buffer.size)
ANDLW 0x20
BNZ 0x2b0
MOVLB 0xf
MOVF 0x7,0x0,0x1
XORWF 0x3,0x0,0x1
BTFSS 0xe8,0x7,0x0
BRA 0x254
RLCF 0x3,0x0,0x1
BRA 0x25c
MOVF 0x2,0x0,0x1
SUBWF 0x6,0x0,0x1
MOVF 0x3,0x0,0x1
SUBWFB 0x7,0x0,0x1
BC 0x2b0
BRA 0x240
{
MOVF 0x0,0x0,0x1 spi_rec_buffer.data[spi_rec_buffer.write_cursor] = SSPBUF;
ADDWF 0x6,0x0,0x1
MOVWF 0xe9,0x0
MOVF 0x1,0x0,0x1
ADDWFC 0x7,0x0,0x1
MOVWF 0xea,0x0
MOVFF 0xfc9,0xfef
MOVLB 0xf SSPBUF = spi_out_msg_buffer.data[spi_out_msg_buffer.read_cursor];
MOVF 0x10,0x0,0x1
ADDWF 0x14,0x0,0x1
MOVWF 0xe9,0x0
MOVF 0x11,0x0,0x1
ADDWFC 0x15,0x0,0x1
MOVWF 0xea,0x0
MOVF 0xef,0x0,0x0
MOVWF 0xc9,0x0
BCF 0x9e,0x3,0x0 PIR1bits.SSPIF = 0;
MOVLB 0xf spi_rec_buffer.write_cursor++;
INCF 0x6,0x1,0x1
MOVLW 0x0
ADDWFC 0x7,0x1,0x1
MOVLB 0xf spi_out_msg_buffer.read_cursor++;
INCF 0x14,0x1,0x1
ADDWFC 0x15,0x1,0x1
MOVF 0x16,0x0,0x1 if (spi_out_msg_buffer.read_cursor == spi_out_msg_buffer.write_cursor)
XORWF 0x14,0x0,0x1
BNZ 0x29e
MOVF 0x17,0x0,0x1
XORWF 0x15,0x0,0x1
BNZ 0x29e
BCF 0x89,0x4,0x0 LATAbits.LATA4 = 0;
BSF 0x8a,0x1,0x0 LATBbits.LATB1 = 1;
MOVF 0x80,0x0,0x0 while (!PORTAbits.NOT_SS && !PIR1bits.SSPIF);
ANDLW 0x20
BNZ 0x2ac
MOVF 0x9e,0x0,0x0
ANDLW 0x8
BZ 0x2a0
BCF 0x8a,0x1,0x0 LATBbits.LATB1 = 0;
}
MOVLB 0xf spi_message_locked = true;
MOVLW 0x1
MOVWF 0x18,0x1
MOVLB 0xf spi_message_received = true;
MOVWF 0x19,0x1
}
MOVLW 0x4 }
SUBWF 0xe1,0x0,0x0
BC 0x2c4
CLRF 0xe1,0x0
MOVF 0xe5,0x1,0x0
MOVWF 0xe1,0x0
MOVF 0xe5,0x1,0x0
MOVFF 0xfe7,0xfd9
MOVF 0xe5,0x1,0x0
MOVFF 0xfe5,0xfea
MOVFF 0xfe5,0xfe9
MOVFF 0xfe5,0xfda
RETFIE 0x1

使用 NOP:

BTFSS     0x9e,0x3,0x0      if (PIR1bits.SSPIF)
BRA 0x30e
{
MOVLB 0xf spi_rec_buffer.read_cursor = 0;
CLRF 0x4,0x1
CLRF 0x5,0x1
MOVLB 0xf spi_rec_buffer.write_cursor = 0;
CLRF 0x6,0x1
CLRF 0x7,0x1
BTG 0x89,0x4,0x0 LATAbits.LATA4 ^= 1;
NOP _asm nop nop _endasm
NOP
BTG 0x89,0x4,0x0 LATAbits.LATA4 ^= 1;
MOVF 0x80,0x0,0x0 while (!PORTAbits.NOT_SS && spi_rec_buffer.write_cursor < spi_rec_buffer.size)
ANDLW 0x20
BNZ 0x302
MOVLB 0xf
MOVF 0x7,0x0,0x1
MOVLB 0xf
XORWF 0x3,0x0,0x1
BTFSS 0xe8,0x7,0x0
BRA 0x27e
RLCF 0x3,0x0,0x1
BRA 0x28c
MOVF 0x2,0x0,0x1
MOVLB 0xf
SUBWF 0x6,0x0,0x1
MOVLB 0xf
MOVF 0x3,0x0,0x1
MOVLB 0xf
SUBWFB 0x7,0x0,0x1
BC 0x302
BRA 0x268
{
MOVLB 0xf spi_rec_buffer.data[spi_rec_buffer.write_cursor] = SSPBUF;
MOVLB 0xf
MOVF 0x0,0x0,0x1
MOVLB 0xf
ADDWF 0x6,0x0,0x1
MOVWF 0xe9,0x0
MOVLB 0xf
MOVLB 0xf
MOVF 0x1,0x0,0x1
MOVLB 0xf
ADDWFC 0x7,0x0,0x1
MOVWF 0xea,0x0
MOVFF 0xfc9,0xfef
MOVLB 0xf SSPBUF = spi_out_msg_buffer.data[spi_out_msg_buffer.read_cursor];
MOVLB 0xf
MOVF 0x10,0x0,0x1
MOVLB 0xf
ADDWF 0x14,0x0,0x1
MOVWF 0xe9,0x0
MOVLB 0xf
MOVLB 0xf
MOVF 0x11,0x0,0x1
MOVLB 0xf
ADDWFC 0x15,0x0,0x1
MOVWF 0xea,0x0
MOVF 0xef,0x0,0x0
MOVWF 0xc9,0x0
BCF 0x9e,0x3,0x0 PIR1bits.SSPIF = 0; // Interruptflag löschen...
MOVLB 0xf spi_rec_buffer.write_cursor++;
INCF 0x6,0x1,0x1
MOVLW 0x0
ADDWFC 0x7,0x1,0x1
MOVLB 0xf spi_out_msg_buffer.read_cursor++;
INCF 0x14,0x1,0x1
MOVLW 0x0
ADDWFC 0x15,0x1,0x1
MOVLB 0xf if (spi_out_msg_buffer.read_cursor == spi_out_msg_buffer.write_cursor)
MOVF 0x16,0x0,0x1
MOVLB 0xf
XORWF 0x14,0x0,0x1
BNZ 0x2ea
MOVLB 0xf
MOVF 0x17,0x0,0x1
MOVLB 0xf
XORWF 0x15,0x0,0x1
BNZ 0x2ee
BCF 0x89,0x4,0x0 LATAbits.LATA4 = 0;
BSF 0x8a,0x1,0x0 LATBbits.LATB1 = 1;
MOVF 0x80,0x0,0x0 while (!PORTAbits.NOT_SS && !PIR1bits.SSPIF);
ANDLW 0x20
BNZ 0x2fe
MOVF 0x9e,0x0,0x0
ANDLW 0x8
BNZ 0x2fe
BRA 0x2f0
BCF 0x8a,0x1,0x0 LATBbits.LATB1 = 0;
}
MOVLB 0xf spi_message_locked = true;
MOVLW 0x1
MOVWF 0x18,0x1
MOVLB 0xf spi_message_received = true;
MOVLW 0x1
MOVWF 0x19,0x1
}
MOVLW 0x4 }
SUBWF 0xe1,0x0,0x0
BC 0x318
CLRF 0xe1,0x0
MOVF 0xe5,0x1,0x0
MOVWF 0xe1,0x0
MOVF 0xe5,0x1,0x0
MOVFF 0xfe7,0xfd9
MOVF 0xe5,0x1,0x0
MOVFF 0xfe5,0xfea
MOVFF 0xfe5,0xfe9
MOVFF 0xfe5,0xfda
RETFIE 0x1

这是部分差异的屏幕截图(点击放大): Diff

最佳答案

为了让人们不必猜测,这是来自 the Microchip C18 manual 的声明(强调):

It is generally recommended to limit the use of inline assembly to a minimum. Any functions containing inline assembly will not be optimized by the compiler. To write large fragments of assembly code, use the MPASM assembler and link the modules to the C modules using the MPLINK linker.

我认为这是内联汇编的常见情况。 GCC 是个异常(exception)——它将优化内联汇编以及周围的 C 代码;为了正确地做到这一点,GCC 的内联汇编非常复杂(您必须让它知道哪些寄存器和内存被破坏了)。

关于c - 在 Microchip C18 中,为什么插入 NOP 会导致更大的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com