I\'m looking at writing a JIT compiler for a hobby virtual machine I\'ve been working on recently. I know a bit of assembly, (I\'m mainly a C programmer. I can read most ass
This is written in AT&T assembly. As you can see from the execution of the program, output has changed because of self-modifying code.
Compilation: gcc -m32 modify.s modify.c
the -m32 option is used because the example works on 32 bit machines
Aessembly:
.globl f4
.data     
f4:
    pushl %ebp       #standard function start
    movl %esp,%ebp
f:
    movl $1,%eax # moving one to %eax
    movl $0,f+1  # overwriting operand in mov instuction over
                 # the new immediate value is now 0. f+1 is the place
                 # in the program for the first operand.
    popl %ebp    # standard end
    ret
C test-program:
 #include 
 // assembly function f4
 extern int f4();
 int main(void) {
 int i;
 for(i=0;i<6;++i) {
 printf("%d\n",f4());
 }
 return 0;
 }
 Output:
1
0
0
0
0
0