counting number of zeros and ones in a byte

后端 未结 2 878
死守一世寂寞
死守一世寂寞 2021-01-28 03:41

I previously posted a program to find the total number of 1s in a byte. Now I am trying to find the number of 0s in a byte. Following is my code:

MOV AL,1
MOV CX         


        
2条回答
  •  一整个雨季
    2021-01-28 03:58

    Often when you write in assembly, it's fun to look at possible optimizations. Using a loop, you use the Z and C flags like so:

        MOV AL, 
        MOV BL, 8
        CLC
    Loop:
        SBB BL, 0
        SHR AL, 1
        JNZ Loop
    Done:
        SBB BL, 0
        ; result is in BL
        HLT
    

    A faster way on older processors is to have a 256 bytes table and do a look up. As mentioned by vitsoft, on modern processors, use the POPCNT instruction is probably the fastest (it takes one clock cycle to counts all the bits of a 64 bit register in hardware.)

    Now, if you need to know the exact timing, my loop is not practical because it will vary depending on AL. Another way to make it go fast is to unroll the loop:

        MOV AL, 
        MOV BL, 8
        SHR AL, 1    ; 1
        SBB BL, 0
        SHR AL, 1    ; 2
        SBB BL, 0
        SHR AL, 1    ; 3
        SBB BL, 0
        SHR AL, 1    ; 4
        SBB BL, 0
        SHR AL, 1    ; 5
        SBB BL, 0
        SHR AL, 1    ; 6
        SBB BL, 0
        SHR AL, 1    ; 7
        SBB BL, 0
        SHR AL, 1    ; 8
        SBB BL, 0
        HLT
    

    This is practical because you have zero branches. Modern processors love that much (although in this case since we're dealing with just 2 registers, I don't think it's a huge advantage.)

提交回复
热议问题