r/AskComputerScience • • Jul 11 '16

How do you program a programming language?

I.e. How do you get a computer to understand a language? How does it actually make the transition from that language into assembly language?

45 Upvotes

18 comments sorted by

View all comments

Show parent comments

1

u/lepriccon22 Jul 11 '16

What does that mean practically? How do I teach a computer to understand what naming a variable is? How do I teach it to understand what a for loop is, etc?

5

u/anamorphism Jul 11 '16 edited Jul 11 '16

you're not teaching the computer anything.

you're basically just writing a translation program.

you would write a program that takes in for (int i = 0; i < 10; ++i); and outputs assembly to represent what you want that line of code to do.

something like

  • allocate 32 bits of memory for i

  • write 0 to all of those bits

  • allocate 32 bits for 10

  • write 10 to those bits

  • declare label 1

  • compare i to 10

  • jump to label 2 if i is less than 10

  • add 1 to i

  • jump to label 1

  • declare label 2

1

u/lepriccon22 Jul 12 '16

allocate 32 bits of memory for i write 0 to all of those bits allocate 32 bits for 10 write 10 to those bits declare label 1 compare i to 10 jump to label 2 if i is less than 10 add 1 to i jump to label 1 declare label 2

How does it know how to understand this, though?

1

u/juuular Jul 12 '16

At the end of the day these simple statements like these (move bit A to spot B, etc) boil down to hardware instructions (aka just 1's and 0's). At the very bottom is the circuit board - different electronic parts that interact through electricity.

The most important part is the transistor - you can apply some voltage to "set" it to 1 of 2 states, and you can also apply some voltage differently to get which state it is in. That "move bit" command would be converted to an electrical signal to a transistor that makes it output it's state, and then route that signal to a different transistor that gets set.

This is massively oversimplified of course, but at the end of the day these instructions are converted into electrical signals being sent from one place to another and changing the state of different electrical devices.

Theoretically you could implement a CPU (and "compile" assembly instructions) by having slaves run around shuffling notecards with different numbers on them, just like we shuffle numbers around on silicon transistors. That would be much slower and more memory inefficient, though.