title: MIPS - Convert Word to Byte
body: <p>I have (what I think) is a simple question.</p>
  
  <p>I have a value in a register in MIPS and I want to convert it from a word to a byte.</p>
  
  <p>So, for example:
  I have the value 123456 in register $t0</p>
  
  <p>The actual value will be:
  00000000 00000001 11100010 01000000</p>
  
  <p>I want to then put the value of the rightmost byte of $t0 (01000000) into $t1. This would be the value 64.</p>
  
  <p>I want to do this without going to memory. I <em>could</em> store it as a byte to the stack and load it back, but that seems unnecessary.</p>
  
  <p>My general idea is to shift the bits to the left, then to the right, to "remove" all of the bits from positions 31...8. But that has the glaring incapability of not negating numbers when it should.</p>
  
  <p>Here is an example of how close I have gotten:</p>
  
  <pre><code>.text
  main:
  
  # START PROLOGUE ####################################
  # Ignore all of this, irrelevent and I know the stack is too big
  la $sp, -8($sp) # allocate space for old $fp and $ra
  sw $fp, 4($sp) # save old $fp
  sw $ra, 0($sp) # save return address
  la $fp, 0($sp) # set up frame pointer
  la $sp, -120($sp) # allocate stack frame: 120 = space for locals, temps in bytes
  # END PROLOGUE ####################################
  
  
  li $a0, 123456
  li $v0, 1
  syscall #would print out 123456, as it should
  
  # Now say I want to load only a 'byte' of $a0 (convert a word to a byte)
  
  sll $a0, $a0, 24
  srl $a0, $a0, 24
  syscall # Will print out 64, as it should
  
  # However!
  
  li $a0, 194
  syscall # Prints out 194 as it should
  
  sll $a0, $a0, 24
  srl $a0, $a0, 24
  syscall # Still prints out 194, but I want it to be -62 (signed)
  
  # START EPILOGUE ####################################
  la $sp, 0($fp) # (deallocate locals)
  lw $ra, 0($sp) # (restore return address)
  lw $fp, 4($sp) # (restore frame pointer)
  la $sp, 8($sp) # (restore stack pointer)
  jr $ra # return
  # END EPILOGUE ####################################
  </code></pre>
  
  <p>I can think of hack-y ways to fix it after the fact, but is there a better way that I should be doing this?</p>