How to write self-modifying code in x86 assembly

后端 未结 7 1589
醉酒成梦
醉酒成梦 2020-11-28 18:38

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

7条回答
  •  眼角桃花
    2020-11-28 18:58

    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
    

提交回复
热议问题