03-11-2015, 09:17 PM
(03-11-2015, 07:41 PM)greatgamer34 Wrote: ...call and return. Arent they just for running subroutines? Why not just have a branch to the sub routine?
You're right they "just" run subroutines.... but it saves the programmer work, and saves memory (program space). "Call" is like "branch", but it automagically stores a return address, which means the end of the subroutine is simply a "return" instead of a "jump to <address>".
Sure, I could (among other possibilities)
Code:
PUSH <PC + 4>
JMP <subroutine>
and my subroutine could finish with
Code:
PULL reg0
JMP <reg0>
But you're looking at probably 4-bytes of instruction per call, and another 4-bytes for return. (That's assuming you've got PUSH and PULL instructions). Instead with call and return, you're looking at probably 2-bytes per CALL and 1-byte to return.
It might not sound like much, but how much program memory does the average Redstone computer have?
And then there's the debugging cost: with automagic CALL and RETURN, there's no way for me to screw up and push the wrong return address, or forget to push anything at all.