The entrance and exit locations in maps was coded in a special way. Programmatically speaking, there are what I call entrance points and warp points. Each was programmed differently. Going into a town is an entrance point and will add your last location ( the world map ) into a stack data format. Going into a house will push the town location into the stack. As you exit these homes / rooms / towns / etc. the stack pops and the game can figure out where to put you.
Warp points were used in other cases such as caves. When you enter a cave, the game does NOT push your current location into the stack. Instead, it REPLACES the last stack location with the location you are entering. Upon exit, depending on the tile you stepped on, it knows to change the location back to something appropriate. The reason this was needed for caves was because some caves have more than one exit point. Some caves lead from one entrance in the world map to another one. If the stack approach was used here, it would be impossible for the game to put you on the other side of the cave when you exited the cave through the other entrance.
When you are on the world map, you are at stack depth 0. If you enter into a town, you are at stack depth 1, and entering a house will put you at stack depth 2. Exiting these simply pops the stack accordingly, making it easy to remember where you came from, and where to return you. Entering a cave from the world map will keep you in stack depth 0 instead of pushing you into stack depth 1. You are essentially "warped" to a new location. If you step on certain tiles on the world map that are out-of-bounds, these tiles are marked as boundaries to the current map you are on. ( Much in the same way a town map has tiles around it marked as boundaries, so the game knows to "exit" you back to the world map when you step on them. )
Well, when you step on these on the world map ( stack depth 0 ) the algorithm is supposed to pop the stack and return you to the previous location you were on. The game is essentially popping the stack that has a current depth of 0. This is a bug and the result is that the memory location right before the call stack memory is used as the return point. This will usually lead to returning to the same coordinates, depending on the memory that shows up before the stack memory. Since you're not supposed to walk thru walls, this is not REALLY a bug. There is no other way to trigger this bug so it does not need fixing.
https://www.gamefaqs.com/boards/563402- ... 42?page=13