Section 05 Part 05 – The JSR and RTS Instructions

 

 “I refuse to go bungee jumping, I came into this world because of a broken rubber, I'm not leaving because of one.” ~Unknown Author

 

 

 

The JSR Instruction

 

JSR – Jump to SubRoutine

 

The destination operand is moved into the PC, the return address is moved into the stack, the 68k will continue reading at the location of the destination operand.

 

 

 

Examples

 

This functions almost exactly the same as the JMP instruction.  When the instruction is read, the address is moved into the PC, and that routine is ran next:

 

          move.w    d0,d1

          add.w     d1,d1

          add.w     d1,d0

          jsr       SkipCode

          add.w     d2,d3

          asr.w     #$04,d0

 

SkipCode:

          move.w    d0,d2

          ...etc

 

 

(See Section 05 Part 02 for details)

 

The difference here is that JSR does one extra thing that JMP does not.  JSR stores the return location into the stack, so the 68k can return back to the JSR, and continue.

 

 

 

The RTS Instruction

 

RTS – ReTurn from Subroutine

 

The return address is loaded out of the stack and put into the PC, the 68k will continue reading at the return address.

 

 

 

Examples

 

Here’s an example of JSR and RTS being used together:

 

          move.w    d0,d1

          jsr       AddNumber

          move.w    #$0020,d1

          jsr       AddNumber

          jmp       Continue

 

AddNumber:

          add.w     d1,d2

          rts

 

Continue:

          ...etc

 

 

 

Here’s a graphical diagram, first JSR/RTS:

 

 

Second JSR/RTS:

 

 

 

 

Previous Part

Main Page

Next Part