2

this is probably an easy one, but I'm confused about this for some time:

Suppose I have something like

main:
...
call some_fun
...

some_fun:
...
jmp op
...

op:
...
ret

would this ret correctly return to where the call starts? if not, then how would the ret returns correctly?

1
  • It's been a long time since I did any assembly, but I think that in some_fun, you'd have to store a return address on the stack before jumping. I think. :/
    – GJK
    Commented May 3, 2013 at 23:08

2 Answers 2

8

If in the provided example, you want the next instruction it executes after it returns to be the instruction after call some_fun, then yes it will do exactly as you want provided that you clean up your stack frame before returning.

Assuming that you are using x86 assembly, here is what is going on.

  1. When call some_fun is executed, it pushes the return address onto the stack and then changes the program counter/instruction pointer to point to some_fun so it continues execution there.
  2. The jmp op instruction does not modify the stack. It only modifies the program counter/instruction pointer.
  3. The ret instruction pops a value off the stack and dumps it into the program counter/instruction pointer. The value it pops off the stack is known as the return address. It is vitally important that anything some_fun and op have pushed onto the stack be popped off before issuing ret. Otherwise, you will return to the wrong return address.

Hope this helps.

0
1

If it still crashes then try out

move dword[stak],esp ;at the very start

and end with

mov esp,[stak]

ret

kinda thing

gl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.