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?
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With