Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly: Why does jumping to a label that returns via ret cause a segmentation fault?

Linux Assembly Tutorial states:

there is one very important thing to remember: If you are planning to return from a procedure (with the RET instruction), don't jump to it! As in "never!" Doing that will cause a segmentation fault on Linux (which is OK – all your program does is terminate), but in DOS it may blow up in your face with various degrees of terribleness.

But I cannot understand why does it causes a segmentation fault. it sounds just like returning from a function.

I have a situation where I need to implement the logic "If X happens, call procedure A. Otherwise, call procedure B." Is there any other way than jumping around like a kangaroo weaving spaghetti code?

like image 260
InvalidBrainException Avatar asked Mar 23 '12 23:03

InvalidBrainException


1 Answers

Because CALL pushes the current instruction address onto the stack, and RET pulls it off in order to get back to the call-site. JMP (and related instructions) don't push anything onto the stack.

like image 177
Oliver Charlesworth Avatar answered Sep 26 '22 14:09

Oliver Charlesworth