invalid program counter value: 0

老子叫甜甜 提交于 2019-12-02 00:22:25

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

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!