title: Invalid PC value: 0x00000000 in MIPS
body: <p>I'm writing a function in MIPS, </p>
  
  <pre><code>   void decodeNull(char[] text, char[] output, int outputLength, int[] pattern)
  </code></pre>
  
  <p>A piece of text $a0 contains a cipher determined by an array of integers. If the index is 1, you read in the first bit of the first word, convert it to uppercase, and add it to the starting address of the output in $a1. If the index is 2, you read in the second letter of the word and add it to the output...etc. If the index is 0, you skip the word as a whole,and go to the next index. If the index is -1, you have reached the end of the string.</p>
  
  <p>For example:</p>
  
  <p>pattern ($a3): [4, 2, 0, 2, -1]</p>
  
  <p>input text ($a0): Hel<strong>l</strong>o m<strong>y</strong> name i<strong>s</strong></p>
  
  <p>output ($a1): LYS</p>
  
  <p>I've stepped through my algorithm many times to completion, and all the values seem to be correct. But when I call jr $ra, I get the error: Invalid PC Value 0x00000000. I'm unsure what this error means, and I don't know where to look in my code. </p>
  
  <p>I think it might have something to do with how I'm storing my output.
  When I pass the character I need into my helper function toUpper, I store the return value ($v0) into $a1, which is supposed to be the starting address of the output. I then move to the next bit in the output address, and then move to the next word.</p>
  
  <pre><code>addToOutput:
    move $a0, $t7 #t7 contains the bit to be stored in the output
    jal toUpper 
    sb $v0, 0($a1) #add return value to a1
    addi $a1, $a1, 1
    j goToNextWord 
  </code></pre>
  
  <p>Perhaps $a1 points to the end of the output address, which would be 0. If so, how can I move it back to the start? I tried saving the original address of the output to $s1 (move $s1, $a1), then calling lw $a1, 0($s0) before jr $ra, but had no success.</p>
  
  <p>I think it could also be a problem with my $sp. I start my function with 
      decodeNull:
      #(char[] text, char[] output, int outputLen, int[] pattern)
      #text = $a0, output = $a1, outputLen = $a2, pattern = $a3
      addi $sp, $sp, -20
      sw $ra, 0($sp)
      sw $a0, -4($sp) #text (string)
      sw $a1, -8($sp) #output (string)
      sw $a2, -12($sp) #outputLen (int)
      sw $a3, -16($sp) #pattern (int[])</p>
  
  <pre><code>move $s1, $a1 #ADDRESS OF OUTPUT IN S1
  move $s3, $a3 #ORIGINAL ADDRESS OF PATTERN IN S3
  move $s0, $a0 #ORIGINAL ADDRESS OF TEXT IN S0
  </code></pre>
  
  <p>and end it with </p>
  
  <pre><code>end:
  
  addi $sp, $sp, 20
  lw $ra, 0($sp) #text (string)
  lw $a0, 4($sp) #output (string)
  lw $a1, 8($sp) #outputLen (int)
  lw $a2, 12($sp) #pattern (int[])
  lw $a3, 16($sp)
  jr $ra
  </code></pre>
  
  <p>I think this is correct but I'm not sure. I think if I understood what this error means I can fix the problem, but I don't know where to look in my code.</p>
  
  <p>Please help!</p>