Note that this was necessary, because on some processors you cannot directly access the address of the program counter, so as this stackoverflow answer explains...
the canonical way to get it on x86 is to jump to a subroutine right in front of you, and pop the return address of the stack, instead of "ret" popping the return-address and, well... returning.
But actually you can translate the 6502 solution directly to x86 assembler (gcc on Linux):
#include <stdio.h>
int
main(int argc, char argv)
{
void p;
(void) argc;
(void) argv;
printf("main is at %p\n",&main);
__asm__(
" jmp 1f\n" /* that's the IORTS */
"0: ret\n" /* subroutine */
"1: call 0b\n" /* call IORTS */
" sub $0x4,%%esp\n" /* decrease stackpointer */
" pop %0\n": "=r"(p)); /* pop register */
printf("PC = %p\n",p);
return 0;
}
http://stackoverflow.com/a/7932364/419237
the canonical way to get it on x86 is to jump to a subroutine right in front of you, and pop the return address of the stack, instead of "ret" popping the return-address and, well... returning.
But actually you can translate the 6502 solution directly to x86 assembler (gcc on Linux):
#include <stdio.h>
int main(int argc, char argv) { void p;