Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an assembler work/how is it written? [closed]

In one of my classes, we are leaning about computer architecture and programming from the ground up. That means we started with pMOS/nMOS circuits, moved on to logic gates, then used logic gates to make registers and combinational logic units, and then analyzed a small microprocessor architecture (LC-3, in the case of my class).

The next thing we did was write programs in assembly, which is where I got a little confused.

Take for example: ADD R0, R1, R2 which should add the contents of registers 1 and 2 and store it in register 0. As a machine word (in the architecture we learned in class) this instruction would be 0001 000 001 000 010 and I understand, on some level, how that binary word is then converted into a voltage signal which sets the proper value in the various multiplexers and control units in the processor.

My question is, how is ADD R0, R1, R2 converted to 0001 000 001 000 010 by the assembler? To someone with my level of understanding, this process seems to be analogous to compiling a higher level language. However there can be no compiler here, since there is no language more basic than Assembly with which to write the compiler!

To put it another way, how is a program written to assemble the assembly code if it cannot itself be written in assembly. Is it written directly in binary by some genius and then hard coded into to program memory of the processor? I'm having trouble imagining a practical way to write an assembler than can be directly executed by the microprocessor.

like image 398
Sam Stern Avatar asked Oct 31 '12 17:10

Sam Stern


2 Answers

A long time ago, in a galaxy far away, the very first assemblers were written directly in machine code. Once you have an assembler, though, you can use it to assemble new assemblers, and you can also use it to assemble a compiler.

Once you have a compiler, you can use it to compile new assemblers.

So in practice, assemblers are written in higher level languages nowadays. Often C or C++. As to how they work, a very, very, simple assembler is essentially just a big switch statement; recognize the opcode, translate it to the corresponding machine encoding.

like image 146
Stephen Canon Avatar answered Oct 12 '22 08:10

Stephen Canon


I am actually doing this exact problem: assembling an assembler without having an assembler. The way I am doing it is with an Excel spreadsheet. The spreadsheet has formulas in it to lookup op codes etc and calculate the binary output. In the old days they did the same thing except they used paper spreadsheets instead of computer-based spreadsheets. Here is a screenshot of my manual assembler, the highlighted area is the machine code:

Screenshot of Manual Assembler

The area on the right is the assembly code. The area on the left is the intermediate calculations. Here is a closeup:

Closeup of manual assembler

So, the short answer to the question is: to do manual assembly in the old days they used a paper spreadsheet. Nowadays, to do manual assembly we use a computer spreadsheet (I use Excel).

In my spreadsheet the column labeled "Encode" is the actual binary code (machine code). The assembly instructions are on the right along with a description of what they are doing.

like image 5
Tyler Durden Avatar answered Oct 12 '22 09:10

Tyler Durden