Connect with us

How assembly instruction convert into binary

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Sep 20, 2013.

Scroll to continue with content
  1. vead

    vead

    473
    14
    Nov 27, 2011
    I know the assembler convert assembly instruction into machine code (MOV A, #10H convert in machine code like 01010101). But I want to know
    how assembler convert MOV A, #10H into 01010101?(internal process of hardware)
    How the hardware of microcontroller works with assembler?
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,312
    2,238
    Nov 17, 2011
    Which processor are you talking about? That's the minimum information we need. Different processors have different assembly and machine instructions.

    The manual of the processor can tell you the conversion. Today it's usually no longer done by hand. A tool called assembler and linker is used. The assembler in its most simple form can look up the machine code in a table and put out the corresponding machine code. Of course, modern assemblers do more than that. Here's a site which offers a documentation on building an assembler. From that documentation you can work out the principle of an assembler.

    A CPU does not work on assembler instructions directly. It operates on binary machine code. This code is generated from the assembly language source by an´assembler and linker. Or from a higher programming language by a cmplier.
     
  3. Solidus

    Solidus

    349
    4
    Jun 19, 2011
    The mnemonic (assembly instruction) is merely a representation of the binary sequence that is read by the instruction decoder.

    When an instruction is fetched, the instruction decoder, a portion of the execution unit, reads the instruction off the operand, which is the whole multi-byte segment in memory.

    For x86 architectures, the instruction is composed of 4 bytes (32 bits) and has the following structure.

    For example, in the 32 bits allotted to an operand, the most significant 5-8 bits form the actual instruction, such as ADD, JMP, or MOV. The other bit allotments in the first byte are usually specific flag bits that signal the various varieties of that instruction.

    The second byte of the instruction is usually register information, such as the register of the source/destination, and flags that signal whether the data is immediate or normal priority, as well as whether the data should be treated as a number or word. The second byte in some instructions can be interchanged for data.

    Bytes 3 and 4 are usually data, and if they aren't data, they usually are not present.

    What happens is the assembler is very rudimentary - that is, all it does is go down the list of code, and ignoring comments, takes each line of instruction and converts it to its binary counterpart. It would not be conceptually difficult for even a moderately skilled programmer to write a basic assembler provided they had the knowledge of the opcodes. There is no "conversion" per se - the instructions translate literally. There is no black box here, nothing assumed by the assembler. Every line you type is what you get. Each instruction you write corresponds exactly to one instruction cycle, unless you use jump branching to form a loop.
     
  4. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    If you get the 'Programmers reference manual' for the microprocessor you use, it will explain the structure and list the codes of the different instruction, The assembler is only a textparser that convert the mnemonics from text to binary values, and combine them with the relative address information. Actual addresses are added in the linker step.

    It is no problem to assemble small programs by hand, using the instruction tables. The problem will be when you need to change the program and add or remove instructions, disrupting the addresses in the sequence. If you have an assembler, don't waste your time on inventing the wheel again.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-