title: MIPS Using Bit Shift Operators to Print a Decimal in Binary
body: <p>I've read numerous threads here and elsewhere online concerning this topic.  Great topics regarding bit shifts (not necessarily pertaining to Assembly but the topic in general are: <a href="https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work">What are bitwise shift (bit-shift) operators and how do they work?</a>   I've gone as far as copying and pasting the code from the OP here: <a href="https://stackoverflow.com/questions/14622371/how-do-i-print-a-binary-number-with-an-inputed-integer">How do I print a binary number with an inputed integer?</a> and making the changes that the replier had suggested and I continue to produce a string of zero's no matter what I do.</p>
  
  <p>I understand what bit shifting is and how it works.  Shifting to the right by 'n' divides the number by 2^n and shifting left multiplies the number by 2^n.  </p>
  
  <p>I had a lab that was due this past week and the third portion of it was to provide a program that would take user input, print out the binary version of it and then the hexidecimal version.  Once that was done, the program was then to print out certain bits in the center of the string and produce its binary and hexidecimal version of that as well. </p>
  
  <p>My original thoughts were to take the string, AND it with 0x01, print the resulting '1' or '0', and then bit shift it right by '1'.  This was contained in a loop and would continue until my counter met the '32' requirement and be done with it.  But I'm extremely confused as to why it prints all '0's.  I've tried other versions such as:</p>
  
  <ul>
  <li><p>shift the user input left by 31, then do the loop for each bit but inside the loop it was shifting to the right (to make up for the reversed order of bits) - failed - all zero's again</p></li>
  <li><p>do the opposite from above - failed again, all zero's</p></li>
  </ul>
  
  <p>I know in my head what I want to do it something like this:</p>
  
  <pre><code>(NOT CODE OBVIOUSLY)
  User input is: 25 &lt;-- store at $t0 
      Binary rep is: 0000 0000 0000 0000 0000 0000 0001 1001 # 25 in $t0
  
  LOOP:
      AND with 0x01: 0000 0000 0000 0000 0000 0000 0000 0001 #saved to $t1
  
      Produces:      0000 0000 0000 0000 0000 0000 0000 0001 #saved to $t2
  
      Print to Console: 1 #send contents of $t2 to syscall
  
      Shift $t0 Right 1: 0000 0000 0000 0000 0000 0000 0000 1100 #
  
  j LOOP (until the beq branch was met and I left the loop)
  </code></pre>
  
  <p>I would have thought that this would have worked.  Even if it produced the result backwards, I thought I would have still received '1's in my produced string that I would have noticed and dealt with accordingly (shift the entire number left in <code>$t0</code> by '31' and then performed the instructions above.  Still all '0's...  </p>
  
  <p>Here is my code.  Now again, it has become corrupted through my numerous attempts and changes.  I'm just asking for some help in understanding if my approach highlighted above is completely off-base and what I can do to the sample code below to fix this.  </p>
  
  <p>Understand that this assignment has been turned in and I did not complete it because of my confusion.  I'm looking to understand this further.  I didn't complete the entire code because I was stumped on this part.  So I'll just ask for the first part of this assignment.  </p>
  
  <pre><code>#Read integer A from user and store it into a register
  #Display the integer in binary
  #Display the integer in Hex
  #set Register $a0 to contain only bits 12,13,14,15 of $a0
  #Display the integer in binary contained in $a0
  #Display the integer in hex contained in $a0
  
  .data
  
      userInput: .asciiz "Please enter your integer:\n"
      binaryInput: .asciiz "Here is the input in binary: "
      nl: .asciiz "\n\n"
      hexInput: .asciiz "Here is the input in hexidecmal: "
      binaryOutput: .asciiz "Here is the output in binary: "
      hexOutput: .asciiz "Here is the output in hexidecimal: "
  
  .text
      main:
  
      #ask end-user to submit an integer value
      li $v0, 4
      la $a0, userInput
      syscall
  
      #read user-input
      li $v0, 5
      syscall
  
      #enter user input into $t0
      move $t0, $v0
      add $t1, $zero, $zero       #counter
      addi $t2, $zero, 32     #target
      sll $s1, $t0, 31        #shift left number 31 bits to s1
      li $v0, 4
      la $a0, binaryInput     #print out string to user
      syscall
  
  
  loop:
      andi $s2, $s1, 1        #and 0x01 with s1 to s2
      srl $s1, $s1, 1         #shift right s1 by 1 bit
      li $v0, 1
      la $a0, ($s2)           #print digit held in s2 to screen
      syscall
      add $t1, $t1, 1         #add 1 to counter
      bne $t1, $t2, loop      #check if counter is equal to target, if not continue loop
  
      #exit the program 
      li $v0, 10
      syscall
  </code></pre>