Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between call, push+ret and push+jump in assembly

I have a trace instruction and want to extract function calls and returns.

I found that except call instruction, push+jmp and push+ret can be used for function call? At first I want to be sure is that correct? and if yes what are the differences between them?

Also if push+ret is kind of call so what would be the end or return of a function? Seeing only ret without push instruction before it?

like image 804
Alex Avatar asked Jun 18 '15 13:06

Alex


People also ask

What is the difference between jump and RET?

The JUMP instruction does not require to store the return address into the stack. At the time of CALL, the return address of a program counter will be pushed into the stack. At the time of RET instruction, the return address will be popped from the stack and added to the program counter.

What is difference between jump and Call return?

JUMP or GOTO is a transfer of the control to another location and the control does not automatically return to the point from where it is called. On the other hand, a CALL or procedure/function call returns to the point from where it is called.

What is the difference between call and jump in assembly language?

The CALL instruction is used to call a subroutine, but the JUMP instruction updates the program counter value and point to another location inside the program.

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.


2 Answers

Yes, you are correct.

When a call is issued, the return address pushed onto the stack is the next address where execution should continue (the address immediately following the current instruction). In essence it is an atomic push followed by a jmp.

This means that if you manually push and then jmp, the function that you jump into can later ret and, provided all stack access in the function is balanced, it will return to the address that you previously pushed.

Similarly, you can push and then ret to simulate a call return, but this does not set you up to be able to later return again. This type of behavior is more commonly done to throw off disassemblers, making it more difficult to determine which address the code is actually heading to with a simple disassembler.

like image 72
David Hoelzer Avatar answered Oct 16 '22 17:10

David Hoelzer


In simplified terms:

call address

This will push the updated program counter (which points to the instruction after the call) onto the stack then jump to the address indicated (addressing modes may apply).

ret

This instruction internally pops and address off the stack and jumps to it. This is nicely matched with call so it can return to the instruction after the prior call.

jmp address

This simply jumps to the given address (addressing modes may apply). It doesn't do anything with the stack at all.


So, you can also do this:
push address
ret

Which will pop and jump to the address that was pushed onto the stack as described above. It's a clever way to do an indirect jump in a microprocessor that doesn't support indirect addressing modes in their jump instructions.

The sequence:

push address
jmp someplace

Will simply jump to someplace and not affect the stack or use the address that was pushed onto the stack. If address is the instruction after the jmp, this is roughly equivalent to call someplace.

For instruction sets that don't support an indirect addressing jump, I've seen this nice little work-around:

push address
ret

Which will jump to whatever address is.

like image 8
lurker Avatar answered Oct 16 '22 16:10

lurker