Assembly 'call' vs 'jmp'

别等时光非礼了梦想. 提交于 2019-12-03 11:24:05

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/32793117/assembly-call-vs-jmp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!