NO

Author Topic: Optimization bug with inline asm  (Read 2992 times)

akko

  • Guest
Optimization bug with inline asm
« on: June 19, 2012, 06:23:12 PM »
The attached code works only with compiler optimization disabled.

The disassembled code shows that the first asm sections in function sel(i) become ignored by the compiler.

Offline Vortex

  • Member
  • *
  • Posts: 797
    • http://www.vortex.masmcode.com
Re: Optimization bug with inline asm
« Reply #1 on: June 19, 2012, 09:02:39 PM »
Yes, it appears there is a problem. Compiled with Maximize speed more :

Code: [Select]
_text   SEGMENT PARA PUBLIC 'CODE'                      ; section number 2

_sel    PROC NEAR
        push    ebp                                     ; 0000 _ 55
        mov     ebp, esp                                ; 0001 _ 89. E5
        mov     eax, dword ptr [ebp+8H]                 ; 0003 _ 8B. 45, 08
        cmp     eax, 12                                 ; 0006 _ 83. F8, 0C
        jz      @12                                     ; 0009 _ 74, 0F
        cmp     eax, 13                                 ; 000B _ 83. F8, 0D
        jz      @12                                     ; 000E _ 74, 0A
        cmp     eax, 12                                 ; 0010 _ 83. F8, 0C
        jl      @9                                      ; 0013 _ 7C, 13
        cmp     eax, 23                                 ; 0015 _ 83. F8, 17
        jnz     @9                                      ; 0018 _ 75, 0E

@12     LABEL NEAR
        mov     eax, offset _strg                       ; 001A _ B8, 00000000(d)
        mov     ch, byte ptr [eax+1H]                   ; 001F _ 8A. 68, 01
        xchg    byte ptr [eax+2H], ch                   ; 0022 _ 86. 68, 02
        mov     byte ptr [eax+1H], ch                   ; 0025 _ 88. 68, 01

@9      LABEL NEAR
        push    offset _strg                            ; 0028 _ 68, 00000000(d)
        call    _puts                                   ; 002D _ E8, 00000000(rel)
        pop     ecx                                     ; 0032 _ 59
        mov     esp, ebp                                ; 0033 _ 89. EC
        pop     ebp                                     ; 0035 _ 5D
        ret                                             ; 0036 _ C3
_sel    ENDP
Code it... That's all...

akko

  • Guest
Re: Optimization bug with inline asm
« Reply #2 on: June 20, 2012, 05:34:20 PM »
Just checked. Bug also in RC4.

Thanks & regards
Andreas

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Optimization bug with inline asm
« Reply #3 on: July 08, 2012, 01:48:41 PM »
Mixing C and inline assembly code is almost always a bad idea; It will usually lead to the slowest and most bloated code. Using __declspec(naked) and writing the entire function body is better, but the best is to use a separate assembler.

That said, the compiler should not silently generate the wrong code. Either report an error or do the right thing. I will look at it...
/Pelle