Day 11 - The JSR Instruction

Published: 2005-07-22
Updated: 2010-12-19
Author: Mike Huber

JSR?

JSR stands for Jump to SubRoutine and is used to, well... jump to a subroutine.

How do we use it?

We use it by giving it the name of the label that we want to jump to. Example:

jsr OurSub ; blah, some code

OurSub: ; blah, some more code ; return to instruction after the JSR. </code>

Return? How?

With the RTS (ReTurn from Subroutine) instruction of course! So the full version of the above stuff is:

jsr OurSub ; blah, some code

OurSub: ; blah, some code rts ; returns and resumes execution at the first “blah, some code”. </code>

Something more

One thing to remember though, if you just want to separate your code out. then what is described above should be enough. If you want to jump to a subroutine based on a comparison, you would have to do something like the following:

cmp #5 ; compare A with 5 bne DONTcall ; if A isn't 5 we branch over the subroutine call. jsr OurSub DONTcall:

;somewhere... possibly far away: OurSub:

;blah, code.
rts ; return </code>

This Day In Review

I hope you get that, if you don't.. well.. I just won't think like that. ;) Seriously though, if you ever need help with something from these tutorials or find an error, don't hesitate to tell me.

Happy coding!,
-Mike H a.k.a GbaGuy