Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly 'call' vs 'jmp'

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.

like image 222
user3502489 Avatar asked Sep 26 '15 02:09

user3502489


People also ask

What is the difference between call and jmp in assembly?

CALL instruction is used to call a subroutine. Subroutines are often used to perform tasks that need to be performed frequently. The JMP instruction is used to cause the PLC (Programmable Logic Control) to skip over rungs.

What does jmp mean in assembly?

In the x86 assembly language, the JMP instruction performs an unconditional jump. Such an instruction transfers the flow of execution by changing the program counter.

What is the difference between jump and RET?

At the time of RET instruction, the return address will be popped from the stack and added to the program counter. The JUMP instruction is not used to transfer the value of a program counter into the stack.

How jmp is different from Jnz?

JMP . is essentially an infinite loop as the code will keep jumping back to itself infinitely until you get an interrupt . The JNZ statement is a conditional jump statement which will work as JMP when the zero flag is not set ( Z = 0 ).


1 Answers

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

like image 94
nameGoesHere Avatar answered Sep 22 '22 22:09

nameGoesHere