Is this possible to do in BMax?
BlitzMax Forums/BlitzMax Programming/Is this possible to do in BMax?
| ||
typedef int (*FuncPtr)();
// Create a function:
char testFunc[] = { 0x90, // NOP (not really necessary...)
0xB8, 0x10, 0x00, 0x00, 0x00, // MOVL $16,%eax
0xC3 }; // RET
int main () {
// Make a copy on the heap, OS doesn't like executing the stack:
FuncPtr testFuncPtr = (FuncPtr) malloc(7);
memmove( (void*) testFuncPtr, testFunc, 7 );
printf("Before function.\n");
int result = (*testFuncPtr)();
printf("Result %d\n", result);
system("PAUSE");
return 0;
} |
| ||
Works fine :)SuperStrict Framework BRL.StandardIO Local testFunc:Byte[]=[$90:Byte,$B8:Byte,$10:Byte,$00:Byte,$00:Byte,$00:Byte,$C3:Byte] Local testFuncPtr:Int()=MemAlloc(testFunc.length) MemMove(testFuncPtr,testFunc,testFunc.length) Print "Before function." Local result:Int=testFuncPtr() Print "Result: "+result |
| ||
| nice :) how does this: 0xB8, 0x10, 0x00, 0x00, 0x00, translate to: // MOVL $16,%eax (I don't mean that I don't believe it, I want to know what byte represents what (except $10 which I can work out for myself)). |
| ||
| Nice Foody, that was surprisingly painless. |
| ||
| @Perturbatio "0xB8, 0x10, 0x00, 0x00, 0x00" is how the PC sees "MOVL $16,%eax". Like in the good old 8-bit days, people used to write entire games like that! :) If you do a search for something like "x86 instruction set" in Google, you should be able to find a list of what bytes mean what. |
| ||
| yep, I presumed 0xB8 is MOV and 0x10 is the value 16, but do you need 0x00 three times to refer to the EAX register? |
| ||
| 0xB8 is probably the whole MOV EAX, and the 3 '00s' are probably the 3 empty bytes of a 32 bit '16' |
| ||
| ahhh, that makes more sense :) |