Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I'm not an expert, but here is my stab:

You don't seem asking about writing a compiler, just an assembler. This isn't really magic.

Stealing someone elses answer from SO ( http://stackoverflow.com/questions/3826692/how-do-i-translate-assembly-to-binaryhttps://stackoverflow.com/questions/3826692/how-do-i-translate-assembly-to-binary), assembly looks like this:

label: LDA #$00 JMP label 

Then you run it through an assembler, and turn into into something like this:

$A9 $00 $4C $10 $00 

Only it's all squashed up, like this:

$A9 $00 $4C $10 $00 

It's really not magic.

You can't write that in notepad, because notepad uses ASCII (not hex). You would use a hex editor, or simply write the bytes out programatically. You write that hex out to a file, name it "a.exe" or "a.out", then tell the OS to run it.

Of course, modern CPUs and operating systems are really quite complicated, but that's the basic idea.

If you want to write a new compiler, here is how it's done:

  1. Write an interpreted language using something like the calculator example in pyparsing (or any other good parsing framework). That will get you up to speed on the basics of parsing.

  2. Write a translator. Translate your language into, say, Javascript. Now your language will run in a browser.

  3. Write a translator to something lower level, like LLVM, C, or Assembly.

You can stop here, this is a compiler. It's not an optimizing compiler, but that wasn't the question. You might also need to consider writing a linker and assembler, but do you really want to?

  1. (Insane) Write an optimizer. Large teams work for decades on this.

  2. (Sane) Get involved in an existing community. GCC, LLVM, PyPy, the core team working on any interpreter.

I'm not an expert, but here is my stab:

You don't seem asking about writing a compiler, just an assembler. This isn't really magic.

Stealing someone elses answer from SO ( http://stackoverflow.com/questions/3826692/how-do-i-translate-assembly-to-binary), assembly looks like this:

label: LDA #$00 JMP label 

Then you run it through an assembler, and turn into into something like this:

$A9 $00 $4C $10 $00 

Only it's all squashed up, like this:

$A9 $00 $4C $10 $00 

It's really not magic.

You can't write that in notepad, because notepad uses ASCII (not hex). You would use a hex editor, or simply write the bytes out programatically. You write that hex out to a file, name it "a.exe" or "a.out", then tell the OS to run it.

Of course, modern CPUs and operating systems are really quite complicated, but that's the basic idea.

If you want to write a new compiler, here is how it's done:

  1. Write an interpreted language using something like the calculator example in pyparsing (or any other good parsing framework). That will get you up to speed on the basics of parsing.

  2. Write a translator. Translate your language into, say, Javascript. Now your language will run in a browser.

  3. Write a translator to something lower level, like LLVM, C, or Assembly.

You can stop here, this is a compiler. It's not an optimizing compiler, but that wasn't the question. You might also need to consider writing a linker and assembler, but do you really want to?

  1. (Insane) Write an optimizer. Large teams work for decades on this.

  2. (Sane) Get involved in an existing community. GCC, LLVM, PyPy, the core team working on any interpreter.

I'm not an expert, but here is my stab:

You don't seem asking about writing a compiler, just an assembler. This isn't really magic.

Stealing someone elses answer from SO ( https://stackoverflow.com/questions/3826692/how-do-i-translate-assembly-to-binary), assembly looks like this:

label: LDA #$00 JMP label 

Then you run it through an assembler, and turn into into something like this:

$A9 $00 $4C $10 $00 

Only it's all squashed up, like this:

$A9 $00 $4C $10 $00 

It's really not magic.

You can't write that in notepad, because notepad uses ASCII (not hex). You would use a hex editor, or simply write the bytes out programatically. You write that hex out to a file, name it "a.exe" or "a.out", then tell the OS to run it.

Of course, modern CPUs and operating systems are really quite complicated, but that's the basic idea.

If you want to write a new compiler, here is how it's done:

  1. Write an interpreted language using something like the calculator example in pyparsing (or any other good parsing framework). That will get you up to speed on the basics of parsing.

  2. Write a translator. Translate your language into, say, Javascript. Now your language will run in a browser.

  3. Write a translator to something lower level, like LLVM, C, or Assembly.

You can stop here, this is a compiler. It's not an optimizing compiler, but that wasn't the question. You might also need to consider writing a linker and assembler, but do you really want to?

  1. (Insane) Write an optimizer. Large teams work for decades on this.

  2. (Sane) Get involved in an existing community. GCC, LLVM, PyPy, the core team working on any interpreter.

Source Link
wisty
  • 131
  • 2

I'm not an expert, but here is my stab:

You don't seem asking about writing a compiler, just an assembler. This isn't really magic.

Stealing someone elses answer from SO ( http://stackoverflow.com/questions/3826692/how-do-i-translate-assembly-to-binary), assembly looks like this:

label: LDA #$00 JMP label 

Then you run it through an assembler, and turn into into something like this:

$A9 $00 $4C $10 $00 

Only it's all squashed up, like this:

$A9 $00 $4C $10 $00 

It's really not magic.

You can't write that in notepad, because notepad uses ASCII (not hex). You would use a hex editor, or simply write the bytes out programatically. You write that hex out to a file, name it "a.exe" or "a.out", then tell the OS to run it.

Of course, modern CPUs and operating systems are really quite complicated, but that's the basic idea.

If you want to write a new compiler, here is how it's done:

  1. Write an interpreted language using something like the calculator example in pyparsing (or any other good parsing framework). That will get you up to speed on the basics of parsing.

  2. Write a translator. Translate your language into, say, Javascript. Now your language will run in a browser.

  3. Write a translator to something lower level, like LLVM, C, or Assembly.

You can stop here, this is a compiler. It's not an optimizing compiler, but that wasn't the question. You might also need to consider writing a linker and assembler, but do you really want to?

  1. (Insane) Write an optimizer. Large teams work for decades on this.

  2. (Sane) Get involved in an existing community. GCC, LLVM, PyPy, the core team working on any interpreter.

Post Made Community Wiki by wisty