check if carry flag is set

后端 未结 4 2150
北海茫月
北海茫月 2020-12-18 22:25

Using inline assembler [gcc, intel, c], how to check if the carry flag is set after an operation?

相关标签:
4条回答
  • 2020-12-18 22:49

    The first function performs unsigned addition and then tests for overflow using the carry flag (CF). The volatile's must remain. Otherwise the optimizer will rearrange instructions, which pretty much ensures an incorrect result. I've seen the optimizer change the jnc to a jae (which is also based on CF).

    /* Performs r = a + b, returns 1 if the result is safe (no overflow), 0 otherwise */
    int add_u32(uint32_t a, uint32_t b, uint32_t* r)
    {
        volatile int no_carry = 1;
        volatile uint32_t result = a + b;
    
        asm volatile
        (
         "jnc 1f          ;"
         "movl $0, %[xc]  ;"
         "1:              ;"
         : [xc] "=m" (no_carry)
         );
    
        if(r)
            *r = result;
    
        return no_carry;
    }
    

    The next function is for the signed ints. Same use of volatile applies. Note that signed integer math jumps on OF flag via jno. I've seen the optimizer change this to a jnb (which is also based on OF).

    /* Performs r = a + b, returns 1 if the result is safe (no overflow), 0 otherwise */
    int add_i32(int32_t a, int32_t b, int32_t* r)
    {   
        volatile int no_overflow = 1;
        volatile int32_t result = a + b;
    
        asm volatile
        (
         "jno 1f          ;"
         "movl $0, %[xo]  ;"
         "1:              ;"
         : [xo] "=m" (no_overflow)
         );
    
        if(r)
            *r = result;
    
        return no_overflow;
    }
    

    In the big picture, you might use the functions as follows. In the same big picture, many folks will probably reject the extra work and aesthetic non-beauty until pwn'd by an overflow/wrap/underflow

    int r, a, b;
    ...
    
    if(!add_i32(a, b, &r))
        abort(); // Integer overflow!!!
    
    ...
    

    The inline GCC assembly is available in GCC 3.1 and above. See Assembler Instructions with C Expression Operands, or search for 'GCC Extended Assembly'.

    Finally, the same in Visual Studio would be as follows (not much difference in code generation), but syntax is much easier since MASM allows you to jump to a C label:

    /* Performs r = a + b, returns 1 if the result is safe (no overflow), 0 otherwise */
    int add_i32(__int32 a, __int32 b, __int32* r)
    {   
        volatile int no_overflow = 1;
        volatile __int32 result = a + b;
    
        __asm
        {
            jno NO_OVERFLOW;
            mov no_overflow, 0;
        NO_OVERFLOW:
        }
    
        if(r)
            *r = result;
    
        return no_overflow;
    }
    

    On the bad side, the above MASM code is only applicable for x86 assembly. For x64 assembly, there is no inlining so you will have to code it up in assembly (in a separate file) and use use MASM64 to compile.

    0 讨论(0)
  • 2020-12-18 22:57

    sbb %eax,%eax will store -1 in eax if the carry flag is set, 0 if it is clear. There's no need to pre-clear eax to 0; subtracting eax from itself does that for you. This technique can be very powerful since you can use the result as a bitmask to modify the results of computations in place of using conditional jumps.

    You should be aware that it is only valid to test the carry flag if it was set by arithmetic performed INSIDE the inline asm block. You can't test carry of a computation that was performed in C code because there are all sorts of ways the compiler could optimize/reorder things that would clobber the carry flag.

    0 讨论(0)
  • 2020-12-18 23:11

    However the x86 assembler hes dedicated fast ALU flag test instructions named SETcc where the cc is desired ALU flag. So you can write:

    setc    AL                           //will set AL register to 1 or clear to 0 depend on carry flag
    
    or
    
    setc    byte ptr [edx]               //will set memory byte on location edx depend on carry flag
    
    or even
    
    setc    byte ptr [CarryFlagTestByte]  //will set memory variable on location CarryFlagTestByte depend on carry flag
    

    With SETcc instruction you can test flags like carry, zero, sign, overflow or parity, some SETcc instructions allow to test two flags at once.

    EDIT: Added simple test made in Delphi to disappear a doubt about term fast

    procedure TfrmTest.ButtonTestClick(Sender: TObject);
      function GetCPUTimeStamp: int64;
      asm
        rdtsc
      end;
    var
     ii, i: int64;
    begin
      i := GetCPUTimeStamp;
      asm
        mov   ecx, 1000000
    @repeat:
        mov   al, 0
        adc   al, 0
        mov   al, 0
        adc   al, 0
        mov   al, 0
        adc   al, 0
        mov   al, 0
        adc   al, 0
        loop  @repeat
      end;
      i := GetCPUTimeStamp - i;
    
      ii := GetCPUTimeStamp;
      asm
        mov   ecx, 1000000
    @repeat:
        setc  al
        setc  al
        setc  al
        setc  al
        loop  @repeat
      end;
      ii := GetCPUTimeStamp - ii;
      caption := IntToStr(i) + '  ' +  IntToStr(ii));
    end;
    

    The loop (1M iterations) wich using instruction setc is more than 5 times faster than loop with adc instriuction.

    EDIT: Added second test which test result stored in register AL comulative in register CL to be more realistic case.

    procedure TfrmTestOtlContainers.Button1Click(Sender: TObject);
      function GetCPUTimeStamp: int64;
      asm
        rdtsc
      end;
    
    var
     ii, i: int64;
    begin
      i := GetCPUTimeStamp;
      asm
        xor   ecx, ecx
        mov   edx, $AAAAAAAA
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
        shl   edx, 1
        mov   al, 0
        adc   al, 0
        add   cl, al
    
      end;
      i := GetCPUTimeStamp - i;
    
      ii := GetCPUTimeStamp;
      asm
        xor   ecx, ecx
        mov   edx, $AAAAAAAA
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
        shl   edx, 1
        setc  al
        add   cl, al
    
      end;
      ii := GetCPUTimeStamp - ii;
      caption := IntToStr(i) + '  ' +  IntToStr(ii);
    end;
    

    Rutine part with SETcc instruction is still faster for about 20%.

    0 讨论(0)
  • 2020-12-18 23:12

    With conditional jumps jc (jump if carry) or jnc (jump if not carry).

    Or you can store the carry flag,

    ;; Intel syntax
    mov eax, 0
    adc eax, 0 ; add with carry
    
    0 讨论(0)
提交回复
热议问题