32

I got told to try and use 'jmp rather than 'call', but 'jmp' is not liking me .. when I jump it doesn't return (so it never exits and not happy days ), but calling returns and exits as normal.

I am happy using 'call' but is there actually a reason I should try and overcome 'jmp' ?

This simple code just shows if when I jmp it never returns and exits.

_start:

    jmp _Print
    jmp _Exit

ret


_Exit:

    ; normal exit 

ret


_Print

    ; print something

ret

also .. I'm running this all in a Linux terminal if that changes anything.

5
  • 5
    This is ambiguous, and very unclear. JMP doesn't set up the stack (by pushing the return value) so when you do a ret, a bogus return address gets popped off the stack and your code jumps to it. You can fake a call with a jmp but prior to doing jmp you must push the return address on the stack yourself. What assembler and what OS are you targeting? 16 or 32 bit? Sep 26, 2015 at 2:59
  • I think your question might have an answer similar to this: stackoverflow.com/a/28133370/3857942 . Sep 26, 2015 at 3:02
  • There is no reason I can think of (in this case) to use JMP instead, except to be done in school to teach you how a CALL and RET function together by simulating the CALL with JMP. It will also teach you something about the stack Sep 26, 2015 at 3:13
  • 1
    Some of the answers on this thread might be useful. stackoverflow.com/questions/41205054/…
    – tanvi
    Jan 3, 2017 at 6:53
  • The others answered it basically. cdecl and stdcall are part of the issue. CALL and RET are designed to build and tear down the stack depending on your calling convention. So by using JMP you don't build the stack appropriately. a JMP is more for loops or continuation of code elsewhere. In other words. a CALL is a JMP with the added feature of pushing the next instruction address onto the stack. Oct 31, 2021 at 19:25

1 Answer 1

50

Well, first of all, jmp simply 'jumps' to the label that you give to it (which is a memory address as program instructions are stored in memory) while call stores the location where it will return (below the call instruction) in the stack, jmp to the label, and then at the ret instruction, jmp back to what location was stored (as said above, below the call instruction). A bit of a difference there as you can see. IMHO, i believe it is fine to simply call functions, as that is what the c++ compiler does with functions, but if you must jmp, then alright then, just make sure to push the return location or create another label to return to once done executing some code.

Here is an example of jumping to other label when done:

_start:



 jmp _Print;



_start_label:



 jmp _Exit;

_Exit:
 ; exit stuff goes here

 ret;     

_Print:

;print stuff goes here

jmp _start_label;

or you could just use call :)

1
  • That definitely clears things up thankyou! I have only just started learning assembly this week so it is good to get advice like this. Sep 26, 2015 at 3:30

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.