Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JUMP and CALL

How is a JUMP and CALL instruction different? How does it relate to the higher level concepts such as a GOTO or a procedure call? (Am I correct in the comparison?)

This is what I think:

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. Due to this difference in their nature, languages typically make use of a stack and a stack frame is pushed to "remember" the location to come back for each procedure called. This behaviour applies to recursive procedures too. In case of tail recursion, there is however no need to "push" a stack frame for each call.

Your answers and comments will be much appreciated.


2 Answers

You're mostly right, if you are talking about CALL/JMP in x86 assembly or something similar. The main difference is:

  • JMP performs a jump to a location, without doing anything else
  • CALL pushes the current instruction pointer on the stack (rather: one after the current instruction), and then JMPs to the location. With a RET you can get back to where you were.

Usually, CALL is just a convenience function implemented using JMP. You could do something like

          movl $afterJmp, -(%esp)
          jmp location
afterJmp:

instead of a CALL.

like image 137
Anteru Avatar answered Sep 13 '25 05:09

Anteru


You're exactly right about the difference between a jump and a call.

In the sample case of a single function with tail recursion, then the compiler may be able to reuse the existing stack frame. However, it can become more complicated with mutually recursive functions:

void ping() { printf("ping\n"); pong(); }
void pong() { printf("pong\n"); ping(); }

Consider the case where ping() and pong() are more complex functions that take different numbers of parameters. Mark Probst's paper talks about tail recursion implementation for GCC in great detail.

like image 38
Greg Hewgill Avatar answered Sep 13 '25 07:09

Greg Hewgill