1

This question already has an answer here:

I know that assembler is the one which converts to machine code. So here in 8085 instruction set LDA has opcode "3A". My question is how assembler convert mnemonics to opcode and finally to machine code.

How LdA mnemonic is converted to 3A hex code

If I come up with a new architecture and new instruction set. How then I create an assembler and mnemonics.

marked as duplicate by maple_shaft Jun 4 '16 at 2:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    I voted to close this question as Too Broad. While I admire your desire to learn, you're doing it in the hardest possible way: by asking a series of questions directed at random people on the Internet. The easier and faster way to learn what you want to learn is to pick up a good book, take a class or watch a video, and then write some code. – Robert Harvey Jun 3 '16 at 16:14
  • 2
    You seem to be coming at this from the wrong direction. If you want to understand this, you should probably start by understanding what machine code is. – JimmyJames Jun 3 '16 at 16:56
  • 2
    you write a program that reads lines of text from a file. when it sees the text LDA, by look for an L then a D and then an A with no white space between but possibly on other sides then it outputs 0x3A in some format. Elementary programming problem, how do you read a file full of numbers and say graph them or average them or whatever? no different. read a file convert the data into some other format. – old_timer Jun 3 '16 at 17:35
  • I rejected the close reason and marked it as duplicate to a similar question. I then reopened the duplicate. Hopefully this gives you the answer you need. – maple_shaft Jun 4 '16 at 2:06
  • It looks it up in a big list of opcodes that the people who made the assembler typed in. – immibis Nov 20 '18 at 3:01
5

Short answer: the same way any other compiler works.

Long answer: A program that takes programming code input in one language and transforms it to output in a different language is called a compiler. (An assembler is a special type of compiler whose input language is assembly language and whose output language is machine code.)

A compiler's work can be broadly described as consisting of three phases: parsing (reading the code and building an internal data structure describing what it says), semantic analysis (making sure the parsed data is actually valid and determining what it means), and code generation (turning the analyzed data into the output language.)

For an assembler, the semantic analysis phase would probably involve looking up each instruction name in a table, then calling a method with that instruction's arguments to figure out if it matches any of the sets of arguments that are valid for that instruction. This determines what hex code it will use. (For example, the hex opcode for MOV (register, register) is different from the hex opcode for MOV (register, memory address) even though the assembly mnemonic is the same.)

Once your parse data has been annotated with semantic data, your code generator can go over it and read the hex opcodes and encode it into machine code output according to the rules of the processor's machine language, in the form of a stream of numbers.

There is no software that "turns it into 1s and 0s." Software operates at a higher abstraction level; breaking down the larger numbers it works with into bits happens at the hardware level, where it's actually implemented as "higher voltage" and "lower voltage". The numbers 1 and 0 are just for our convenience, but don't have much actual meaning (at any level) these days.

  • The comments really don't improve the answer or ask for clarification on it so I purged them. We should talk about your concerns more in chat or in meta. – maple_shaft Jun 4 '16 at 1:59
  • See my comments to the OP's question – maple_shaft Jun 4 '16 at 2:04
  • 4
    @maple_shaft: If you're going to nuke a conversation, at least move it to a chat room so that we can get back to it. I was in the middle of writing a meta post to discuss this, but now I can't cite any of this material because you deleted it. – Robert Harvey Jun 4 '16 at 20:19
  • 4
    @maple_shaft: Consider a chat room next time. They are essentially free. Respectfully, I don't believe that it's a moderator's job to make a value judgement about the quality of a conversation unless the conversation is abusive in some way. By the way, I did try to move the conversation to chat when the link was presented to me, but got an error. – Robert Harvey Jun 4 '16 at 20:27
  • 1
    @maple_shaft: Meta question is here, if you're interested: meta.stackexchange.com/questions/279947/… – Robert Harvey Jun 4 '16 at 20:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.