In: Computer Science
In one instruction scan register BX from left to right for the position of the first 1 bit and
put the result (the position of the first bit 1) in register CX (only one instruction).
{hint: use from the instructions bsf, bsr, bt, btc, btr}
Answer: BSR CX,BX
Explanation: Since we need to scan the BX register from left to right for the position of the first 1(high) bit and put the result (the position of the first bit 1) in register CX as well, the most appropriate instruction for this process will be the BSR instruction.
This is because the BSR instruction stands for Bit Scan Reverse. The BSR instruction is one of the bits scanning instructions that work on a register and the purpose of BSR instruction is to scan a specific register in reverse manner that is from left to right (from 16th bit towards 0th bit) for the first high bit (1) and also put the position of the first 1 bit into an another register after scanning.
Note: 'Put the position of the bit' here means to simply copy the position of the first 1 bit into an another register.
The syntax for BSR instruction is:
BSR destination register, scan register
In this instruction, the first operand 'destination register' is the register into which we want to put the position of the first high (1) bit, and the second operand 'scan register' is the register that we want to scan for finding the position of the first 1 bit.
So now, as we are required to scan the BX register from left to right (means starting from 16th bit towards 0th bit) and put the position of first 1 bit into the CX register, it means that the BSR instruction will do this required job. The BX register is a 16-bit register with lower 8 bits (0-7) in the BL register and the upper 8 bits (8-15) in the BH register. So, the BSR instruction will scan the BX register from left to right, starting from 16th bit towards the 0th bit.
So, the assembly instruction to scan the BX register from left to right for the position of the first 1(high) bit and put the result (the position of the first bit 1) in register CX is:
BSR CX,BX
Thanks!