Build: Why does switching to a label that returns via ret cause a segmentation error?

Linux Build Tutorial :

There is one very important thing to remember: if you plan to return from the procedure (with the RET instruction), do not go to it! Like in "never!" This will cause segmentation to fail on Linux (this is normal - your entire program is complete), but in DOS it can explode in your face with varying degrees of horror.

But I can’t understand why it causes a segmentation error. it sounds the same as returning from a function.

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

+4
source share
2 answers

Because CALL pushes the current instruction address onto the stack, and RET disables it to return to the call site. JMP (and related instructions) push nothing onto the stack.

+7
source

I think this advice may be related to the assembly line, but I'm not sure.

I believe the question you ask is:

 ... subroutine entrypoint ... ... various instructions in a routine ... jmp label ... move instructions in a routine... label: ret 

What problem, if any, is with this? Firstly, I'm not sure if this is a problem at all. But if so, this is a pipeline. On some processors, one or more instructions after running jmp before the control moves to the mark.

Basically, I'm afraid that you misunderstood what you read, or I misunderstood what you wrote. jmp-ing from one point in your routine to the ret statement should be fine. jmp-ing instead of doing ret, as other people point out, is a dumb idea.

-1
source

Source: https://habr.com/ru/post/1403177/


All Articles