title: How to check if register is empty in MIPS assembly?
body: <p>So I am writing a tree traversal and my code breaks upon comparing an empty $a0 register with $zero.</p>
  
  <p>I don't know why, but I imagined that since null is just 0, comparing it to zero should not give me an error of</p>
  
  <p>"Attempt to use non-existent memory"
   By the way, I am traversing recursively.</p>
  
  <p>Here is my code snippet</p>
  
  <pre><code>        addi    $sp, $sp, -16
          sw      $ra, 12($sp)
          sw      $s0, 8($sp)     # $a0
          sw      $s1, 4($sp)     # $a1
          sw      $s2, 0($sp)     # $a2
  
  pre:
          beq     $a0, $zero, done        # If null done
          move    $s0, $a0                # Preserve $a0
          jalr    $a1                     # Print
          lw      $a0, 4($s0)             # Left Node
          jal     traverse_tree           # Recursive call
          lw      $a0, 8($s0)             # Right Node
          jal     pre                     # Recursive call
          j       done                    # Done
  
  done:
          lw      $ra, 12($sp)
          lw      $s0, 8($sp)
          lw      $s1, 4($sp)
          lw      $s2, 0($sp)
          addi    $sp, $sp, 16
          jr      $ra
  </code></pre>