I read quite a bit about processors and architectures recently; mainly opcodes, integrated circuits, etc. I have been a python developer for several years now and I would like to get some practice writing machine code.
I thought that I would compile a very simple python script in machinecode as a way to practice it. The script looks like this:
a = 2 b = 3 c = a + b print c
I am writing a compiler in python because I am not as good in C as I am in python. I looked around a bit and I have the following python libraries that 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 need to find opcodes for the Intel Core i5, but this should be easy.
My question is this:
1) How to write the operation code to a file? In other words, suppose that the operation code for setting the register that contains the value 2 is 0010, how to do it, is it the first four numbers in the first line of program execution?
2) How to tell OS, OS X or Ubuntu to load the program into physical memory? I assume that the first thing the compiler does is write instructions for the OS on the resulting binary?
3) Any resources you might know about this can help me.
compiler-construction python
Sam hammamy
source share