From: Thomas McGahee To: pic microcontroller discussion list Subject: Re: Goto & Call Date: Saturday, March 20, 1999 11:21 AM When programming with a processor that allows a reasonable size stack, I prefer to use CALLs. But most PICs have a very small stack size and I find I have to be careful not to exceed it. For this reason, when working with PICs I have adopted the following rules for myself: Whenever a routine will actually "return" to a single, fixed location, use a GOTO to access the routine, and use a GOTO to "return" from the routine. Note that you can save two GOTO's by simply coding the routine in-line in the first place. However, it is often useful (for the sake of your sanity) to write some sections of code this way, as it makes the underlying structure of the main program easier to follow. This is a matter of personal preference, of course. If you later discover that you need to save a few bytes of code space, then you can always move the code to in-line form and remove the accompanying GOTOs. But during initial coding it is very handy to compartmentalize sections of code. Whenever a routine will need to "return" to one of *several* possible calling locations, use a CALL to access the routine, and a RETURN instruction to terminate it. It is always good programming style to have a comment header at the beginning of a subroutine that tells what parameters are being passed, whether they are in W or in file registers, what ACTION is performed by the subroutine, and what return values there are, and how they are being returned (in W or in file registers). ****** That being said, there are some sneaky things you can do to create a sort of subroutine that does not use the usual CALL/RETURN instruction set. To implement this method requires the use of a file register to hold the "return" address. Actually, it is more like a "goto" address! Here is a short (stupid) example just to show how the method works: Assume a file register has been assigned the name GOTOHERE **** "CALLING" routine movlw RETURNHERE ;set up "return" address movwf GOTOHERE ;save it in file register goto PSEUDOSUB ;run "subroutine" returnhere ;"return" label. .... ;this is "return" address! ..... ;continue with user program... --------------- "CALLED" routine PSEUDOSUB ;label for "subroutine" ..... ;instructions that make up "subroutine" ..... ;etc., etc., etc.... movf GOTOHERE,w ;recover "return" vector movwf PCL ;now it is in the Program Counter and that's all there is to it, because the PIC will now execute the instruction pointed to by the contents of PCL! Hope this helps. Fr. Tom McGahee ---------- > From: Javier > To: PICLIST@MITVMA.MIT.EDU > Subject: Goto & Call > Date: Friday, March 19, 1999 10:54 PM > > Hi > Can somebody tell me when I should use a Call or Goto instruction?? Pro and > Cons?? > I also have a problem that when I use Goto and Call instruction, that > reaches a Routine at the bottom of my code it doesnt work, but works with a > routine at the begining of the code, ?4m really confused.How is this > possible?? > Thanks for your time > Javier