What other problems can be caused when using the JMP instruction to navigate through subroutines instead of using the instruction CALL/RETURN

assemblymicrochip

Image of the code

I've been creating a report on why the JUMP instruction shouldn't be used in the piece of code above and instead CALL/RETURN instructions should be used to navigate through the subroutines.

I understand that the code will never reach back2: because it is using the jump command and would continuously loop between back1: and the calculateNextPower routine.

Would I be correct saying that:

  • the jump instruction doesn't push the instruction onto a stack and results in you not being able to return to where you've jumped from.

  • When using call, it prevents you from pushing to many addresses to the stack by recursion like the JUMP instruction would. This can cause a stack overflow/tail recursion and can result in you not getting the result of the calculation until you have returned by using the call and return?

I think that I've made this way more complicated than what it is, read way to many sites and have confused myself on the reasons why it should be used instead of the jump instruction, other than it not being able to get to the "back2:" routine.

Appreciate any help on clearing this up, I'm new to this type of work

Best Answer

So many misconceptions, where to begin...?

First, the real problem with your code is a badly architected loop. Not that CalculateNextPower is immediately before the loop and at the end of the loop. This code should simply be put in-line at the start of the loop, right below Back1. Then there wouldn't be any need to jump or call to code outside the loop.

Second, nothing pushes a instruction onto the stack. CALL instructions generally push addresses onto a stack. That is the address of the instruction following the call. The RETURN instruction pops this address off the stack and jumps to it, thereby returning to immediately after the CALL instruction.

In general, the point of the CALL/RETURN mechanism is to allow execution of a piece of code in multiple routines, but have the piece of code only exist in memory once. CALL/RETURN allows for temporarily diverting execution to the single piece of re-usable code, then going back to where you were.