Filip Luchianenco
Filip Luchianenco

Reputation: 7012

How to get a specific bit of a binary number in 8086 assembly?

I am trying to write a program which will do binary signed floating-point multiplication/division. I need to use a specific algorithm(start with msb of the multiplier and shift the multiplicand to the right), i should not use IMUL or IDIV. What I a trying to do in a loop is:

for(i; i<sizeOfY; i++) {
 tmp = getBitOfY[i]
if (tmp==1) {
//do something
} else {
do something else 
}
}  

So, I'd like to get every bit and see if it is 1 or 0 and shift to the needed direction and then add. So how do I get the needed bit on every step? Or is my approach wrong?

Upvotes: 0

Views: 2101

Answers (1)

Deleted User
Deleted User

Reputation: 2541

As you use assembly for speed, you want a speedy bit masking too. 8086 has, to my knowledge, no barrel shifter - that means, shifting a bit for multiple positions takes time for each shift cycle, as that is done in microcode. More modern CPUs with barrel shifters can shift any number of positions with the same number of clocks. So, you may be quicker by creating a lookup table, in which the bitmasks are stored as table elements, using the bit number as index into table. For getting a bit with that specific number, you use bit number to read bit mask from table, then you mask the value you're testing against with that.

If it's not for speed that you're isolating bits, then don't bother using assembly for that. Running a loop you could do just as well with a script language :)

Oh, I should mention that 8086 SHL instruction comes in a variant which allows shifting by number of positions which is specified in the CL register.

But - for your purpose you don't need to extract bits this way - it seems to be enough to simply shift them out one by one, no masking involved. The title of your question "How to get a specific bit of a binary number in 8086 assembly?" suggests that you're asking something else than you actually want to know: Simply catch the bits when they're shifted out of the register. How to catch them, you can take from the effects the SHR instruction has on the condition code register. Look which flags are affected, and how they are affected.

There is no code here because those artificial contraints you're exposed to suggest that it is expected from you to solve the actual implementation yourself.

BTW, this; "start with msb of the multiplier and shift the multiplicand to the right" makes only partly sense. Starting with LSB you want to shift to the right. When starting with MSB you shift left, in order for those bits dropping out of the register when shifted.

Upvotes: 2

Related Questions