I also had to program this bit for university (for 8 bits). Here is how I did it:
MOV AL, 10001011B ;set the value to test MOV CL, 7 MOV DH, 1 MOV DL, 0 loop1: PUSH AX AND AL, DH PUSH CX MOV CL, DL SHR AL, CL POP CX MOV BH, AL SHL BH,CL OR CH,BH DEC CL INC DL SHL DH, 1 POP AX CMP DL, 8 JE END JMP LOOP1 END:
I have not commented on this, here is how it works: DH is 1 , which moves in byte as the first time: 00000001 ; second time 00000010 and so on. When you do AND with AL, you get 0 or something like 100 or 10000 , you need to shift it to the right to get it as 0 or 1 . Then put it in BH and go to the desired position, which is 7 for byte 0 , 6 for byte 1 and so on. Then OR to our final result and INC and DEC , which is necessary. Do not forget the conditional jumps and pop AX for the next cycle :)
The result will be in CH.
Laurent meyer
source share