0

Can I use JMP and RET to jump back from a label as you would with CALL and RET?

Dianabolz
  • 79
  • 1
  • 13

4 Answers4

4

When you use CALL the current value of the instruction pointer is saved on the stack...when the corresponding RET executes, it takes the address from the stack and jumps there. If you just JMP without saving the current address on the stack the corresponding RET will, unsurprisingly, not find a correct address where it expects one. It will probably find some data, nevertheless, it will try to jump to the address represented by those bits. On any decent processor, this will result in some form on violation.

You can jump to a procedure and return with a RET only if you mimic what the CALL instruction does.

user1666959
  • 1,805
  • 12
  • 11
3

No. JMP changes the instruction pointer. CALL pushes the current IP onto the stack and updates the instruction pointer.

If you use a RET with a JMP you are going to return to some unknown location based on what happens to be on the stack at that moment.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
3

A better answer if you want to use JMP to replace CALL, but still use RET or as a replacement for RET also:

    PUSH WORD CS:Call_Return
    JMP My_Method
Call_Return:
    ... (cont)

My_Method:
    ...(some code)
    RET

Or

My_Method:
    ...(some code)
    POP DX
    JMP DX

This just proves it is possible to do the same thing many different ways. This assumes 16-bit addressing (real mode) which does make a difference in this case. In 32-bit/64-bit addressing modes you will need to change the push, pop, and JMP commands accordingly.

Jeremy
  • 843
  • 11
  • 16
1

Maybe if you used something like this :

MOV BX,IP
ADD BX,10  ;If I am not mistaken mov=3bytes,add=3bytes jmp=3 bytes,push=1 byte
PUSH BX
JMP

and then :

RET
manosnismo
  • 11
  • 3
  • Almost. Using a label with a relative address is better. You can't `MOV` the `IP` register. Also, if you use a label, make sure to reference it `CS` relative. x86 is segment based in 16-bit and 32-bit modes. – Jeremy Oct 20 '17 at 16:08