bt assembly instruction

tina nyaa picture tina nyaa · Sep 22, 2011 · Viewed 9.1k times · Source

I have quesetion about bt assembly instruction. I have excerpted part of book to provide context. Please see last example, bt Testme, bx. Why does that copy TestMe+8? Shouldn't it copy TestMe+65?

Very much thank you for help!

6.6.4.2 The Bit Test Instructions: BT, BTS, BTR, and BTC

On an 80386 or later processor, you can use the bt instruction (bit test) to test a single bit. Its second operand specifies the bit index into the first operand. Bt copies the addressed bit into the carry flag. For example, the instruction

  bt  ax, 12

copies bit twelve of ax into the carry flag.

The bt/bts/btr/btc instructions only deal with 16 or 32 bit operands. This is not a limitation of the instruction. After all, if you want to test bit three of the al register, you can just as easily test bit three of the ax register. On the other hand, if the index is larger than the size of a register operand, the result is undefined.

If the first operand is a memory location, the bt instruction tests the bit at the given offset in memory, regardless the value of the index. For example, if bx contains 65 then

  bt  TestMe, bx

will copy bit one of location TestMe+8 into the carry flag. Once again, the size of the operand does not matter. For all intents and purposes, the memory operand is a byte and you can test any bit after that byte with an appropriate index. The actual bit bt tests is at bit position index mod 8 and at memory offset effective address + index/8.

Answer

Ray Toal picture Ray Toal · Sep 22, 2011

When the book says "bit one of location TestMe+8", the "8" refers to an address offset, which is measured in bytes. There are 64 bits in 8 bytes, so the 65th bit is bit one of 8 bytes past TestMe.

  • The byte at TestMe has bits 7..0
  • The byte at TestMe+1 has bits 15..8
  • The byte at TestMe+2 has bits 23..16
  • ...
  • The byte at TestMe+8 has bits 71..64

So "65" refers to "bit 1" (the second counting from the right) of the byte at address TestMe+8.