title: Extract bits of a register starting with the most significant bit, or a high bit
body: <p>EDIT:</p>
  
  <p>I didn't expect this question to gain traction so quickly. Following from the answers I've already received it seems I may have left out an important piece of information. The pattern is <strong>not a fixed amount of bits</strong>. Some letters might have more or fewer bits. I.e, B has 5 bits, but C might use up to 6 bits, but none use more than a byte. I've included an example of the 'A' bit pattern in my question which uses 7 bits for each line. Also see edit at the bottom of the question.</p>
  
  <p>I'm new to assembly. I have bit pattern which corresponds to a text representation of a letter. Each 1 represents a $ (or any symbol) and each 0 represents a blank space. I.e:</p>
  
  <pre><code>    $$$$            11110
      $   $           10001
      $   $           10001
      $$$$            11110
      $   $           10001
      $   $           10001
      $$$$            11110
  
     $            0001000  
    $ $           0010100
   $$$$$          0111110  
  $     $         1000001
  </code></pre>
  
  <p>I've written an assembly language program which reads each pattern and prints the right symbol depending on if it reads a 1 or a 0. To determine if it is a 1 or a 0, I AND the register with 1 and then right shift the bits an amount of times equaling the amount of bits in each line and then compare the result:</p>
  
  <p>Notice that the bits for each row are stored at the bottom of a separate 2-byte word, which I load into an 8-bit register.</p>
  
  <pre><code>patternb:   dw 011110b,010001b,010001b,011110b,010001b,010001b,011110b
  
  rowloop:
      mov bl,[patternb+si]    ;iterate through each element in binary array
  
      patternloop:    
          mov bh,bl   ;move bit pattern into register so that we can change it
          and bh,1    ;AND register to find set bits and store back in register
          shr bl,1    ;SHIFT original bit pettern right
          cmp bh,1    ;check if bit is set or not (1=set, else 0)
          je writesym ;if set, write symbol
          jne writeblank  ;if not set, write space
  </code></pre>
  
  <p>The problem is the way that AND works. Obviously it starts from the least significant bit and prints as the bits are shifted right, but this causes the problem that it prints the letter in "reverse" order. I.e:</p>
  
  <pre><code> ####
  #   #
  #   #
   ####
  #   #
  #   #
   ####
  </code></pre>
  
  <p>I've tried a few bit manipulations but none seem to work. I've also tried shifting and rotating the bit pattern to correspond to the correct printing but this won't work for each line as not each line needs to be manipulated in this way. (Line 2 for example will print correctly without needing to be manipulated first). I have this same bit pattern technique for each letter from A-E.</p>
  
  <p><strong>Ideally I'd want it to start comparing from the most significant bit somehow which should then print it in the correct order</strong>, but I'm not sure on how to go about manipulating the bits in order to achieve that.</p>
  
  <p>EDIT:
  Following Peter Duniho's answer, I'd like to post some things I have tried:
  I've tried AND'ing the pattern with 10000b and then ROL the result to get an answer of <code>00001b</code> and then shift the bits left. Then compare the result to see which symbol should be printed. This doesn't work either but because the bit pattern isn't always fixed it wouldn't be a solution anyway.</p>
  
  <pre><code>    mov bh,bl   ;move bit pattern into register so that we can change it
      and bh,10000b ;AND register to find set bits and store back in register
      rol bh,1    ;rol result to obtain 00001b
      shl bl,1    ;SHIFT original bit pettern right
      cmp bh,1    ;check if bit is set or not (1=set, else 0)
      je writesym ;if set, write symbol
      jne writeblank  ;if not set, write space
  </code></pre>
  
  <p>The closest I've come right now to solving this problem (with some help from Peter Duniho's answer as guidance) is to store my bit array as full 8 bit form (i.e. <code>011110000b</code>, etc instead of <code>011110b</code> otherwise the assembler stores it as <code>00011101</code> implicitly as mentioned in Martin Rosenau's answer, which we don't want) AND it with the full <code>10000000b</code> (since we're using at most 8 bits and this lets us check the MSB) instead of the 1 (<code>000000001b</code>) as I was trying to do before and then using the ROL and compare approach above (Or just compare it to <code>10000000b</code>). The loop runs a total of 7 times (due to each letter having 7 lines/bit patterns, except A which has 4 so A does not print right, but this is a problem I can solve myself with some conditions. The program works and prints correctly now. This is the code I've used:</p>
  
  <pre><code>        mov bh,bl   ;move bit pattern into register so that we can change it
          and bh,10000000b   ;AND register to find set bits and store back in register
          rol bh,1    ;shift MSB to LSB to compare (or could just compare with 10000000b instead)
          shl bl,1    ;SHIFT original bit pettern left
          cmp bh,1    ;check if bit is set or not (or use cmp bh,10000000b and omit rol above)
          je writesym ;if set, write symbol
          jne writeblank  ;if not set, write space
  </code></pre>
  
  <p>I've marked Peter's solution as the answer as it was the one which pointed me in the right direction to solve this. But as he mentioned there are a great number of ways to solve this problem (as indicated by the differing solutions posted) but his just happened to be the easiest for me to implement for my own code which is what he was going for. </p>
  
  <p>Martin Rosenau's answer was also insightful especially the optimizations. I'll try to implement those when I have some more time and then update the solution above.</p>