0

The code as following

    call next
next:
    popl %eax
  1. To what value does register %eax get set?
  2. Explain why there's no matching ret instruction to this call?
  3. What usful purpose does this code fragment serve?

The answer from book are the following. Which I don't understand. Is there any more detailed explainatio of this part of code?

  1. %eax is set to the address of popl instruction.
  2. This is not a true subroutine call, since the control folloe the same ordering as the instructions and the return address is popped from the stack.
  3. This is the only way in IA32 to get value of the program counter into an integer register.
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David Zhou
  • 21
  • 1
  • 2
  • 2
    The way `call` works is by putting the address of the 'next' instruction on the stack and then jumping to the specified instruction. But the stack it's putting that address on is the same stack that `push` and `pop` use. So if you do a call followed by pop, you get the address that the `call` put on the stack. – David Wohlferd Dec 12 '21 at 05:03
  • Related: [Reading program counter directly](https://stackoverflow.com/q/599968), but that doesn't fully explain *how* it works. For that, see [Substitutes for x86 assembly 'call' instruction?](https://stackoverflow.com/q/7060970). Or you can do what GCC does and call a function that loads its return address into EAX before returning with `ret`, as in [How to check the EIP value with assembly language?](https://stackoverflow.com/a/4062434) – Peter Cordes Dec 12 '21 at 06:18
  • Other partial duplicates include: [Substitutes for x86 assembly 'call' instruction?](https://stackoverflow.com/q/7060970) / [Decribing pop in Assembly](https://stackoverflow.com/q/14918663). (and related: [What is the x86 "ret" instruction equivalent to?](https://stackoverflow.com/a/54816685)) – Peter Cordes Dec 12 '21 at 06:31
  • Thanks, this solved my problem. I appreciate your help Mr Wohlderd and Mr Cordes. – David Zhou Dec 12 '21 at 07:40

0 Answers0