invalid program counter value: 0

29,577

Solution 1

You probably finish your program with a jr $ra (return to caller). However, the code executed by MARS doesn't have a caller - it's executed at startup and has no function to return to, so the contents of $ra are zero.

The correct way to end a program on MARS is using the "exit" syscall:

    li $v0, 10
    syscall

Solution 2

I am new to MIPS and I just had this problem. This is what I had:

    .data

    .text

swap:
    # do stuff
    jr  $ra

main:
    # do stuff
    jal swap
    li  $v0,10
    syscall

I fixed it by changing it to this:

    .data

    .text
main:
    # do stuff
    jal swap
    li  $v0,10
    syscall

swap:
    # do stuff
    jr  $ra

Notice I moved main to be before swap. I mistakenly assumed that main was a reserved label. And that it would automatically jump straight to main first. But apparently that isn't the case because it was hitting my jr $ra instruction before I got to call jal swap in main.

Anyway, I hope this helps.

Solution 3

I know this question is old, but for anyone who was just like me and was desperately Googling for an answer: try doing the above syscall thing instead of a return, and try putting your main function before all other labels. Also, there's a "Initialize Program Counter to global "main" if defined" under the Settings menu; make sure that is checked. I do not know if enabling that allows you to put your main label after other labels, as I haven't tried that yet. What I have described is what I did to make it work, and nothing else. Good luck!

Share:
29,577
Krewie
Author by

Krewie

Updated on July 09, 2022

Comments

  • Krewie
    Krewie almost 2 years

    I'm currently working with assembly language under the MIPS processor. I'm currently using MARS simulator and for reasons unknown I get the following error message after every run:

    Go: running try.s

    Error in : invalid program counter value: 0

    Go: execution terminated with errors.

    I get this error message independent of the code I'm using, just wondering if this is a bug in the MARS simulator or if it's something I'm missing.

    • Krewie
      Krewie almost 14 years
      It appears as if MARS resets some of the registers (like $ra) upon call of jr $31 at the end of the file. I'm currently using breakpoints at the end of the file but shouldn't there be another way ?
    • JUST MY correct OPINION
      JUST MY correct OPINION almost 14 years
      From the MARS home page: "Send MARS questions and comments to Dr. Pete Sanderson at [email protected] or Dr. Ken Vollmar at [email protected]." That's probably your best call for something as specific as this. Failing that, can you put up some more information like, say, the source code that's failing (simplest case) and what you're doing with it?
    • JUST MY correct OPINION
      JUST MY correct OPINION almost 14 years
      I just downloaded MARS (3.8) and tried one of the supplied sample files (Fibonacci.asm). I'm not seeing this error at any point. What are you doing to get this error?
  • Senseful
    Senseful over 10 years
    Note: If you want to return an integer as part of the exit, see this answer.
  • Spencer H
    Spencer H over 7 years
    Not working. In my case I get the same error still.