Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/24/05 05:44
Read: times


 
#101477 - Yes
Responding to: ???'s previous message
Guy Lavoie said:
So technically, this can be done.


Sure.

Sometimes, I find that the line between a clever programming trick and a case of spaghetti code can be blurry. In most cases, it seems to be more of a question of generally accepted programming practices...


My personal opinion is that there are few instances when modifying the return address is a good idea. Especially in this case where you've indicated that you want to return to a fixed address in some situations.

You need to be very careful if you call the subroutine from different levels of program nesting. Consider the following sequence of events.

1. Program calls stack-modifying-subroutine.
2. Stack-modifying-subroutine pops 2 bytes off the stack (return address) and jumps to the fixed address when a carriage return is detected.
3. Program continues execution at the fixed address.

But what happens in this cse:

1. Program calls a subroutine.
2. Program calls stack-modifying-subroutine.
3. Stack-modifying-subroutine pops 2 bytes off the stack (return address from step #2) and jumps to the fixed address when a carriage return is detected.
4. Program continues execution at the fixed address; but the return address from step #1 is still on the stack.

So if you're not really careful and if you call the stack-modifying-subroutine from different levels, you could very easily mess up the stack and cause the stack to eventually overflow.

One possibility is actually resetting the stack pointer (SP) SFR when the stack-modifying-subroutine jumps to the fixed address, but that's getting really messy and would definitely qualify as "spaghetti" in my book.

This is just my opinion, though. It's not an exact science and there isn't a right or wrong answer. Be very careful if you try it.

Regards,
Craig Steiner


List of 24 messages in thread
TopicAuthorDate
RET to a different address            01/01/70 00:00      
   here is how pseudocode            01/01/70 00:00      
      RET to a different address            01/01/70 00:00      
         no flaw, but 1.000.000 gotchas            01/01/70 00:00      
            That's what I wanted to know            01/01/70 00:00      
               Yes            01/01/70 00:00      
               "clever"            01/01/70 00:00      
                  OT: my wife            01/01/70 00:00      
         no flaw, but seriously not recommended            01/01/70 00:00      
            experience            01/01/70 00:00      
               reload SP            01/01/70 00:00      
                  restoring stack            01/01/70 00:00      
         Recognisable string            01/01/70 00:00      
      named return value            01/01/70 00:00      
   Bad Practice            01/01/70 00:00      
      Well phrased            01/01/70 00:00      
   What I am doing with it            01/01/70 00:00      
      try...catch            01/01/70 00:00      
         setjmp / longjmp            01/01/70 00:00      
         when to try ... catch            01/01/70 00:00      
            the borderline            01/01/70 00:00      
               Promises            01/01/70 00:00      
                  who cares if an exceptiom is "acceptable            01/01/70 00:00      
      Parsing input data            01/01/70 00:00      

Back to Subject List