Assembly 'call' vs 'jmp'

user3502489 picture user3502489 · Sep 26, 2015 · Viewed 34k times · Source

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.

Answer

nameGoesHere picture nameGoesHere · Sep 26, 2015

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