What does JMP do for stack and frame pointers?

When an assembly has an instruction like jmp f , what happens with the stack and frame pointers?

I mean - is f a label in memory? How can we go to another address in memory and not update our pointers to the frame and stack ...

EDIT: I'm talking about Intel x86 build yes :)

+4
source share
5 answers

Stack and frame pointers have data locations . jmp instructions relate to the location of the code . Unless something decisive happens, one cannot influence the other. Here is a list of radical things:

  • Task switches - due to long-distance transition using task shutter
  • Errors - due to the transition to a new page that is invalid or jumps from the current segment, or jmp , which is trying to illegally change the privilege.
  • traps - for example, due to a code breakpoint. In fact, at the moment no other trap comes to mind.

What about that. Even these cases change the stack because they are associated with some kind of context switch, or with a new task, or with some kind of exception handler.

Please note that not a single OS that I know of uses CPU switching functions. It is usually implemented in software.

+5
source

I assume you are talking about Intel instructions? In this case, nothing happens with the stack / frame pointers, the code just continues execution in the same context, but at the new address.

I think there are some caveats in the answer - you can invoke the task switch using the jmp instruction, and in this case all kinds of crazy things can happen. You will probably want to read the documentation for all the details. Intel Software Developers Guide contains detailed information:

  • jmp documentation in volume 2A
  • 7.3 Task Switching in Volume 3A

Edit: referring to your question about jumping without an update.

You should be able to jump around your code without changing the stack and frame pointers. For example, this is the same as goto in C code - you can jump over your entire function without changing the execution context at all.

+3
source

How can we go to another address in memory and not update our pointers to the frame and stack ...

Since the instruction pointer (eip) is stored in a different register from the frame and stack pointers (esp, ebp). Changing one will not affect the rest (usually).

+3
source

Just a note: there are many kinds of "jmp" commands in x86. The most common is the "local" jmp, which simply changes the value of the EIP register, so the stack frame is not affected at all, as Karl pointed out. I assume that you are talking about this type of jmp, as this is the one that collectors generate with syntax like:

 jmp label ... label: 

But there is a โ€œdistantโ€ jump, which also affects the CS segment register. If the processor is in real mode, it is still nothing but changing the CS: IP registers (just a โ€œbigโ€ transition), but in protected mode the CS segments have a completely different and much more complex function: it is interpreted as a handle to the CALL / gates TASK / INTERRUPT, that is, an index in a descriptor table that defines many things, such as a privilege level, a task ... Depending on a particular descriptor, an escalation of the privilege level or the โ€œhardware taskโ€ switch can occur. This can lead to a change in context. Usually you will not find transitions in protected mode unless you program the kernel of the operating system. Creating segment descriptors is almost always the job of the kernel.

Hi

+3
source

JMP is a goto assembly, with all of this implied.

Sometimes you just need to start execution from a different address.

+2
source

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


All Articles