Assembly 'call' vs 'jmp'
Asked Answered
C

1

33

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.

Conjuncture answered 26/9, 2015 at 2:47 Comment(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?Pastrami
I think your question might have an answer similar to this: https://mcmap.net/q/453153/-best-way-to-simulate-a-call-with-jmp-dos-assembly .Pastrami
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 stackPastrami
Some of the answers on this thread might be useful. #41205554Borax
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.Antediluvian
S
51

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

Sidman answered 26/9, 2015 at 3:21 Comment(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.Conjuncture

© 2022 - 2024 — McMap. All rights reserved.