title: unshifted register required - Assembler throws error on the TST instruction
body: <p>I am currently rewriting an algorithm from C to arm assembly (ARM Cortex M4 CPU).</p>
  <p><strong>What does my code do?</strong></p>
  <p>This algorithm takes an 8-bit number as input and starting from the right tells us what is the first bit that’s 0.
  Here are a few examples:</p>
  <p>Input: B01111111    Output:7</p>
  <p>Input: B01110111    Output:3</p>
  <p>Input: B11111110    Output:0</p>
  <p>Here is the original C code that accomplished this:</p>
  <pre><code>uint8_t find_empty(uint32_t input_word)
  {
    for (uint8_t searches=7; searches&gt;=0; searches--)
    {
      if ((input_word&amp;1)==0)
      {
        return 7-searches;
      }
      
      input_word=input_word&gt;&gt;1;
    }
    return 255;
  }
  </code></pre>
  <p>And here is my beginner attempt at rewriting this in ARM (Cortex M4) assembly.</p>
  <pre><code>.global findEmpty
  findEmpty:
      mov r1, r0 //Move input_word to r1
      
      //Config
      mov r0, #7 //search through 8 (7+1) bits. &lt;-searches
  
      FindLoop:
        tst r1, #1 //ANDs input_word with 1, sets the Z flag accordingly.
        
        beq NotFoundYet //didn't get a 0, jump forward
          rsb r0, r0, #7 //searches=7-searches &lt;- which bit is 0? 
          bx lr //Return found bit number
        
        NotFoundYet:
        lsr r1, r1, #1 //input_word=input_word&gt;&gt;1
  
        sub r0, r0, #1 //Decrement searches
        cmp r0, #0
        bpl FindLoop //If searches&gt;=0, do the loop again. 
      mov r0, #255 //We didn't find anything. Return 255 to signal that
      bx lr
  </code></pre>
  <p><strong>Quick note:</strong>
  I used r1 as a variable here, which I heard you are not supposed to do as the compiler (I am linking my assembly “.S” file to a C file with gcc) uses r0-r3 to pass data to and receive data from functions. However, because of that it doesn’t use these registers for important things, so I don’t have to deal with pushing stuff to the stack, which saves cycles.</p>
  <p><strong>What’s the problem?</strong></p>
  <p>When I try to compile my project gcc gives me an assembler error on the TST line:</p>
  <p><em>Assembler messages:</em>
  <em>Error: unshifted register required -- `tst r1, #1’</em></p>
  <p>This is very confusing to me, as I’ve looked at the keil site for both the <a href="https://www.keil.com/support/man/docs/armasm/armasm_dom1361289913099.htm" rel="nofollow noreferrer">TST instruction</a> and the <a href="https://www.keil.com/support/man/docs/armasm/armasm_dom1361289876525.htm" rel="nofollow noreferrer">LSR instruction</a> which I am using later to shift r1 by 1. Yet none of them say anything about not being able to work together. I’ve looked online for other discussions on this topic. I came across <a href="https://groups.google.com/g/android-platform/c/7jOP7xaZoRk?pli=1" rel="nofollow noreferrer">this discussion</a> where people were saying to tell the compiler to compile in ARM mode, but my code already is running in ARM mode, not Thumb. I confirmed this by making another .global subroutine and trying to add an immediate over 7 to a number, and sure enough it doesn’t work, like it shouldn’t if the CPU is in ARM mode.</p>
  <pre><code>.global illegal_add
  illegal_add:
      add r0, r0, #20
      bx lr
  </code></pre>
  <p>I know very little and am out of ideas how to try and tackle this issue. If anybody has any ideas with things to try, please let me know.
  Thank You for the help.</p>