I'm a computer design enthusiast working with Logisim simulator to create CPUs from scratch (out of logic gates). I have successfully built a working CPU which has similar instruction set to MOS 6502 processor. The CPU understands machine code only. My question is and I swear I have researched this a lot: how do you implement an assembly language into a CPU? What hardware is required or is it purely software? My guess is that you need to implement a database with letters, as well as some sort of a decoder that will read your written assembly language and translate it into machine language ready to be executed. I wasn't able to find a decent answer to my question anywhere. Any guidance is greately appreciated.
Asked
Active
Viewed 666 times
1
-
2You understand that a CPU cannot execute assembly language, right? – Sneftel Mar 18 '19 at 22:23
-
Assembly language is basically 1:1 with the instructions the processor executes - so if you look at the manual for the processor you will see the instructions and coresponding assembly language equivalent. Of course it’s not quite as simple as that, but basically assembly language is the first step away frm the 1s and 0s of the binary that the CPU really understands. – DisappointedByUnaccountableMod Mar 18 '19 at 22:25
-
You pre-convert assembly language into machine language by running it through an *assembler*. How to write one is beyond the scope of an SO answer but simple ones are sometimes available in academic course materials (or even included in better debug monitor programs to run on the target) while fancier ones able to calculate the position of symbols and potentially implement macros can be larger pieces of software. Given the historic usage of the 6502 you shouldn't have much trouble finding a small assembler. – Chris Stratton Mar 18 '19 at 22:25
-
But what then does it? CPU has to be involved in it... what hardware executes assembly language? Or is the whole assembler written as software in memory? – Senijs Mar 18 '19 at 22:26
-
Hardware *doesn't* "execute assembly language", it executes the *machine language* output of the assembler. – Chris Stratton Mar 18 '19 at 22:27
-
I see. So the hardware I'm actually looking for is the assembler. Chris Stratton, what exactly do you mean by "academic course materials" on the matter of how to build a simple assembler (for 6502)? – Senijs Mar 18 '19 at 22:31
-
@Senijs: The assembler is *software* that runs on the CPU, translating text into machine code. As far as the CPU is concerned, there is *nothing* special about an assembler, no special support for it. The first ever assembler had to be hand-written in machine code so it could run on a CPU, but later assemblers for later CPUs could be cross-developed. See also [How was the first assembler for a new home computer platform written?](//retrocomputing.stackexchange.com/q/3326) – Peter Cordes Mar 18 '19 at 22:32
-
@PeterCordes, thank you for the link. I want to write the assembler by hand in machine code, I probably should have mentioned that in my question. It seems like it would be a huuuge code to write though. I would love to see any examples of assembler datasheet or the process of making one. – Senijs Mar 18 '19 at 22:39
-
Assemblers don't have "datasheets", they're software. Maybe you want a 6502 ISA reference that lists the encodings of every instruction? And yes, they're not tiny even for a simple compact ISA like 6502. IDK why you'd want to write one by hand in machine code, instead of in 6502 assembler (and then assemble it into 6502 machine code using an existing 6502 assembler on another computer.) Or easier, C or C++, or an even higher-level language that still allows easy bitfields and byte manipulation. – Peter Cordes Mar 18 '19 at 22:43
-
Also [Were the first assemblers written in machine code?](//softwareengineering.stackexchange.com/q/129123) and other links you'll find in the linked duplicates of this question, as well as the direct answers. – Peter Cordes Mar 18 '19 at 22:48
-
@PeterCordes , Yeah, I suppose writing an assembler in a higher language is better and then translating that to my machine code. Sorry for a duplicate question, I really wanted to find a quick answer to my question and couldn't find anything... Now I know that assembler is pretty much a pure software that requires a ton of coding. – Senijs Mar 18 '19 at 22:53
-
Say, are ASCII characters also written purely in memory? Just like assembler? – Senijs Mar 18 '19 at 22:55
-
[How do computers translate everything to binary? When they see a binary code, how do they know if it represents a number or a word or an instruction?](//stackoverflow.com/q/26267044) – Peter Cordes Mar 18 '19 at 22:58
-
I do know that computers are using machine code from the start to make complex languages, characters, etc. But thank you for all your answers. – Senijs Mar 18 '19 at 23:03
-
"ASCII characters" don't exist from the HW point of view. From HW point of view there are things like eight bits per byte, consecutive bytes storage as "RAM memory" which can be read/written by CPU by using address+data bus and memory controller, internal CPU storage (registers), etc.. there's nothing about ASCII or characters. The farthest the HW may get to "ASCII" are some graphic cards, which provide some kind of "text" mode, which means they interpret content of video frame buffer as some index into font graphics, where particular glyphs may be positioned in a way natural for ASCII ... – Ped7g Mar 19 '19 at 09:44
-
So if you have `print("hello world")` type of program, it does convert 8bit integers like `104` (letter `h` in ASCII encoding) into whatever is needed by the graphics card to show pixels forming shape of "h" on the screen. On some graphic cards and their selected display mode that may be as trivial as writing value `104` into text-mode video ram (maybe with some extra byte describing colour attributes), on some others that may need drawing actual single pixels into multiple "bitmap" planes in the shape of "h", i.e. having also font data, etc.. hundreds of instructions for single letter. – Ped7g Mar 19 '19 at 09:47