Difference between JUMP and CALL

。_饼干妹妹 提交于 2020-08-20 18:36:31

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


One correction to your thoughts: It is not only with tail recursion, but generally with tail calls, that we don't need the stack frame and hence simply could JMP there (provided the arguments have been set up correctly).




回答5:


According to microprocessor, firstly the condition is checked and then it performs jump operations (goes to other code) and does not return. Call operation is like a function callig in the c language and when the function is performed it returns back to complete its execution.



来源:https://stackoverflow.com/questions/523540/difference-between-jump-and-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!