When your program begins execution, the top item on the stack is a return address. Thus, your main program should end with an "rts" instruction. To avoid bus errors and segmentation faults when this rts gets executed, you will need to make sure the stack pointer is back where it started. So any subroutine that puts stuff on the stack should clean up after itself.
Here is a brief list of the gdb commands you might wish to use.