Check number of bits on in ax [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-11 17:31:09

问题


i need to find a way to check number of bits that is current on (1) in the ax register. i was thinking to use in shl or shr, but i do not know how exactly i suppose to use them. this is my code so far:

org 100h

mov ax, 0xffffh
;shr ax TO DO

int 16h
ret

i red in some place that the shift right function can move the bits to the right. so i might should use it by try to "push" the 16 bits to the right every time, and then check the LSB 1 or 0 flag? it sound to me as good idea.


回答1:


Use the popcnt instruction:

popcnt bx,ax

This sets bx to the number of bits set in ax. You need a somewhat recent CPU for this instruction to be available though.




回答2:


The shifted bit goes in the CF(carry flag) so one solution is that everytime after each shift you check that if CF=1 then increment a count variable that will tell the num of ON bits at the end.



来源:https://stackoverflow.com/questions/47161950/check-number-of-bits-on-in-ax

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