Printing Opcodes to a File

365 views Asked by At

Just curious, if open a hex editor and type opcodes manually and save it. Such as move 1 to register 1 move 2 to register 2 then call add, would that give me working program? Or does some how automating this gives me crappy compiler?

EDIT: Forgot about OS, I have Mac OS X and Linux at my disposal, x86's as the CPU.

3

There are 3 answers

2
Brian Agnew On BEST ANSWER

As I noted in the comments, the OS/platform is important here. Most platforms will expect some sort of structure to an executable (if only a header etc.). e.g. see the a.out format as used by older versions of Unix.

As you've indicated you have Mac OS X, check out the Mach-O format.

Beware that typing 'A0 A1'... etc. is different from putting the actual bytes (in this case 160/161) in the file.

0
Aaron On

As long as you have the proper file format for the binary then yes.

Old DOS .com files were just this - the raw program serialized out to disk. .exe files and unix ELF files have a structured format to them which includes (among other things) relocation information.

0
David R Tribble On

Only if the file is a properly formatted executable binary file. See references on the COFF and ELF formats, for example.

You could also dump the contents of a small executable file using od if you're curious about what a particular executable file looks like.