Re: Jit Implementation

Philip Herron <herron.philip@googlemail.com>
Sun, 21 Mar 2010 22:34:21 +0000

          From comp.compilers

Related articles
[3 earlier articles]
Re: Jit Implementation jgd@cix.compulink.co.uk (2010-03-20)
Re: Jit Implementation anton@mips.complang.tuwien.ac.at (2010-03-21)
Re: Jit Implementation gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-03-21)
Re: Jit Implementation herron.philip@googlemail.com (Philip Herron) (2010-03-21)
Re: Jit Implementation jthorn@astro.indiana-zebra.edu (Jonathan Thornburg \[remove -animal to reply\]) (2010-03-21)
Re: Jit Implementation cr88192@hotmail.com (BGB / cr88192) (2010-03-21)
Re: Jit Implementation herron.philip@googlemail.com (Philip Herron) (2010-03-21)
Re: Jit Implementation barry.j.kelly@gmail.com (Barry Kelly) (2010-03-22)
Re: Jit Implementation bartc@freeuk.com (bartc) (2010-03-23)
Re: Jit Implementation bartc@freeuk.com (bartc) (2010-03-23)
Re: Jit Implementation cr88192@hotmail.com (cr88192) (2010-03-23)
Re: Jit Implementation cr88192@hotmail.com (BGB / cr88192) (2010-03-23)
Re: Jit Implementation bartc@freeuk.com (bartc) (2010-03-24)
[3 later articles]
| List of all articles for this month |
From: Philip Herron <herron.philip@googlemail.com>
Newsgroups: comp.compilers
Date: Sun, 21 Mar 2010 22:34:21 +0000
Organization: Compilers Central
References: 10-03-054 10-03-060
Keywords: code
Posted-Date: 22 Mar 2010 21:04:15 EDT

bartc wrote:
> #include <stdio.h> #include <stdlib.h>
>
> int main(void){
>
> char* program; int (*fnptr)(void); int a;
>
> program = malloc(1000); /* Space for the code */
>
> program[0] = 0xB8; /* mov eax,1234h */ program[1] =
> 0x34; program[2] = 0x12; program[3] = 0; program[4] = 0; program[5]
> = 0xC3; /* ret */
>
> fnptr = (int (*)(void)) program;
>
> a = fnptr(); /* call the code */
>
> printf("Result = %X\n",a); /* show result */ }
>
> (If this shows "1234", then you're past the main hurdle.)
Some many find this useful, to get this working on linux i had to do this:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include <errno.h>
#include <sys/mman.h>


int main( int argc, char *argv[] )
{
    int (*fnptr)(void); int retval= 0; size_t c_len= 1024;


    char *c_buffer = mmap( NULL, c_len, PROT_EXEC | PROT_READ | PROT_WRITE,
                          MAP_ANONYMOUS | MAP_PRIVATE , -1, 0 );


    *(c_buffer) = 0xB8; /* mov eax,1234h */
    *(c_buffer+1) = 0x34;
    *(c_buffer+2) = 0x12;
    *(c_buffer+3) = 0;
    *(c_buffer+4) = 0;
    *(c_buffer+5) = 0xC3; /* ret */


    fnptr = (int (*)(void)) c_buffer;
    retval = fnptr( ); /* call the code */


    printf("Result = %X\n", retval ); /* show result */
    munmap( c_buffer, c_len );


    return 0;
}


Hope some may find it useful. Have you any good pointers to references
for the operand values for this instruction set? I guess its in the
intel manuals but which ones are most useful or relevant?


- --Phil


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.