views:

508

answers:

4
+2  Q: 

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.

A: 

You're right about JUMP, CALL you have 50% of the picture. A fundamental part of the CALL is passing parameters to the call, and optionally recieving values back.

squareOfTwo = SQR(2);

In this example the CALL to SQR takes a single parameter, and returns a value.

MrTelly
This is not a exam/assignment question.
Amit
Sorry Amit, removed that line
MrTelly
Thanks mate :-) I am just trying to clear up basics as I learn these concepts in more details and more time .
Amit
+5  A: 

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.

Anteru
When you say: "CALL is just a convenience function implemented using JMP." Does that mean that CALL isn't atomic (one processor instruction)?
Anzurio
You mean like in a thread could interrupt between the movl, but before the jmp itself, if a CALL would be implemented as two instructions? Looking at the docs: http://www.intel.com/Assets/PDF/manual/253666.pdf, it says the procedure does first move the address on the stack, and then the jump, clearly stating this as a sequence. I don't see why it should be atomic, plus, how would it matter? It's just the same as if you would be interrupted right after the jmp, as the jmp is unconditional.
Anteru
+1  A: 

I think you've got the general idea.

It depends on the architecture, but in general, at the hardware level:

  • A jump instruction will change the program counter to continue execution at a different part of the program.

  • A call instruction will push the current program location (or current location + 1) to the call stack and jump to another part of a program. A return instruction will then pop the location off of the call stack and jump back to the original location (or orignal location + 1).

So, a jump instruction is close to a GOTO, while a call instruction is close to a procedural/function call.

Also, for the reason that a call stack is used when making function calls, pushing too many return addresses to the call stack by recursion will cause a stack overflow.

When learning assembly, I find it easier when dealing with RISC processors than x86 processors, as it tends to have less instruction and simpler operations.

coobird
+2  A: 

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.

Greg Hewgill