How cmp assembly instruction sets flags (X86_64 GNU Linux)

前端 未结 2 1889
余生分开走
余生分开走 2021-01-12 12:56

Here is a simple C program:

void main()
{
       unsigned char number1 = 4;
       unsigned char number2 = 5;

       if (number1 < number2)
       {
             


        
2条回答
  •  囚心锁ツ
    2021-01-12 13:37

    I think i understand it now. This is how i think it goes (borrow flag is set)

    4 - 5
    
    1st operand = 4 = 0000 0100
    2nd operand = 5 = 0000 0101
    
    So we have to perform
    
          1st operand
        - 2nd operand
        --------------
    
    
          7654 3210 <-- Bit number
          0000 0100
        - 0000 0101
        ------------
    
    Lets start.
    
    Bit 0 of 1st operand = 0
    Bit 0 of 2nd operand = 1
    
    so
    
      0
    - 1 
     ===
      ?
    

    to do this,

    let's borrow a 1 from left side of bit 0 of 1st operand.

    so we see bit 2 of 1st operand is 1.

    when bit 2 is = 1, it means 4.

    we know that we can write 4 as 2 + 2. So we can write 4 as two 2s.

          7654 3210 <-- Bit number
                 1
                 1         
          0000 0000
        - 0000 0101
        ------------
    

    So in above step, we have written bit 4 of 1st operand as two 2s (two 1 on top of bit 2 of 1st operand.)

    Now again as we know, a 2 can be written as two 1s. So we borrow one 1 from bit 1 of 1st operand and write two 1s on bit 0 of 1st operand.

          7654 3210 <-- Bit number
                  1
                 11         
          0000 0000
        - 0000 0101
        ------------
    

    Now we are ready to perform subtraction on bit 0 and bit 1.

          7654 3210 <-- Bit number
                  1
                 11         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    So after solving bit 0 and bit 1, lets see bit 2.

    We again see same problem.

    Bit 2 of 1st operand = 0

    Bit 2 of 2nd operand = 1

    to do this, let's borrow a 1 from left side of bit 2 of 1st operand.

        8 7654 3210 <-- Bit number
                  1
                 11         
        1 0000 0000
        - 0000 0101
        ------------
                 11
    

    Now you see, bit 8 of 1st operand is 1. We have borrowed this 1.

    At this stage, carry flag will be set. So CF=1.

    Now, if bit 8 is 1, it means 256.

    256 = 128 + 128

    if bit 7 is 1, it means 128. We can rewrite as

        8 7654 3210 <-- Bit number
          1       1
          1      11         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    As previously, we can re-write it as:

        8 7654 3210 <-- Bit number
           1      1
          11     11         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    As previously, we can re-write it as:

        8 7654 3210 <-- Bit number
            1     1
          111    11         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    As previously, we can re-write it as:

        8 7654 3210 <-- Bit number
             1    1
          1111   11         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    As previously, we can re-write it as:

        8 7654 3210 <-- Bit number
               1  1
          1111 1 11         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    As previously, we can re-write it as:

        8 7654 3210 <-- Bit number
                1 1
          1111 1111         
          0000 0000
        - 0000 0101
        ------------
                 11
    

    At last we can solve this.

    Subtracting 2nd operand from all above it will give

        8 7654 3210 <-- Bit number
                1 1
          1111 1111         
          0000 0000
        - 0000 0101
        ------------
          1111 1111
    
    
    So result = 1111 1111
    

    Notice, sign bit in result = bit 7 = 1

    so sign flag will be set. i.e SF=1

    And therefore SF=1, CF=1 after 4 - 5

提交回复
热议问题