News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to open a binary file and execute its code?

Started by winter, January 23, 2005, 09:55:19 PM

Previous topic - Next topic

winter

ok I have a file with this in it


mov eax, 2
ret

I compiled it to just its binary. I have another file c++ files which loads this file into memory. In c++ there is no function to execute it so, I made another asm file with the function "_jump" useing stdcall calling convection. It takes one paramemeter (where to jump). the C++ file starts of with main(). "main" loads the binary file then calls "_jump" then "_jump" puts the location to eax and calls it ("call eax"). Thinking everything should go ok, but  it doesn't.  Eax doesn't get set to "2".


; ASM file
section .data
a resd 1

section .text
GLOBAL _jump

_jump:
pop eax
mov [a], eax

pop eax
call eax

push dword [a]

ret



// c++ file
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

extern long __stdcall jump(void *a) asm("_jump");

int main(void)
{
FILE *f;
int sz;
void *buf;

buf = (void*)new char[512];
f = fopen("func.bin", "rb");
if(f == NULL)
{
cout << "didn't open\n";
return 0;
}
sz = fread(buf, 1, 512, f);
cout << sz << '\n';
sz = jump(buf);

cout << sz << '\n';
return 0;
}




Thanks in advance!

winter

Aha I found it :). Just add [bits 32] to the bigining of the binary file.