tags:

views:

99

answers:

5

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

I mean - f is a label in memory right? How can we jump to different address in memory and not update our frame and stack pointers...

EDIT: I am talking about Intel x86 assembly yes :)

+2  A: 

I'll guess you're talking about the Intel instruction? In that case, nothing happens to any stack/frame pointers, the code just continues execution in the same context but at the new address.

I guess that answer has some caveats - it is possible to cause a task switch using the jmp instruction, in which case all kinds of crazy stuff might happen. You'll probably want to read the documentation for all the details. The Intel Software Developer's Manual has all the details:

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

Edit: referring to your question about jumping without updating.

You have to be able to jump around your code without modifying the stack & frame pointers. It's the same as goto in C code, for example - you can hop all over your function without needing to modify the execution context at all.

Carl Norum
Wow... i'm completely lost now. Let me think about it. Updated the question...
drozzy
Those are a bit over my head there...
drozzy
Note that AFAIK no OS actually uses tasking.
Nathan Fellman
Thanks but Nathan's answer was the one I was looking for. I guess I didn't know what I was confused about.
drozzy
+3  A: 

How can we jump to different address in memory and not update our frame and stack pointers...

Because the instruction pointer (eip) is stored in a different register from the frame and stack pointers (esp, ebp). Changing one won't affect the others (normally).

KennyTM
Ok thanks. I'll guess I'll come up with a more concrete question, draw the stack frames etc.. and then ask again. if only i could draw them easily somewhere...
drozzy
+2  A: 

JMP is assembly's goto, with all that implies.

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

Mike DeSimone
+3  A: 

Just a note: in x86, there are many kinds of "jmp" instructions. The most common one is the "local" jmp, which simply changes the value of EIP register, so stack frame isn't touched at all, as Carl pointed out. I assume you are talking about this type of jmp, since it's the one the assemblers generate with a syntax like:

jmp label
...

label:

But there is also the "far" jump, which affects also the CS segment register. If the processor is in real mode, it's still nothing but a change in CS:IP registers (just a "larger" jump), but in protected mode the CS segments has a very different and much more complex function: it's interpreted as a descriptor to CALL/TASK/INTERRUPT gates, i.e. an index in a descriptor table which defines many things like the privilege level, task... Depending on the specific descriptor, a privilege level escalation may happen, or also an "hardware task" switch. This may produce a context change. You won't usually find far jumps in protected mode, unless you are programming the core of an operating system. Creating segment descriptors is almost always a kernel's job.

Regards

Giuseppe Guerrini
+1 for actually explaining a local jump
zebrabox
+4  A: 

The stack and frame pointers deal with location of the data. jmp instructions deal with location of the code. Unless something drastic happens, one should not affect the other. Here's a list of drastic things:

  • Task switches - due to a far jump using a task gate
  • faults - due to a jump to a new page that is invalid, or jumping out of the current segment, or a jmp that tries to change the privilege illegally.
  • traps - for instance, due to a code breakpoint. In fact, no other trap comes to mind at the moment.

That's about it. Even those cases change the stack because they involve some sort of context switch, either to a new task or to some exception handler.

Note also that no OS that I know of uses the CPU's task switching features. It's usually implemented in software.

Nathan Fellman
I think your explanation actually made it clear to me! JMP is for jumping to a certain position IN THE CODE, the text block! I get it. Thanks!
drozzy