31

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user3502489
  • 361
  • 1
  • 4
  • 11
  • 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? – Michael Petch Sep 26 '15 at 02:59
  • I think your question might have an answer similar to this: http://stackoverflow.com/a/28133370/3857942 . – Michael Petch Sep 26 '15 at 03: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 – Michael Petch Sep 26 '15 at 03:13
  • 1
    Some of the answers on this thread might be useful. http://stackoverflow.com/questions/41205054/what-if-there-is-no-return-statement-in-a-called-block-of-code-in-assembly-progr?noredirect=1#comment69616101_41205054 – tanvi Jan 03 '17 at 06: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. – Robert Cotterman Oct 31 '21 at 19:25

1 Answers1

47

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 :)

nameGoesHere
  • 488
  • 5
  • 4
  • That definitely clears things up thankyou! I have only just started learning assembly this week so it is good to get advice like this. – user3502489 Sep 26 '15 at 03:30