Enviado em 17/07/2012 - 19:54h
//-------------------------------------------------------------------
// A tiny ByteCode example ... changed
//
// FILE: jump.c
//
// This work is based in this:
//----------------------------
// http://byteworm.com/
// Benjamin Kittridge. Copyright (C) 2010, All rights reserved.
//----------------------------
//
// Test Speed ... this execute in 0.452 second in a ( CORE i3 2.1 MHZ )
//
// BY: gokernel - gokernel@hotmail.com
//-------------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#define NEXT goto *(++ip)->jmp
#define NOP 0
#define INC 1
#define CMP 2
#define JMP 3
#define END 4
typedef struct Tasm Tasm;
struct Tasm {
void *jmp;
int arg;
};
void *Gtable [5] = { NULL };
Tasm code[5];
void run (Tasm *o)
{
register int a = 0;
// Setup the Gtable only one
if (Gtable [0] == NULL)
{
Gtable [0] = && label_nop;
Gtable [1] = && label_inc;
Gtable [2] = && label_cmp;
Gtable [3] = && label_jmp;
Gtable [4] = && label_end;
return;
}
Tasm *ip;
goto *(ip=o)->jmp;
label_nop: NEXT;
label_inc: a++; NEXT;
label_cmp: if (a != ip->arg) ip++; NEXT;
label_jmp: goto *(ip=o+ip->arg)->jmp;
label_end: printf ("Exiting OK ... a = %d\n", a);
}
void setup_code (Tasm *o)
{
// 00: { INC },
// 01: { CMP, 100000000 },
// 02: { JMP, 4 },
// 03: { JMP, 0 },
// 04: { END }
o[0].jmp = Gtable [INC]; o[0].arg = 0;
o[1].jmp = Gtable [CMP]; o[1].arg = 100000000; // compare a == 100 000 000
o[2].jmp = Gtable [JMP]; o[2].arg = 4;
o[3].jmp = Gtable [JMP]; o[3].arg = 0;
o[4].jmp = Gtable [END]; o[4].arg = 0;
}
int main (int argc, char *argv[])
{
run (NULL); // set the Gtable opcode( labels to jump )
// This set the ( code->jmp, code->arg )
setup_code (code);
printf ("Please wait ...\n");
clock_t init = clock ();
run (code);
clock_t end = clock ();
printf ("TIME: %ld\n", end - init);
return 0;
}
--------------------------------------
-- Simple test count 100 000 000
value=0;
inicio = os.clock();
for i=0, 100000000 do value=i end
fim = os.clock();
print("Value : "..value.." "..fim-inicio);
--------------------------------------