Recently I've been reading quite a bit about CPUs and architectures; mainly opcodes, Integrated Circuits, etc. I've been a python developer for a few years, and I'd like to get some practice in writing machinecode.

I thought for fun I'd compile a very simple python script into machinecode as a way to practice it. The script is as follows:

a = 2
b = 3
c = a + b
print c

I'm writing the compiler in python, because I'm not as good at C as I am at python. I've looked a round a little, and I have the following python libraries at my disposal, which might help, i.e.

binascii.hexify(hex(2))  <-- should convert 2 to binary, correct?

file = open('/usr/local/bin/my_sample_program','wb') <-- should write the resulting binary file

I still have to find the opcodes for Intel Core i5, but that should be easy to.

My question is as follows:

1) How do I write the opcode to the file? In other words, assume the opcode for setting a register to contain the value 2 is 0010 how do I write this is as the first four numbers in the program's first line of execution?

2) How do I tell the OS, either OS X or Ubuntu, to load the program into physical memory? I'm assuming that the first thing a compiler does is write instructions for the OS onto the resulting binary file?

3) Any resources that you might know of that can help me would be appreciated.

Edit
Report