Re: A86: Program flow


[Prev][Next][Index][Thread]

Re: A86: Program flow




Instead of <jp keyloop>, do <call keyloop>.  Make sure you have a RET at
the end of the keyloop.  No, the ret command is not just used to exit a
program.

When you run a program, a register called the program counter (PC) keeps
track of the address of the current (or is it next?) program
instruction.  The JP command loads the address of a specified label into
the PC, and program execution moves to that spot.  The CALL instruction
also loads the address of the label to be called into PC, but beforehand
it saves the current PC to the stack.  When a RET instruction is
encountered, the opposite takes place:  the value on the top of the
stack is loaded into PC, returning program execution to where it was
before the CALL.

You always want to make sure that routines that you call have a ret at
the end.  Otherwise, the value of PC when the call instruction was
encountered will remain on the stack.  Then when you attempt to use a
ret to quit to the homescreen, it will go to that spot in your program
instead.  Another thing you have to watch is having a PUSH command
without a POP, or vice versa, as this can cover up or delete the stack
entry that tells the ret instruction where to go, and your calculator
will likely crash and burn. :)

Cassady Roop

> I have a question about program flow and the ret function.  The best way to
> explain my question is with this little example.
> 
>  code
>  ......
>  ......
>  jp keyloop
	make this 'call keyloop'
>  more code
>  ......
>  ......
> 
> keyloop
>   some code for getting a string
	make sure to have a RET at the end of this
> 
> How do I return from the key loop to where I left off in the code?  Does the
> ret function do this, or that only to quit to the TI-OS?  I will need to call
> the getstring function from many different places in my program, so I can't
> just use a jump.  This is probably an easy question, so I hope I'm not making
> it more complicated than it needs to be.  Any help would be appreciated.
> 
> Thanks,
> Bowser


References: