3 Replies - 1873 Views - Last Post: 02 March 2011 - 09:11 AM Rate Topic: -----

#1 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

JIT compiling (little endian) mov

Posted 14 November 2010 - 05:15 PM

Description: allocate an array on the heap and store the instructions. allocates a function on the heap and stores x86 instructions to copy the value of y to x.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef void(*func)(); //type def for a function pointer so we can call the genrated function

inline void GetMov(unsigned char** code,int* x,int* y) {
    *code = malloc(11);
    //check your endianness here, this is for little ediean i dont have a big computer to test this on so i cant say how it will work
    /*try this
    unsigned char temp[] = {0xA1,((unsigned int)y)<<24,((unsigned int)y)<<16,((unsigned int)y)<<8,((unsigned int)y),
                            0xA3,((unsigned int)x)<<24,((unsigned int)x)<<16,((unsigned int)x)<<8,((unsigned int)x),
                            0xC3
                           };
    */
    unsigned char temp[] = {0xA1,((unsigned int)y),((unsigned int)y)>>8,((unsigned int)y)>>16,((unsigned int)y)>>24, //MOV EAX,[y]
                            0xA3,((unsigned int)x),((unsigned int)x)>>8,((unsigned int)x)>>16,((unsigned int)x)>>24, //MOV [x],EAX
                            0xC3
                           };
    memcpy(*code,temp,11);
}

void Run() {
    unsigned char* code;
    int x,y=22;
    GetMov(&code,&x,&y);
    ((func)code)();
    printf("%i",x); //should print 22 to the console
}

int main() {
    //this code must assume that pointers are 4 bytes, int's are 4 bytes, and char's are 1 byte,
    assert(sizeof(unsigned char)==1);
    assert(sizeof(unsigned int)==4);
    assert(sizeof(unsigned char*)==4);
    assert(sizeof(int*)==4);
    Run();
}



Is This A Good Question/Topic? 0
  • +

Replies To: JIT compiling (little endian) mov

#2 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: JIT compiling (little endian) mov

Posted 02 March 2011 - 04:53 AM

have you ever written a (dynamic) recompiler, perhaps for an emulator? or are you interested in the subject?
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: JIT compiling (little endian) mov

Posted 02 March 2011 - 09:06 AM

i wrote a a JITed brainfuck compiler, i made a tutorial out of it as well. im proofreading it right now but i should present it soon. and yes im very intrested in the subject :)
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: JIT compiling (little endian) mov

Posted 02 March 2011 - 09:11 AM

i kinda want to update this now that i look at it, i figured out how to do it in an endian independent way and i know i few other things now that make this whole thing work better.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1