How to calculate parity of a 32bits number in assembly?

半腔热情 提交于 2021-02-08 12:16:37

问题


I need to calculate the parity of a 32 bits number and return it. To do this I must use XOR, SHR,SETP(or similar) and movzx. Somebody have an idea ?


回答1:


Many ways to do this, of course. If you decide to use the hardware parity flag, keep in mind that it only checks the low 8 bits. A solution using this approach might be to process your 32 bit input as four 8 bit values, and use XOR on the partial results:

p(31:0) = p(31:24) ^ p(23:16) ^ p(15:8) ^ p(7:0)

To get the 8 bit units, you can use SHR and to get the partial values you can XOR, use the SETP. This should be enough to get you started.

Another, possibly more efficient, option is to XOR the bytes and grab the parity of them:

p(31:0) = p(31:24 ^ 23:16 ^ 15:8 ^ 7:0)




回答2:


Use this code:

    mov ebx, 1234 ;Replace this number with the value of which you want to know the parity
    mov cx, 4
a:
    or bl,  bl
    lahf
    ror eax, 8
    ror ebx, 8
    loop a
    xor ah, al
    ror eax, 16
    xor al, ah
    rol eax, 8
    xor ah, al
    and ah, 4
    sahf ;now the parity flag is defined


来源:https://stackoverflow.com/questions/27040875/how-to-calculate-parity-of-a-32bits-number-in-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!